nice script for python pacman
- User
nice script for python pacman
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
nice script for python pacman
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a simple Python script for a basic Pac-Man game using the pygame library. This script creates a playable Pac-Man with ghosts in a maze. To run this script, you'll need to have pygame installed (pip install pygame).
import pygame
import random
from enum import Enum
# Constants
WIDTH, HEIGHT = 400, 400
CELL_SIZE = 20
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
# Directions
class Direction(Enum):
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pac-Man")
clock = pygame.time.Clock()
# Maze layout (1: wall, 0: path, 2: dot)
maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
# Pac-Man class
class PacMan:
def __init__(self):
self.x = 1
self.y = 1
self.direction = Direction.RIGHT
self.speed = 1
def move(self):
if self.direction == Direction.UP and maze[self.y - 1][self.x] != 1:
self.y -= self.speed
elif self.direction == Direction.DOWN and maze[self.y + 1][self.x] != 1:
self.y += self.speed
elif self.direction == Direction.LEFT and maze[self.y][self.x - 1] != 1:
self.x -= self.speed
elif self.direction == Direction.RIGHT and maze[self.y][self.x + 1] != 1:
self.x += self.speed
def draw(self):
pygame.draw.circle(screen, YELLOW, (int(self.x * CELL_SIZE + CELL_SIZE / 2), int(self.y * CELL_SIZE + CELL_SIZE / 2)), CELL_SIZE // 2)
# Ghost class
class Ghost:
def __init__(self, color, start_x, start_y):
self.x = start_x
self.y = start_y
self.color = color
self.direction = random.choice(list(Direction))
self.speed = 1
def move(self):
if random.randint(0, 100) > 90: # Change direction randomly
self.direction = random.choice(list(Direction))
if self.direction == Direction.UP and maze[self.y - 1][self.x] != 1:
self.y -= self.speed
elif self.direction == Direction.DOWN and maze[self.y + 1][self.x] != 1:
self.y += self.speed
elif self.direction == Direction.LEFT and maze[self.y][self.x - 1] != 1:
self.x -= self.speed
elif self.direction == Direction.RIGHT and maze[self.y][self.x + 1] != 1:
self.x += self.speed
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x * CELL_SIZE + CELL_SIZE / 2), int(self.y * CELL_SIZE + CELL_SIZE / 2)), CELL_SIZE // 2)
# Initialize Pac-Man and Ghosts
pacman = PacMan()
ghosts = [Ghost(RED, 8, 8), Ghost(BLUE, 9, 9)]
# Game loop
running = True
while running:
screen.fill(BLACK)
# Draw maze
for y in range(len(maze)):
for x in range(len(maze[y])):
if maze[y][x] == 1:
pygame.draw.rect(screen, BLUE, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
elif maze[y][x] == 2:
pygame.draw.circle(screen, WHITE, (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2), CELL_SIZE // 4)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
pacman.direction = Direction.UP
elif event.key == pygame.K_DOWN:
pacman.direction = Direction.DOWN
elif event.key == pygame.K_LEFT:
pacman.direction = Direction.LEFT
elif event.key == pygame.K_RIGHT:
pacman.direction = Direction.RIGHT
# Move Pac-Man and Ghosts
pacman.move()
for ghost in ghosts:
ghost.move()
# Draw Pac-Man and Ghosts
pacman.draw()
for ghost in ghosts:
ghost.draw()
# Check for collisions
if any(abs(pacman.x - ghost.x) < 1 and abs(pacman.y - ghost.y) < 1 for ghost in ghosts):
print("Game Over!")
running = False
pygame.display.flip()
clock.tick(10)
pygame.quit()
pygame: pip install pygame.py file and run it.This is a basic implementation and can be expanded with features like dots, power-ups, and more complex AI for ghosts.