Creating a simple Pac-Man game that is controllable using arrow keys can be done using Python and the pygame library. Below is a complete implementation of a basic Pac-Man game. This version includes a maze, Pac-Man, and movement controlled by the arrow keys.
Full Implementation of Pac-Man Game
python
import pygame
import sys
# Initialize pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
# Colors
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
# Maze layout (1 = wall, 0 = path)
maze = [
"11111111111111111111",
"10000000000000000001",
"10111101111101111001",
"10000000000000001001",
"10111011111101111001",
"10001000000000000001",
"11101111011111111011",
"10000000000000000001",
"10111101111101111111",
"10000000000000000001",
"11111111111111111111"
]
# Tile size
TILE_SIZE = 20
# Screen setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pac-Man")
# Clock for controlling frame rate
clock = pygame.time.Clock()
# Pac-Man properties
pacman_x, pacman_y = 1, 1 # Initial position in grid
pacman_speed = 1 # Grid cells per frame
# Function to draw the maze
def draw_maze():
for row_index, row in enumerate(maze):
for col_index, cell in enumerate(row):
if cell == "1": # Wall
pygame.draw.rect(screen, BLUE, (col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE))
elif cell == "0": # Path
pygame.draw.rect(screen, BLACK, (col_index * TILE_SIZE, row_index * TILE_SIZE, TILE_SIZE, TILE_SIZE))
# Function to draw Pac-Man
def draw_pacman(x, y):
pygame.draw.circle(screen, YELLOW, (x * TILE_SIZE + TILE_SIZE // 2, y * TILE_SIZE + TILE_SIZE // 2), TILE_SIZE // 2)
# Main game loop
running = True
while running:
screen.fill(BLACK) # Clear screen
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get key presses
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and maze[pacman_y - 1][pacman_x] == "0":
pacman_y -= pacman_speed
if keys[pygame.K_DOWN] and maze[pacman_y + 1][pacman_x] == "0":
pacman_y += pacman_speed
if keys[pygame.K_LEFT] and maze[pacman_y][pacman_x - 1] == "0":
pacman_x -= pacman_speed
if keys[pygame.K_RIGHT] and maze[pacman_y][pacman_x + 1] == "0":
pacman_x += pacman_speed
# Draw everything
draw_maze()
draw_pacman(pacman_x, pacman_y)
# Update display
pygame.display.flip()
# Cap the frame rate
clock.tick(10)
# Quit pygame
pygame.quit()
sys.exit()
Explanation of the Code
Maze Representation:
The maze is represented as a 2D list of strings, where "1" represents walls and "0" represents paths.
Each cell corresponds to a square of size TILE_SIZE.
Pac-Man Movement:
Pac-Man's position is tracked using (pacman_x, pacman_y) in grid coordinates.
Movement is controlled by checking the arrow keys (K_UP, K_DOWN, K_LEFT, K_RIGHT) and ensuring the target cell is not a wall ("1").
Drawing:
The maze is drawn using pygame.draw.rect for walls and paths.
Pac-Man is drawn as a yellow circle using pygame.draw.circle.
Game Loop:
The game runs in a loop, handling events, updating Pac-Man's position, and redrawing the screen.
Frame Rate:
The frame rate is capped at 10 frames per second (clock.tick(10)) for smooth movement.
How to Run the Code
Install pygame if you don't already have it:
bash
pip install pygame
Save the code to a file, e.g., pacman.py.
Run the script:
bash
python pacman.py
Features and Extensions
This is a basic implementation. You can extend it with:
Ghosts that chase Pac-Man.
Pellets to collect for scoring.
A win/lose condition.
Animations for Pac-Man's mouth opening/closing.
Let me know if you'd like help adding any of these features!