Small project codes for simple games


 

3 Simple Python Game Projects for Beginners

Building small games is a fun way to learn programming and engage beginners. Below are three Python projects for classic games: Rock, Paper, Scissors, Tic-Tac-Toe (XO), and Guess the Number. Each includes complete code, explanations, and example outputs, perfect for new coders to explore and enjoy.

1. Rock, Paper, Scissors

This game lets a player compete against the computer by choosing rock, paper, or scissors.

import random
def rock_paper_scissors():
choices = ["rock", "paper", "scissors"]
player = input("Enter rock, paper, or scissors: ").lower().strip()
if player not in choices:
return "Invalid choice! Please try again."
computer = random.choice(choices)
print(f"Computer chose: {computer}")
if player == computer:
return "It's a tie!"
elif (player == "rock" and computer == "scissors") or \
(player == "paper" and computer == "rock") or \
(player == "scissors" and computer == "paper"):
return "You win!"
else:
return "Computer wins!"
# Example usage
print(rock_paper_scissors())

Explanation:

  • The player inputs their choice, and the computer randomly selects from choices.
  • The code checks for valid input, compares choices, and determines the winner based on standard rules (e.g., rock beats scissors).
  • Use Case: Teaches random selection, conditionals, and user input handling.
  • Example Output:
    Enter rock, paper, or scissors: rock
    Computer chose: scissors
    You win!

2. Tic-Tac-Toe (XO)

A two-player game where players take turns marking X or O on a 3x3 grid to get three in a row.

def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)
def check_winner(board, player):
# Check rows, columns, and diagonals
for i in range(3):
if all(board[i][j] == player for j in range(3)) or \
all(board[j][i] == player for j in range(3)):
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 tic_tac_toe():
board = [[" " for _ in range(3)] for _ in range(3)]
players = ["X", "O"]
turn = 0
for _ in range(9): # Max 9 moves
print_board(board)
player = players[turn % 2]
row = int(input(f"Player {player}, enter row (0-2): "))
col = int(input(f"Player {player}, enter column (0-2): "))
if row not in range(3) or col not in range(3) or board[row][col] != " ":
print("Invalid move! Try again.")
continue
board[row][col] = player
if check_winner(board, player):
print_board(board)
return f"Player {player} wins!"
turn += 1
print_board(board)
return "It's a tie!"
# Example usage
print(tic_tac_toe())

Explanation:

  • The game uses a 3x3 list to represent the board, displays it, and alternates between players X and O.
  • Players input row and column numbers (0-2) to place their mark. The check_winner function checks rows, columns, and diagonals for a win.
  • Use Case: Teaches 2D lists, loops, and conditionals while being interactive.
  • Example Output:
    | |
    ---------
    | |
    ---------
    | |
    ---------
    Player X, enter row (0-2): 1
    Player X, enter column (0-2): 1
    | |
    ---------
    | X |
    ---------
    | |
    ---------
    Player O, enter row (0-2): 0
    Player O, enter column (0-2): 0
    O | |
    ---------
    | X |
    ---------
    | |
    ---------
    ... (continues until a win or tie)
    Player X wins!

3. Guess the Number

The player guesses a number between 1 and 100, and the computer provides hints.

import random
def guess_the_number():
secret = random.randint(1, 100)
attempts = 0
max_attempts = 10
print("Guess the number between 1 and 100!")
while attempts < max_attempts:
guess = int(input("Enter your guess: "))
attempts += 1
if guess == secret:
return f"Congratulations! You guessed it in {attempts} attempts!"
elif guess < secret:
print("Too low! Try again.")
else:
print("Too high! Try again.")
return f"Game over! The number was {secret}."
# Example usage
print(guess_the_number())

Explanation:

  • The computer generates a random number, and the player has 10 attempts to guess it.
  • After each guess, the program provides hints ("too high" or "too low") to guide the player.
  • Use Case: Teaches random number generation, loops, and user interaction.
  • Example Output:
    Guess the number between 1 and 100!
    Enter your guess: 50
    Too high! Try again.
    Enter your guess: 25
    Too low! Try again.
    Enter your guess: 42
    Congratulations! You guessed it in 3 attempts!

Tips for Extending These Games

  • Enhancements: Add a score system, play-again functionality, or a graphical interface using pygame or tkinter.
  • Learning Benefits: These games teach core programming concepts like loops, conditionals, lists, and random number generation.
  • Engagement: Their simplicity makes them perfect for beginners, while their interactive nature keeps players hooked.

Try coding and modifying these games to create your own versions and boost your programming skills!


We’ve put a lot of time and effort into writing and gathering this information, so we’d really appreciate it if you could share it with your friends.

Post a Comment

0 Comments