|
| 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] |
0 commit comments