import pygame import random pygame.init() SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Collect All Coins") clock = pygame.time.Clock() WHITE = (255, 255, 255) BLUE = (50, 100, 200) GOLD = (220, 180, 0) BLACK = (0, 0, 0) RED = (200, 50, 50) font = pygame.font.SysFont("arial", 24) COIN_COUNT = 5 COIN_RADIUS = 10 TIME_LIMIT = 30 # seconds player = pygame.Rect(300, 220, 30, 30) player_speed = 4 def make_coins(count): coins = [] for _ in range(count): x = random.randint(COIN_RADIUS, SCREEN_WIDTH - COIN_RADIUS) y = random.randint(COIN_RADIUS, SCREEN_HEIGHT - COIN_RADIUS) coins.append([x, y]) return coins coins = make_coins(COIN_COUNT) # TODO: track how many coins have been collected collected = 0 # TODO: set up a timer # Hint: each frame the clock ticks at 60 fps # frames_elapsed counts how many frames have passed frames_elapsed = 0 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # TODO: move player with arrow keys (same pattern as coin-collector project) # TODO: clamp player inside screen bounds # TODO: check each coin for collision with the player # Hint: iterate over coins with a regular index so you can remove them # if player.collidepoint(coin[0], coin[1]): remove it, add to collected # TODO: increment frames_elapsed each frame # TODO: calculate seconds_left = TIME_LIMIT - frames_elapsed // 60 # TODO: if seconds_left <= 0, show a game-over screen and stop the loop # TODO: if collected == COIN_COUNT, show a win screen and stop the loop screen.fill(WHITE) for coin in coins: pygame.draw.circle(screen, GOLD, (coin[0], coin[1]), COIN_RADIUS) pygame.draw.rect(screen, BLUE, player) # TODO: draw "Coins: X / Y" in the top-left corner # TODO: draw "Time: X" in the top-right corner pygame.display.flip() clock.tick(60) pygame.quit()