Skip to content

Commit 964b8da

Browse files
authored
Merge pull request #125 from jposada202020/PacMan_animation
PacMan animation
2 parents 37800fe + 78a7418 commit 964b8da

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# SPDX-FileCopyrightText: 2025 Bob Loeffler
2+
# SPDX-FileCopyrightText: 2025 Jose D. Montoya
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
"""
7+
`adafruit_led_animation.animation.pacman`
8+
================================================================================
9+
10+
PacMan Animation for CircuitPython helper library for LED animations.
11+
PACMAN ANIMATION Adapted from https://github.com/wled-dev/WLED/pull/4536 # by BobLoeffler68
12+
13+
* Author(s): Bob Loeffler, Jose D. Montoya
14+
15+
Implementation Notes
16+
--------------------
17+
18+
**Software and Dependencies:**
19+
20+
* Adafruit CircuitPython firmware for the supported boards:
21+
https://circuitpython.org/downloads
22+
23+
24+
"""
25+
26+
import time
27+
from adafruit_led_animation.animation import Animation
28+
from adafruit_led_animation.color import (
29+
BLACK,
30+
YELLOW,
31+
RED,
32+
PURPLE,
33+
CYAN,
34+
ORANGE,
35+
BLUE,
36+
WHITE,
37+
)
38+
39+
ORANGEYELLOW = (255, 136, 0)
40+
41+
42+
class Pacman(Animation):
43+
"""
44+
Simulate the Pacman game in a single led strip.
45+
46+
:param pixel_object: The initialised LED object.
47+
:param float speed: Animation speed rate in seconds, e.g. ``0.1``.
48+
"""
49+
50+
# pylint: disable=too-many-arguments, too-many-branches
51+
def __init__(
52+
self,
53+
pixel_object,
54+
speed,
55+
color=WHITE,
56+
name=None,
57+
):
58+
self.num_leds = len(pixel_object)
59+
self.pacman = [YELLOW, 10]
60+
self.ghosts_original = [[RED, 6], [PURPLE, 4], [CYAN, 2], [ORANGE, 0]]
61+
self.ghosts = [[RED, 6], [PURPLE, 4], [CYAN, 2], [ORANGE, 0]]
62+
self.direction = 1
63+
self.black_dir = -1
64+
self.flag = "beep"
65+
self.power_pellet = [ORANGEYELLOW, self.num_leds]
66+
self.ghost_timer = time.monotonic()
67+
if self.num_leds > 150:
68+
self.start_blinking_ghosts = self.num_leds // 4
69+
else:
70+
self.start_blinking_ghosts = self.num_leds // 3
71+
72+
super().__init__(pixel_object, speed, color, name=name)
73+
74+
on_cycle_complete_supported = True
75+
76+
def draw(self):
77+
"""
78+
Draw the Pacman animation.
79+
:param led_object: led object
80+
:param neopixel_list: list of neopixel colors
81+
:param int num_leds: number of leds.
82+
:param int duration: duration in seconds. Default is 15 seconds
83+
"""
84+
pixel_list = self.pixel_object
85+
pixel_list[-1] = self.power_pellet[0]
86+
87+
delta = time.monotonic() - self.ghost_timer
88+
if delta > 1:
89+
if self.power_pellet[0] == ORANGEYELLOW:
90+
self.power_pellet[0] = BLACK
91+
else:
92+
self.power_pellet[0] = ORANGEYELLOW
93+
pixel_list[self.power_pellet[1] - 1] = self.power_pellet[0]
94+
95+
self.ghost_timer = time.monotonic()
96+
97+
if self.pacman[1] >= self.num_leds - 2:
98+
self.direction = self.direction * -1
99+
self.black_dir = self.black_dir * -1
100+
for ghost in self.ghosts:
101+
ghost[0] = BLUE
102+
103+
pixel_list[self.pacman[1]] = self.pacman[0]
104+
pixel_list[self.pacman[1] + self.black_dir] = BLACK
105+
self.pacman[1] += self.direction
106+
107+
if self.ghosts[3][1] <= self.start_blinking_ghosts and self.direction == -1:
108+
if self.flag == "beep":
109+
for i, ghost in enumerate(self.ghosts):
110+
ghost[0] = BLACK
111+
self.flag = "bop"
112+
else:
113+
for i, ghost in enumerate(self.ghosts):
114+
ghost[0] = self.ghosts_original[i][0]
115+
self.flag = "beep"
116+
117+
for i, ghost in enumerate(self.ghosts):
118+
pixel_list[ghost[1]] = ghost[0]
119+
pixel_list[ghost[1] + self.black_dir] = BLACK
120+
ghost[1] += self.direction
121+
122+
if self.ghosts[3][1] <= 0:
123+
self.direction = self.direction * -1
124+
self.black_dir = self.black_dir * -1
125+
for i, ghost in enumerate(self.ghosts):
126+
ghost[0] = self.ghosts_original[i][0]

docs/examples.rst

+9
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ Demonstrates the sparkle animations.
104104
.. literalinclude:: ../examples/led_animation_sparkle_animations.py
105105
:caption: examples/led_animation_sparkle_animations.py
106106
:linenos:
107+
108+
Pacman
109+
------
110+
111+
Demonstrates the pacman animation.
112+
113+
.. literalinclude:: ../examples/led_animation_pacman.py
114+
:caption: examples/led_animation_pacman.py
115+
:linenos:

examples/led_animation_pacman.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2025 Jose D. Montoya
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
This example animates a Pacman on a NeoPixel strip.
6+
"""
7+
import board
8+
import neopixel
9+
from adafruit_led_animation.animation.pacman import Pacman
10+
from adafruit_led_animation.color import WHITE
11+
12+
# Update to match the pin connected to your NeoPixels
13+
pixel_pin = board.D6
14+
# Update to match the number of NeoPixels you have connected
15+
num_pixels = 50
16+
17+
# Create the NeoPixel object
18+
ORDER = neopixel.GRB
19+
pixels = neopixel.NeoPixel(
20+
pixel_pin,
21+
num_pixels,
22+
brightness=0.5,
23+
auto_write=False,
24+
pixel_order=ORDER,
25+
)
26+
27+
# Create the Pacman animation object
28+
pacman = Pacman(pixels, speed=0.1, color=WHITE)
29+
30+
# Main loop
31+
while True:
32+
pacman.animate()

0 commit comments

Comments
 (0)