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) BLACK = (0, 0, 0) font = pygame.font.SysFont("arial", 24) player = pygame.Rect(100, 100, 30, 30) player_speed = 4 def new_coin_position(): x = random.randint(20, SCREEN_WIDTH - 20) y = random.randint(20, SCREEN_HEIGHT - 20) return x, y coin_x, coin_y = new_coin_position() coin_radius = 10 score = 0 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.x -= player_speed if keys[pygame.K_RIGHT]: player.x += player_speed if keys[pygame.K_UP]: player.y -= player_speed if keys[pygame.K_DOWN]: player.y += player_speed # Keep player inside the screen player.x = max(0, min(player.x, SCREEN_WIDTH - player.width)) player.y = max(0, min(player.y, SCREEN_HEIGHT - player.height)) # Check collection if player.collidepoint(coin_x, coin_y): score += 1 coin_x, coin_y = new_coin_position() screen.fill(WHITE) pygame.draw.circle(screen, GOLD, (coin_x, coin_y), coin_radius) pygame.draw.rect(screen, BLUE, player) score_text = font.render("Score: " + str(score), True, BLACK) screen.blit(score_text, (10, 10)) pygame.display.flip() clock.tick(60) pygame.quit()