Skip to main content

Game 1: Tic-Tac-Toe (2 Players in Console)

 

🎮 Game 1: Tic-Tac-Toe (2 Players in Console)

python
def print_board(board): for row in board: print(" | ".join(row)) print("-" * 5) def check_winner(board, player): for row in board: if all(s == player for s in row): return True for col in zip(*board): if all(s == player for s in col): return True if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player for i in range(3)): return True return False def play_game(): board = [[" "]*3 for _ in range(3)] current_player = "X" moves = 0 while moves < 9: print_board(board) row = int(input(f"{current_player}'s turn. Enter row (0-2): ")) col = int(input(f"{current_player}'s turn. Enter col (0-2): ")) if board[row][col] == " ": board[row][col] = current_player moves += 1 if check_winner(board, current_player): print_board(board) print(f"{current_player} wins!") return current_player = "O" if current_player == "X" else "X" else: print("Spot taken! Try again.") print_board(board) print("It's a tie!") play_game()

🎮 Game 2: Rock-Paper-Scissors (Player vs Computer)

python
import random def rps(): choices = ['rock', 'paper', 'scissors'] while True: player = input("Choose rock, paper, or scissors: ").lower() if player not in choices: print("Invalid choice.") continue computer = random.choice(choices) print(f"Computer chose: {computer}") if player == computer: print("It's a tie!") elif (player == 'rock' and computer == 'scissors') or \ (player == 'scissors' and computer == 'paper') or \ (player == 'paper' and computer == 'rock'): print("You win!") else: print("You lose!") if input("Play again? (y/n): ").lower() != 'y': break rps()

🎮 Game 3: Number Guessing Game

python
import random def guess_game(): number = random.randint(1, 100) tries = 0 print("Guess a number between 1 and 100.") while True: guess = int(input("Your guess: ")) tries += 1 if guess < number: print("Too low!") elif guess > number: print("Too high!") else: print(f"Correct! It took you {tries} tries.") break guess_game()

🎮 Game 4: Typing Speed Test

python
import time import random def typing_test(): sentences = [ "The quick brown fox jumps over the lazy dog", "Typing is a useful skill for everyone", "Practice makes perfect in everything" ] sentence = random.choice(sentences) print("Type this sentence:") print(sentence) input("Press Enter when ready...") start = time.time() typed = input("> ") end = time.time() if typed.strip() == sentence: print(f"Great! Time taken: {round(end - start, 2)} seconds") else: print("Oops! You typed it incorrectly.") print("Try again!") typing_test()

🎮 Game 5: Pygame Ball Bounce Game

python
import pygame import random pygame.init() win = pygame.display.set_mode((400, 400)) pygame.display.set_caption("Ball Bounce") ball = pygame.Rect(200, 200, 20, 20) ball_speed = [3, 3] clock = pygame.time.Clock() running = True while running: win.fill((0, 0, 0)) pygame.draw.ellipse(win, (255, 0, 0), ball) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False ball.x += ball_speed[0] ball.y += ball_speed[1] if ball.left <= 0 or ball.right >= 400: ball_speed[0] *= -1 if ball.top <= 0 or ball.bottom >= 400: ball_speed[1] *= -1 pygame.display.flip() clock.tick(60) pygame.quit()

Comments

Popular posts from this blog

Teacher Index – Teaching Jobs & Terms and Conditions in Nepal

Teacher Index – Terms & Conditions | K-Learning • The Jump Academy • Focus Edge Official Policy Teacher Index – Terms & Conditions Transparent engagement for teaching opportunities across Nepal with K-Learning , The Jump Academy , and Focus Edge Tuition Classes . Last updated: Scope: Grades 1–12, College & University • Institutional • Home Tuition • Online Contents 1. Introduction 2. Eligibility 3. Engagement Structure 4. Fees & Payment Terms 5. Teacher Responsibilities 6. Institutional Responsibilities 7. Termination & Suspension 8. Confidentiality & Data Protection 9. Disclaimer 10. Governing Law 11. Declaration & Acknowledgement Apply ...

PASHUPATI PROPERTIES - A REAL ESTATE COMPANY

🏡 Pashupati Properties – Your Trusted Real Estate Partner! 🏡 Looking to buy,sell, or invest in properties? Pashupati Properties is a renowned real estate company, helping you find the perfect property in Kathmandu, Bhaktapur, Lalitpur, Dhading, Chitwan, Pokhara, and Narayanghat. 📌 Fast Transactions: We work within 7 to 10 days to complete deals. 💸 Commission: We charge only 7% of the total traded amount. 📍 Located at New Baneshwor, Kathmandu 📞 Call/Message/WhatsApp: 9701322765 🌐 Website: https://empiricalten.blogspot.com/2025/02/pashupati-properties-realestate-company.html Whether you're searching for your dream home, commercial space, or investment opportunities, we are here to guide you with expert advice and reliable service. Let’s build your future together! 🏠✨ #RealEstate #PashupatiProperties #Kathmandu #Bhaktapur #Lalitpur #Dhading #Chitwan #Pokhara #Narayanghat #DreamHome #Investment #PropertyForSale #BuySellRent #NewBaneshwor #HouseH...

Grade 12 Accounts MCQ Quiz | Final Accounts, Worksheet & Cost Sheet Practice (With Answers & Explanations)

Grade 12 Accounts MCQ Quiz | Final Accounts • Worksheet • Cost Sheet Grade 12 Accounts MCQ Quiz Final Accounts • Worksheet • Cost Sheet All topics Final Accounts Worksheet Cost Sheet Shuffle Reset Submit Tap an option to check your answer instantly. Explanations appear after you choose. Use Shuffle to practice and Submit to see totals. 0 / 0 answered Score: 0% Topic: All