# Homework 34, challenge: collect a target. # # A player square moves with arrow keys. A stationary target circle is drawn # somewhere on screen. When the player's rect overlaps the target (use # colliderect or a distance check for circles), move the target to a new # random position and print "collected!" to the terminal. # # Run with: python exercises/34/homework/04-collect-target.py import pygame import random pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Collect Target") clock = pygame.time.Clock() 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) 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, print "collected!" and move target to a random position screen.fill((20, 20, 30)) pygame.draw.circle(screen, (255, 220, 0), (target.centerx, target.centery), target.width // 2) pygame.draw.rect(screen, (100, 200, 255), player) pygame.display.flip() clock.tick(60) pygame.quit()