import pygame import sys import random import math # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 # Three times as big FPS = 30 PACMAN_SIZE = 20 GHOST_SIZE = 10 WHITE = (255, 255, 255) YELLOW = (255, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) # Pac-Man class class Pacman(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((PACMAN_SIZE, PACMAN_SIZE), pygame.SRCALPHA) pygame.draw.circle(self.image, YELLOW, (PACMAN_SIZE // 2, PACMAN_SIZE // 2), PACMAN_SIZE // 2) self.rect = self.image.get_rect(center=(x, y)) self.speed = 5 def update(self, keys, pellets, ghosts): dx, dy = 0, 0 if keys[pygame.K_LEFT]: dx = -self.speed elif keys[pygame.K_RIGHT]: dx = self.speed elif keys[pygame.K_UP]: dy = -self.speed elif keys[pygame.K_DOWN]: dy = self.speed self.rect.x += dx self.rect.y += dy # Check collisions with pellets hit_list = pygame.sprite.spritecollide(self, pellets, True) for pellet in hit_list: print("Nom nom!") # Check collisions with ghosts if pygame.sprite.spritecollideany(self, ghosts): print("Game Over!") pygame.quit() sys.exit() # Ghost class class Ghost(pygame.sprite.Sprite): def __init__(self, x, y, pacman): super().__init__() self.image = pygame.Surface((GHOST_SIZE, GHOST_SIZE), pygame.SRCALPHA) pygame.draw.circle(self.image, RED, (GHOST_SIZE // 2, GHOST_SIZE // 2), GHOST_SIZE // 2) self.rect = self.image.get_rect(center=(x, y)) self.speed = 2 # Slower speed self.pacman = pacman def update(self): # Calculate distance to Pac-Man distance = math.sqrt((self.pacman.rect.x - self.rect.x) ** 2 + (self.pacman.rect.y - self.rect.y) ** 2) # Move towards Pac-Man dx = (self.pacman.rect.x - self.rect.x) / distance * self.speed dy = (self.pacman.rect.y - self.rect.y) / distance * self.speed self.rect.x += dx self.rect.y += dy # Pellet class class Pellet(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((10, 10)) self.image.fill(WHITE) self.rect = self.image.get_rect(center=(x, y)) # Initialize game window screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pac-Man") clock = pygame.time.Clock() # Create sprites all_sprites = pygame.sprite.Group() pellets = pygame.sprite.Group() ghosts = pygame.sprite.Group() pacman = Pacman(WIDTH // 2, HEIGHT // 2) all_sprites.add(pacman) # Increase the number of pellets for _ in range(200): pellet = Pellet(random.randrange(WIDTH), random.randrange(HEIGHT)) all_sprites.add(pellet) pellets.add(pellet) # Ghosts spawn in each corner ghost1 = Ghost(50, 50, pacman) ghost2 = Ghost(WIDTH - 50, 50, pacman) ghost3 = Ghost(50, HEIGHT - 50, pacman) ghost4 = Ghost(WIDTH - 50, HEIGHT - 50, pacman) all_sprites.add(ghost1, ghost2, ghost3, ghost4) ghosts.add(ghost1, ghost2, ghost3, ghost4) # Font for displaying messages font = pygame.font.Font(None, 36) # Game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() pacman.update(keys, pellets, ghosts) ghosts.update() # Check win condition if len(pellets) == 0: win_text = font.render("You win!", True, WHITE) screen.blit(win_text, (WIDTH // 2 - 80, HEIGHT // 2 - 18)) pygame.display.flip() pygame.time.delay(2000) # Display "You win!" for 2 seconds running = False # Draw everything screen.fill(BLUE) all_sprites.draw(screen) # Update display pygame.display.flip() # Cap the frame rate clock.tick(FPS) # Quit the game pygame.quit() sys.exit()