Make me a pacman clone
- User
Make me a pacman clone
- Assistant
Make me a pacman clone
Sure! I'd be happy to help you create a simple Pac-Man clone. We'll use Python and the Pygame library, which is great for building 2D games. This will be a basic version to get you started, and you can expand upon it to add more features.
pip install pygame
First, we'll set up the basic game window and main loop.
import pygame
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 600, 700
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Pac-Man Clone')
# Frames per second
FPS = 60
CLOCK = pygame.time.Clock()
We'll create a Player class to represent Pac-Man.
class Player:
def __init__(self, x, y):
self.radius = 15
self.x = x
self.y = y
self.speed = 3
self.direction = 'STOP'
self.color = (255, 255, 0) # Yellow color
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
def move(self, walls):
dx, dy = 0, 0
if self.direction == 'LEFT':
dx = -self.speed
elif self.direction == 'RIGHT':
dx = self.speed
elif self.direction == 'UP':
dy = -self.speed
elif self.direction == 'DOWN':
dy = self.speed
# Check for wall collisions
next_rect = pygame.Rect(self.x + dx - self.radius, self.y + dy - self.radius,
self.radius * 2, self.radius * 2)
if not any(next_rect.colliderect(wall) for wall in walls):
self.x += dx
self.y += dy
We'll define the maze layout using a list of strings.
# Define the maze
maze = [
"XXXXXXXXXXXXXXXXXXXX",
"X.........XX........X",
"X.XXXXXXX.XX.XXXXXX.X",
"X.X.....X......X...X",
"X.X.XX.XXXXXXX.X.X.X",
"X...XX.....X...X.X.X",
"XXX.XXXXXX.X.XXX.X.X",
"X.X........X.....X.X",
"X.X.XXXXXXXXXXXXX.X",
"X.X...............X",
"XXXXXXXXXXXXXXXXXXXX"
]
X represents a wall.. represents an open space where Pac-Man can move.def draw_maze(screen, maze):
walls = []
size = 30 # Size of each block
for row_index, row in enumerate(maze):
for col_index, col in enumerate(row):
if col == 'X':
rect = pygame.Rect(col_index * size, row_index * size, size, size)
walls.append(rect)
pygame.draw.rect(screen, (0, 0, 255), rect) # Blue walls
return walls
We'll place dots (pellets) that Pac-Man can collect.
def create_dots(maze):
dots = []
size = 30
for row_index, row in enumerate(maze):
for col_index, col in enumerate(row):
if col == '.':
x = col_index * size + size // 2
y = row_index * size + size // 2
dots.append((x, y))
return dots
def draw_dots(screen, dots):
for dot in dots:
pygame.draw.circle(screen, (255, 255, 255), dot, 5)
We'll create a basic Enemy class.
class Enemy:
def __init__(self, x, y, color=(255, 0, 0)):
self.rect = pygame.Rect(x - 15, y - 15, 30, 30)
self.speed = 2
self.direction = 'LEFT'
self.color = color
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect)
def move(self, walls):
dx, dy = 0, 0
if self.direction == 'LEFT':
dx = -self.speed
elif self.direction == 'RIGHT':
dx = self.speed
elif self.direction == 'UP':
dy = -self.speed
elif self.direction == 'DOWN':
dy = self.speed
# Check for wall collisions
next_rect = self.rect.move(dx, dy)
if not any(next_rect.colliderect(wall) for wall in walls):
self.rect = next_rect
else:
self.change_direction()
def change_direction(self):
import random
self.direction = random.choice(['LEFT', 'RIGHT', 'UP', 'DOWN'])
Update the player's direction based on key presses.
def handle_input(player):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.direction = 'LEFT'
elif keys[pygame.K_RIGHT]:
player.direction = 'RIGHT'
elif keys[pygame.K_UP]:
player.direction = 'UP'
elif keys[pygame.K_DOWN]:
player.direction = 'DOWN'
Here's the main loop of the game:
def main():
# Initialize game objects
player = Player(WIDTH // 2, HEIGHT // 2 + 100)
dots = create_dots(maze)
enemies = [
Enemy(300, 300, color=(255, 0, 0)),
Enemy(300, 360, color=(0, 255, 255)),
Enemy(360, 300, color=(255, 184, 255)),
Enemy(360, 360, color=(255, 184, 82)),
]
running = True
while running:
CLOCK.tick(FPS)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
handle_input(player)
# Game logic
walls = draw_maze(SCREEN, maze)
player.move(walls)
for enemy in enemies:
enemy.move(walls)
if player.x - player.radius < enemy.rect.right and \
player.x + player.radius > enemy.rect.left and \
player.y - player.radius < enemy.rect.bottom and \
player.y + player.radius > enemy.rect.top:
print("Game Over!")
running = False
# Check for dot collisions
player_rect = pygame.Rect(player.x - player.radius, player.y - player.radius,
player.radius * 2, player.radius * 2)
dots = [dot for dot in dots if not player_rect.collidepoint(dot)]
# Drawing
SCREEN.fill((0, 0, 0)) # Clear screen
draw_maze(SCREEN, maze)
draw_dots(SCREEN, dots)
player.draw(SCREEN)
for enemy in enemies:
enemy.draw(SCREEN)
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()
Here is the complete code for the basic Pac-Man clone:
import pygame
import sys
pygame.init()
WIDTH, HEIGHT = 600, 700
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Pac-Man Clone')
FPS = 60
CLOCK = pygame.time.Clock()
# Define the maze
maze = [
"XXXXXXXXXXXXXXXXXXXX",
"X.........XX........X",
"X.XXXXXXX.XX.XXXXXX.X",
"X.X.....X......X...X",
"X.X.XX.XXXXXXX.X.X.X",
"X...XX.....X...X.X.X",
"XXX.XXXXXX.X.XXX.X.X",
"X.X........X.....X.X",
"X.X.XXXXXXXXXXXXX.X",
"X.X...............X",
"XXXXXXXXXXXXXXXXXXXX"
]
class Player:
# ... (Player class code as above)
class Enemy:
# ... (Enemy class code as above)
# ... (Other functions as above)
def main():
# ... (Main game loop code as above)
if __name__ == '__main__':
main()
Now that you have a basic Pac-Man clone, here are some ideas to extend and enhance the game:
Feel free to ask if you have any questions or need further assistance modifying the game!
Not yet included in a published release.