Python Game Development Summer Crash Course
Python Game Development Summer Crash Course
What You'll Build: A game where the computer picks a random number
(1-100) and the player guesses it.
python
Copy
import random
Day 2: Rock-Paper-Scissors
What You'll Build: Play against the computer in this classic game.
python
Copy
import random
python
Copy
import random
def roll_dice(sides=6):
return random.randint(1, sides)
while True:
input("Press Enter to roll (or 'q' to quit)...")
if input().lower() == 'q':
break
print("You rolled:", roll_dice(sides))
What You'll Build: Guess letters to save a stick figure from hanging!
python
Copy
import random
print(" ".join(guessed))
while tries > 0 and "_" in guessed:
guess = input("Guess a letter: ").lower()
if guess in word:
for i, letter in enumerate(word):
if letter == guess:
guessed[i] = guess
else:
tries -= 1
print(f"Wrong! {tries} tries left.")
print(" ".join(guessed))
python
Copy
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)
def check_win(board):
# Check rows, columns, diagonals
for row in board:
if row[0] == row[1] == row[2] != " ":
return True
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] != " ":
return True
if board[0][0] == board[1][1] == board[2][2] != " ":
return True
if board[0][2] == board[1][1] == board[2][0] != " ":
return True
return False
while True:
print_board(board)
print(f"Player {current_player}'s turn.")
row = int(input("Row (0-2): "))
col = int(input("Column (0-2): "))
python
Copy
import pygame
import time
import random
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != "DOWN":
direction = "UP"
elif event.key == pygame.K_DOWN and direction != "UP":
direction = "DOWN"
elif event.key == pygame.K_LEFT and direction != "RIGHT":
direction = "LEFT"
elif event.key == pygame.K_RIGHT and direction != "LEFT":
direction = "RIGHT"
# Move snake
head_x, head_y = snake[0]
if direction == "UP":
new_head = (head_x, head_y - 10)
elif direction == "DOWN":
new_head = (head_x, head_y + 10)
elif direction == "LEFT":
new_head = (head_x - 10, head_y)
elif direction == "RIGHT":
new_head = (head_x + 10, head_y)
snake.insert(0, new_head)
# Draw everything
screen.fill((0, 0, 0))
for segment in snake:
pygame.draw.rect(screen, (0, 255, 0), (segment[0], segment[1], 10, 10))
pygame.draw.rect(screen, (255, 0, 0), (food[0], food[1], 10, 10))
pygame.display.update()
clock.tick(15)
Choose from:
Space Invaders
Flappy Bird Clone
Maze Runner
(Full code templates provided in PDF...)