# Homework 35, challenge: three lives. # # The player collects coins (score += 1 per coin). A second set of red squares # moves toward the player (you can pick a simple movement direction per # square). If the player touches a red square, lives -= 1 and the square # respawns. Display both "Score: X" and "Lives: X" on screen. When lives reach # 0, show "Game Over" and stop the game. # # Run with: python exercises/35/homework/04-three-lives.py import pygame import random pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Three Lives") clock = pygame.time.Clock() font = pygame.font.SysFont("arial", 28) large = pygame.font.SysFont("arial", 64) W, H = 800, 600 player = pygame.Rect(370, 270, 50, 50) coin = pygame.Rect(random.randint(0, W-30), random.randint(0, H-30), 30, 30) score = 0 lives = 3 speed = 4 game_over = False # TODO: create a list of red squares (pygame.Rect) at random positions running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # TODO: while not game_over: move the player, move the red squares, # handle touching a red square and collecting the coin screen.fill((20, 20, 40)) pygame.draw.circle(screen, (255, 220, 0), coin.center, coin.width // 2) pygame.draw.rect(screen, (100, 200, 255), player) # TODO: draw the red squares, "Score: X" and "Lives: X" # TODO: when game_over, show "Game Over" pygame.display.flip() clock.tick(60) pygame.quit()