import pygame import random import sys # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 800, 600 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Dodge and Grow") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Game variables PLAYER_RADIUS = 20 PLAYER_COLOR = BLUE ENEMY_COLOR = RED FOOD_COLOR = GREEN PLAYER_SPEED = 5 ENEMY_SPEED = 3 FOOD_SPEED = 2 WIN_SIZE = 100 # The size needed to win the game # Font FONT = pygame.font.SysFont("comicsans", 30) class Ball: def __init__(self, x, y, radius, color, speed): self.x = x self.y = y self.radius = radius self.color = color self.speed = speed def draw(self, win): pygame.draw.circle(win, self.color, (self.x, self.y), self.radius) def move(self, dx, dy): self.x += dx self.y += dy def draw_window(player, enemies, food): WIN.fill(WHITE) player.draw(WIN) for enemy in enemies: enemy.draw(WIN) for f in food: f.draw(WIN) pygame.display.update() def handle_collision(player, enemies, food): for enemy in enemies: if (player.x - enemy.x)**2 + (player.y - enemy.y)**2 < (player.radius + enemy.radius)**2: if player.radius > enemy.radius: player.radius += 1 enemies.remove(enemy) else: return False # Game over for f in food: if (player.x - f.x)**2 + (player.y - f.y)**2 < (player.radius + f.radius)**2: player.radius += 1 food.remove(f) return True def main(): run = True clock = pygame.time.Clock() player = Ball(WIDTH//2, HEIGHT//2, PLAYER_RADIUS, PLAYER_COLOR, PLAYER_SPEED) enemies = [Ball(random.randint(0, WIDTH), random.randint(0, HEIGHT), random.randint(30, 50), ENEMY_COLOR, ENEMY_SPEED) for _ in range(5)] food = [Ball(random.randint(0, WIDTH), random.randint(0, HEIGHT), random.randint(10, 15), FOOD_COLOR, FOOD_SPEED) for _ in range(10)] while run: clock.tick(60) # 60 FPS for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player.x - player.radius - player.speed > 0: player.move(-player.speed, 0) if keys[pygame.K_RIGHT] and player.x + player.radius + player.speed < WIDTH: player.move(player.speed, 0) if keys[pygame.K_UP] and player.y - player.radius - player.speed > 0: player.move(0, -player.speed) if keys[pygame.K_DOWN] and player.y + player.radius + player.speed < HEIGHT: player.move(0, player.speed) for enemy in enemies: enemy.move(random.choice([-enemy.speed, enemy.speed]), random.choice([-enemy.speed, enemy.speed])) if enemy.x - enemy.radius < 0 or enemy.x + enemy.radius > WIDTH: enemy.move(-enemy.speed, 0) if enemy.y - enemy.radius < 0 or enemy.y + enemy.radius > HEIGHT: enemy.move(0, -enemy.speed) for f in food: f.move(random.choice([-f.speed, f.speed]), random.choice([-f.speed, f.speed])) if f.x - f.radius < 0 or f.x + f.radius > WIDTH: f.move(-f.speed, 0) if f.y - f.radius < 0 or f.y + f.radius > HEIGHT: f.move(0, -f.speed) if not handle_collision(player, enemies, food): run = False draw_window(player, enemies, food) if player.radius >= WIN_SIZE: WIN.fill(WHITE) win_text = FONT.render("You Win!", 1, BLACK) WIN.blit(win_text, (WIDTH//2 - win_text.get_width()//2, HEIGHT//2 - win_text.get_height()//2)) pygame.display.update() pygame.time.delay(5000) run = False pygame.quit() sys.exit() if __name__ == "__main__": main()