import pygame import random import time # Initialize Pygame pygame.init() # Set up display WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Coin Collector") # Colors WHITE = (255, 255, 255) YELLOW = (255, 255, 0) BLACK = (0, 0, 0) GRAY = (200, 200, 200) # Player player_size = 80 # Increased player size player_x = WIDTH // 2 - player_size // 2 player_y = HEIGHT // 2 - player_size // 2 player_speed = 3 # Slower starting speed player_movement = [0, 0] # [x, y] movement direction # Sprite images for walking animation sprite_images_right = [pygame.transform.scale(pygame.image.load('sprite_0.png'), (player_size, player_size)), pygame.transform.scale(pygame.image.load('sprite_1.png'), (player_size, player_size)), pygame.transform.scale(pygame.image.load('sprite_2.png'), (player_size, player_size)), pygame.transform.scale(pygame.image.load('sprite_3.png'), (player_size, player_size))] sprite_images_left = [pygame.transform.flip(image, True, False) for image in sprite_images_right] current_sprite_index = 0 # Index of the current sprite image last_sprite_update_time = time.time() sprite_update_interval = 0.2 # Interval between sprite image updates (in seconds) # Coins coins = [] coin_size = 20 coin_spawn_time = 0 coins_collected = 0 # Upgrade options speed_level = 1 speed_cost = 10 coin_spawn_rate_level = 1 coin_spawn_rate_cost = 20 magnet_level = 1 magnet_cost = 30 # Upgrade panel upgrade_panel_open = False upgrade_button_rect = pygame.Rect(WIDTH - 100, 10, 80, 30) upgrade_panel_width = 200 upgrade_panel_height = 200 upgrade_panel_rect = pygame.Rect((WIDTH - upgrade_panel_width) // 2, (HEIGHT - upgrade_panel_height) // 2, upgrade_panel_width, upgrade_panel_height) font = pygame.font.Font(None, 20) # Function to spawn coins def spawn_coin(): coins.append([random.randint(0, WIDTH - coin_size), random.randint(0, HEIGHT - coin_size)]) # Function to upgrade speed def upgrade_speed(): global speed_level, player_speed, coins_collected, speed_cost if coins_collected >= speed_cost: coins_collected -= speed_cost speed_level += 1 player_speed += 1 speed_cost *= 2 # Function to upgrade coin spawn rate def upgrade_coin_spawn_rate(): global coin_spawn_rate_level, coin_spawn_time, coins_collected, coin_spawn_rate_cost if coins_collected >= coin_spawn_rate_cost: coins_collected -= coin_spawn_rate_cost coin_spawn_rate_level += 1 coin_spawn_rate_cost *= 2 # Function to upgrade magnet def upgrade_magnet(): global magnet_level, coins_collected, magnet_cost if coins_collected >= magnet_cost: coins_collected -= magnet_cost magnet_level += 1 magnet_cost *= 2 # Function to check collision between player and coins def check_collision(player_rect, coin_rect): return player_rect.colliderect(coin_rect) # Game loop running = True while running: # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if upgrade_button_rect.collidepoint(event.pos): upgrade_panel_open = not upgrade_panel_open elif upgrade_panel_open and not upgrade_panel_rect.collidepoint(event.pos): upgrade_panel_open = False elif upgrade_panel_open: # Check for upgrade button clicks if upgrade_panel_rect.collidepoint(event.pos): if event.pos[1] > upgrade_panel_rect.top + 10 and event.pos[1] < upgrade_panel_rect.top + 30: upgrade_speed() elif event.pos[1] > upgrade_panel_rect.top + 50 and event.pos[1] < upgrade_panel_rect.top + 70: upgrade_coin_spawn_rate() elif event.pos[1] > upgrade_panel_rect.top + 90 and event.pos[1] < upgrade_panel_rect.top + 110: upgrade_magnet() # Player movement keys = pygame.key.get_pressed() player_movement = [0, 0] # Reset movement if keys[pygame.K_LEFT]: player_movement[0] -= player_speed if keys[pygame.K_RIGHT]: player_movement[0] += player_speed if keys[pygame.K_UP]: player_movement[1] -= player_speed if keys[pygame.K_DOWN]: player_movement[1] += player_speed # Update player position player_x += player_movement[0] player_y += player_movement[1] # Boundaries for player if player_x < 0: player_x = 0 elif player_x > WIDTH - player_size: player_x = WIDTH - player_size if player_y < 0: player_y = 0 elif player_y > HEIGHT - player_size: player_y = HEIGHT - player_size # Update sprite animation current_time = time.time() if player_movement != [0, 0]: # Only animate if moving if current_time - last_sprite_update_time > sprite_update_interval: current_sprite_index = (current_sprite_index + 1) % len(sprite_images_right) last_sprite_update_time = current_time # Coin spawning current_time = time.time() if current_time - coin_spawn_time > 1 / coin_spawn_rate_level: spawn_coin() coin_spawn_time = current_time # Remove coins that go off screen coins[:] = [coin for coin in coins if 0 <= coin[0] < WIDTH and 0 <= coin[1] < HEIGHT] # Define player hitbox player_hitbox = pygame.Rect(player_x, player_y, player_size, player_size) # Collision detection for coin in coins[:]: coin_rect = pygame.Rect(coin[0], coin[1], coin_size, coin_size) if check_collision(player_hitbox, coin_rect): coins.remove(coin) coins_collected += 1 # Clear screen screen.fill(WHITE) # Draw player sprite if player_movement[0] > 0: player_sprite = sprite_images_right[current_sprite_index] elif player_movement[0] < 0: player_sprite = sprite_images_left[current_sprite_index] else: player_sprite = sprite_images_right[0] # Default to right-facing when not moving screen.blit(player_sprite, (player_x, player_y)) # Draw coins for coin in coins: pygame.draw.ellipse(screen, YELLOW, (coin[0], coin[1], coin_size, coin_size)) # Draw upgrade button pygame.draw.rect(screen, GRAY, upgrade_button_rect) upgrade_text = font.render("Upgrades", True, BLACK) upgrade_text_rect = upgrade_text.get_rect(center=upgrade_button_rect.center) screen.blit(upgrade_text, upgrade_text_rect) # Draw upgrade panel if open if upgrade_panel_open: pygame.draw.rect(screen, GRAY, upgrade_panel_rect) upgrade_panel_text = font.render("Speed: Level {} - Cost: {}".format(speed_level, speed_cost), True, BLACK) screen.blit(upgrade_panel_text, (upgrade_panel_rect.left + 10, upgrade_panel_rect.top + 10)) upgrade_panel_text = font.render("Coin Spawn Rate: Level {} - Cost: {}".format(coin_spawn_rate_level, coin_spawn_rate_cost), True, BLACK) screen.blit(upgrade_panel_text, (upgrade_panel_rect.left + 10, upgrade_panel_rect.top + 50)) upgrade_panel_text = font.render("Magnet: Level {} - Cost: {}".format(magnet_level, magnet_cost), True, BLACK) screen.blit(upgrade_panel_text, (upgrade_panel_rect.left + 10, upgrade_panel_rect.top + 90)) # Draw counter font = pygame.font.Font(None, 36) text = font.render("Coins collected: " + str(coins_collected), True, YELLOW) screen.blit(text, (10, 10)) # Update display pygame.display.update() # Frame rate pygame.time.Clock().tick(60) # Quit Pygame pygame.quit()