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.
Here’s a compact, self-contained “text-mode” Pac-Man clone written in pure Python 3 with the standard library only (no external packages).
Copy it into a file called pacman.py, run with python pacman.py, and play with the arrow keys. Press q to quit.
#!/usr/bin/env python3
"""
Minimal ASCII Pac-Man in one file.
Python 3, no external dependencies.
"""
import os
import sys
import tty
import termios
import time
import random
from collections import deque
# --------------------------- CONFIG ---------------------------------
WIDTH, HEIGHT = 19, 11
GHOSTS = 2
DOTS = 20
SPEED = 0.15
# --------------------------------------------------------------------
# Simple maze: 1 = wall, 0 = empty
MAZE = [
"###################",
"#........#........#",
"#.##.###.#.###.##.#",
"#.................#",
"#.##.#.#####.#.##.#",
"#....#...#...#....#",
"####.###.#.###.####",
" #.#.......#.# ",
" #.#.## ##.#.# ",
" #....P ....# ",
" ############ ",
]
DIRS = [(0, -1), (0, 1), (-1, 0), (1, 0)] # ← → ↑ ↓
KEYMAP = {
"\x1b[A": (0, -1), # up
"\x1b[B": (0, 1), # down
"\x1b[C": (1, 0), # right
"\x1b[D": (-1, 0), # left
"q": "quit"
}
class Game:
def __init__(self):
self.maze = [list(row) for row in MAZE]
self.px, self.py = 9, 9 # start position of Pac-Man
self.maze[self.py][self.px] = " "
self.ghosts = []
self.dots = set()
self.score = 0
self._place_dots()
self._spawn_ghosts()
def _place_dots(self):
empties = [(x, y) for y in range(HEIGHT) for x in range(WIDTH)
if self.maze[y][x] == "."]
self.dots = set(random.sample(empties, min(DOTS, len(empties))))
def _spawn_ghosts(self):
empties = [(x, y) for y in range(HEIGHT) for x in range(WIDTH)
if self.maze[y][x] == " " and (x, y) != (self.px, self.py)]
for _ in range(GHOSTS):
x, y = random.choice(empties)
self.ghosts.append([x, y, random.choice(DIRS)])
def draw(self):
os.system("clear" if os.name != "nt" else "cls")
for y in range(HEIGHT):
line = []
for x in range(WIDTH):
ch = self.maze[y][x]
if (x, y) == (self.px, self.py):
ch = "C"
elif (x, y) in [(g[0], g[1]) for g in self.ghosts]:
ch = "G"
elif (x, y) in self.dots:
ch = "."
line.append(ch)
print("".join(line))
print(f"Score: {self.score} (q to quit)")
def move_pacman(self, dx, dy):
nx, ny = self.px + dx, self.py + dy
if 0 <= nx < WIDTH and 0 <= ny < HEIGHT and self.maze[ny][nx] != "#":
self.px, self.py = nx, ny
if (nx, ny) in self.dots:
self.dots.remove((nx, ny))
self.score += 10
def move_ghosts(self):
for g in self.ghosts:
x, y, (dx, dy) = g
opts = [(dx, dy)] + [(a, b) for a, b in DIRS if (a, b) != (-dx, -dy)]
random.shuffle(opts)
for ndx, ndy in opts:
nx, ny = x + ndx, y + ndy
if 0 <= nx < WIDTH and 0 <= ny < HEIGHT and self.maze[ny][nx] != "#":
g[0], g[1], g[2] = nx, ny, (ndx, ndy)
break
def check_death(self):
return any((g[0], g[1]) == (self.px, self.py) for g in self.ghosts)
def run(self):
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
while True:
self.draw()
if self.check_death():
print("GAME OVER")
break
if not self.dots:
print("YOU WIN!")
break
# Non-blocking key read
key = ""
while True:
ch = sys.stdin.read(1)
key += ch
if ch == "q" or key in KEYMAP:
break
if key == "q":
break
if key in KEYMAP and KEYMAP[key] != "quit":
self.move_pacman(*KEYMAP[key])
self.move_ghosts()
time.sleep(SPEED)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
if __name__ == "__main__":
Game().run()
How it works (quick notes)
#, empty walkable cells are . or space.C, ghosts are G, dots are ..\x1b[A etc.); we map them to deltas.tty.setcbreak gives us single-character input without pressing Enter.os.system("clear") clears the screen each frame for a flicker-free ASCII animation.Enjoy hacking on it—add power pellets, smarter ghost AI, levels, sound, etc.