import pygame import random pygame.init() SCREEN_WIDTH = 600 SCREEN_HEIGHT = 400 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Coin Collector") clock = pygame.time.Clock() WHITE = (255, 255, 255) BLUE = (50, 100, 200) GOLD = (220, 180, 0) # Player: a rectangle with a position and size player = pygame.Rect(100, 100, 30, 30) player_speed = 4 # Coin: a fixed position on screen coin_x = random.randint(20, SCREEN_WIDTH - 20) coin_y = random.randint(20, SCREEN_HEIGHT - 20) coin_radius = 10 score = 0 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # TODO: read keyboard input and move the player # Hint: use pygame.key.get_pressed() # keys = pygame.key.get_pressed() # if keys[pygame.K_LEFT]: player.x -= player_speed # ... add UP, DOWN, RIGHT # TODO: keep the player inside the screen boundaries # Hint: clamp player.x between 0 and SCREEN_WIDTH - player.width # TODO: check whether the player has reached the coin # Hint: use player.collidepoint(coin_x, coin_y) # If they collide: add 1 to score, pick a new random coin position screen.fill(WHITE) # Draw the coin pygame.draw.circle(screen, GOLD, (coin_x, coin_y), coin_radius) # Draw the player pygame.draw.rect(screen, BLUE, player) # TODO: draw the score text on screen # Hint: create a font with pygame.font.SysFont("arial", 24) # then use font.render() and screen.blit() pygame.display.flip() clock.tick(60) pygame.quit()