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