By AI Quick Tool
05 Mar, 2025 · 2 days ago
Coin Collector: Dodge Obstacles in Fun Pygame Adventure is a simple yet engaging 2D game built using the Pygame library in Python. In this game, you control a blue square (the player) using arrow keys to navigate a white screen. Your objective is to collect green coins that fall from the top to increase your score while avoiding red obstacles. If you collide with an obstacle, the game ends, and you can restart by pressing 'R'. The game features basic graphics, smooth animations, collision detection, and score tracking, making it a fun and educational project for Python beginners or game development enthusiasts.
1. Install Python
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Set up the display
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Coin Collector")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Player settings
PLAYER_SIZE = 40
player_x = WIDTH // 2
player_y = HEIGHT // 2
player_speed = 5
# Item settings
ITEM_SIZE = 20
COIN_SPEED = 3
OBSTACLE_SPEED = 4
# Initialize game variables
score = 0
font = pygame.font.Font(None, 36)
# Classes for game objects
class Item:
def __init__(self, color, speed):
self.x = random.randint(0, WIDTH - ITEM_SIZE)
self.y = -ITEM_SIZE
self.color = color
self.speed = speed
def move(self):
self.y += self.speed
def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, ITEM_SIZE, ITEM_SIZE))
# Create initial items
coins = [Item(GREEN, COIN_SPEED) for _ in range(3)]
obstacles = [Item(RED, OBSTACLE_SPEED) for _ in range(2)]
# Game loop
clock = pygame.time.Clock()
game_over = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and game_over:
if event.key == pygame.K_r: # Restart game
player_x = WIDTH // 2
player_y = HEIGHT // 2
score = 0
coins = [Item(GREEN, COIN_SPEED) for _ in range(3)]
obstacles = [Item(RED, OBSTACLE_SPEED) for _ in range(2)]
game_over = False
if not game_over:
# Player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < WIDTH - PLAYER_SIZE:
player_x += player_speed
if keys[pygame.K_UP] and player_y > 0:
player_y -= player_speed
if keys[pygame.K_DOWN] and player_y < HEIGHT - PLAYER_SIZE:
player_y += player_speed
# Update items
for coin in coins[:]:
coin.move()
if coin.y > HEIGHT:
coins.remove(coin)
coins.append(Item(GREEN, COIN_SPEED))
# Collision detection with coin
player_rect = pygame.Rect(player_x, player_y, PLAYER_SIZE, PLAYER_SIZE)
coin_rect = pygame.Rect(coin.x, coin.y, ITEM_SIZE, ITEM_SIZE)
if player_rect.colliderect(coin_rect):
coins.remove(coin)
coins.append(Item(GREEN, COIN_SPEED))
score += 1
for obstacle in obstacles[:]:
obstacle.move()
if obstacle.y > HEIGHT:
obstacles.remove(obstacle)
obstacles.append(Item(RED, OBSTACLE_SPEED))
# Collision detection with obstacle
obstacle_rect = pygame.Rect(obstacle.x, obstacle.y, ITEM_SIZE, ITEM_SIZE)
if player_rect.colliderect(obstacle_rect):
game_over = True
# Drawing
screen.fill(WHITE)
# Draw player
pygame.draw.rect(screen, BLUE, (player_x, player_y, PLAYER_SIZE, PLAYER_SIZE))
# Draw items
for coin in coins:
coin.draw()
for obstacle in obstacles:
obstacle.draw()
# Draw score
score_text = font.render(f"Score: {score}", True, (0, 0, 0))
screen.blit(score_text, (10, 10))
# Game over screen
if game_over:
game_over_text = font.render("Game Over! Press R to Restart", True, (0, 0, 0))
screen.blit(game_over_text, (WIDTH//2 - 150, HEIGHT//2))
pygame.display.flip()
clock.tick(60)
Coin Collector: Dodge Obstacles in Fun Pygame Adventure is a...
View