-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.py
77 lines (68 loc) · 1.92 KB
/
pacman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# ========================================================================
# pacman.py
#
# Description: Pac-Man chomping on a dot.
#
# Author: Jim Ing
# Date: 2024-08-25
# ========================================================================
from time import sleep
from config import sense
# Define the colors
Y = (255, 255, 0) # Yellow
F = (153, 153, 0) # Dark Faded Yellow
D = (51, 51, 0) # Obscure Weak Yellow
W = (255, 255, 255) # White
K = (0, 0, 0) # Black
# Pac-Man with Mouth Open (facing right)
pacman_open = [
K, D, F, Y, Y, Y, K, K,
D, F, Y, Y, Y, F, K, K,
F, Y, Y, Y, D, K, K, K,
Y, Y, F, D, K, K, K, W,
Y, Y, F, D, K, K, K, W,
F, Y, Y, Y, D, K, K, K,
D, F, Y, Y, Y, F, K, K,
K, D, F, Y, Y, Y, K, K
]
# Pac-Man with Mouth Half-Open (facing right)
pacman_half_open = [
K, D, F, Y, Y, Y, F, D,
D, F, Y, Y, Y, Y, Y, F,
F, Y, Y, Y, Y, Y, Y, F,
Y, Y, Y, Y, Y, F, D, K,
Y, Y, Y, F, D, K, K, W,
Y, Y, Y, Y, Y, F, D, K,
D, F, Y, Y, Y, Y, Y, F,
K, D, F, Y, Y, Y, F, D,
]
# Pac-Man Closed (facing right)
pacman_closed = [
K, D, F, Y, Y, F, D, K,
D, Y, Y, Y, Y, Y, Y, D,
F, Y, Y, Y, Y, Y, Y, F,
Y, Y, Y, Y, Y, Y, Y, Y,
Y, Y, Y, Y, Y, Y, Y, Y,
F, Y, Y, Y, Y, Y, Y, F,
D, Y, Y, Y, Y, Y, Y, D,
K, D, F, Y, Y, F, D, K,
]
# Function to shift the frame to the left
def shift_left(frame):
return frame[1:] + [K] # Shift everything left and add black at the end
# Function to animate Pac-Man
def animate_pacman():
frames = [pacman_open, pacman_half_open, pacman_closed, pacman_half_open]
for _ in range(8): # Move Pac-Man across the screen
for frame in frames:
sense.set_pixels(frame)
sleep(0.2)
frame = shift_left(frame)
# Run the animation
try:
print ("To quit, press Ctrl+C")
while True:
animate_pacman()
except KeyboardInterrupt:
sense.clear()