# Homework 35, problem 3: collect and score. # # Use the arrow-key mover and collision pattern. Each time the player touches # the target, add one to the score and reposition the target. Display the # score at the top of the screen. # # Run with: python exercises/35/homework/03-collect.py import pygame import random pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Collect") clock = pygame.time.Clock() font = pygame.font.SysFont("arial", 28) W, H = 800, 600 player = pygame.Rect(370, 270, 50, 50) target = pygame.Rect(random.randint(0, W-40), random.randint(0, H-40), 40, 40) score = 0 speed = 5 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # TODO: move player with get_pressed and keep it inside the window # TODO: on overlap, add 1 to score and reposition target screen.fill((20, 20, 40)) pygame.draw.circle(screen, (255, 220, 0), target.center, target.width // 2) pygame.draw.rect(screen, (100, 200, 255), player) # TODO: render "Score: X" and blit it at the top of the screen pygame.display.flip() clock.tick(60) pygame.quit()