|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +SnakeAnimation helper class |
| 6 | +""" |
| 7 | +from micropython import const |
| 8 | + |
| 9 | +from adafruit_led_animation.animation import Animation |
| 10 | +from adafruit_led_animation.grid import PixelGrid, HORIZONTAL |
| 11 | +import random |
| 12 | + |
| 13 | + |
| 14 | +class SnakeAnimation(Animation): |
| 15 | + UP = const(0x00) |
| 16 | + DOWN = const(0x01) |
| 17 | + LEFT = const(0x02) |
| 18 | + RIGHT = const(0x03) |
| 19 | + ALL_DIRECTIONS = [UP, DOWN, LEFT, RIGHT] |
| 20 | + DIRECTION_OFFSETS = { |
| 21 | + DOWN: (0, 1), |
| 22 | + UP: (0, -1), |
| 23 | + RIGHT: (1, 0), |
| 24 | + LEFT: (-1, 0) |
| 25 | + } |
| 26 | + |
| 27 | + def __init__(self, pixel_object, speed, color, width, height, snake_length=3): |
| 28 | + """ |
| 29 | + Renders a snake that slithers around the 2D grid of pixels |
| 30 | + """ |
| 31 | + super().__init__(pixel_object, speed, color) |
| 32 | + |
| 33 | + # how many segments the snake will have |
| 34 | + self.snake_length = snake_length |
| 35 | + |
| 36 | + # create a PixelGrid helper to access our strand as a 2D grid |
| 37 | + self.pixel_grid = PixelGrid(pixel_object, width, height, orientation=HORIZONTAL, alternating=False) |
| 38 | + |
| 39 | + # size variables |
| 40 | + self.width = width |
| 41 | + self.height = height |
| 42 | + |
| 43 | + # list that will hold locations of snake segments |
| 44 | + self.snake_pixels = [] |
| 45 | + |
| 46 | + # initialize the snake |
| 47 | + self._new_snake() |
| 48 | + |
| 49 | + def _clear_snake(self): |
| 50 | + """ |
| 51 | + Clear the snake segments and turn off all pixels |
| 52 | + """ |
| 53 | + while len(self.snake_pixels) > 0: |
| 54 | + self.pixel_grid[self.snake_pixels.pop()] = 0x000000 |
| 55 | + |
| 56 | + def _new_snake(self): |
| 57 | + """ |
| 58 | + Create a new single segment snake. The snake has a random |
| 59 | + direction and location. Turn on the pixel representing the snake. |
| 60 | + """ |
| 61 | + # choose a random direction and store it |
| 62 | + self.direction = random.choice(SnakeAnimation.ALL_DIRECTIONS) |
| 63 | + |
| 64 | + # choose a random starting tile |
| 65 | + starting_tile = (random.randint(0, self.width - 1), random.randint(0, self.height - 1)) |
| 66 | + |
| 67 | + # add the starting tile to the list of segments |
| 68 | + self.snake_pixels.append(starting_tile) |
| 69 | + |
| 70 | + # turn on the pixel at the chosen location |
| 71 | + self.pixel_grid[self.snake_pixels[0]] = self.color |
| 72 | + |
| 73 | + def _can_move(self, direction): |
| 74 | + """ |
| 75 | + returns true if the snake can move in the given direction |
| 76 | + """ |
| 77 | + # location of the next tile if we would move that direction |
| 78 | + next_tile = tuple(map(sum, zip(SnakeAnimation.DIRECTION_OFFSETS[direction], self.snake_pixels[0]))) |
| 79 | + |
| 80 | + # if the tile is one of the snake segments |
| 81 | + if next_tile in self.snake_pixels: |
| 82 | + # can't move there |
| 83 | + return False |
| 84 | + |
| 85 | + # if the tile is within the bounds of the grid |
| 86 | + if 0 <= next_tile[0] < self.width and 0 <= next_tile[1] < self.height: |
| 87 | + # can move there |
| 88 | + return True |
| 89 | + |
| 90 | + # return false if any other conditions not met |
| 91 | + return False |
| 92 | + |
| 93 | + |
| 94 | + def _choose_direction(self): |
| 95 | + """ |
| 96 | + Choose a direction to go in. Could continue in same direction |
| 97 | + as it's already going, or decide to turn to a dirction that |
| 98 | + will allow movement. |
| 99 | + """ |
| 100 | + |
| 101 | + # copy of all directions in a list |
| 102 | + directions_to_check = list(SnakeAnimation.ALL_DIRECTIONS) |
| 103 | + |
| 104 | + # if we can move the direction we're currently going |
| 105 | + if self._can_move(self.direction): |
| 106 | + # "flip a coin" |
| 107 | + if random.random() < 0.5: |
| 108 | + # on "heads" we stay going the same direction |
| 109 | + return self.direction |
| 110 | + |
| 111 | + # loop over the copied list of directions to check |
| 112 | + while len(directions_to_check) > 0: |
| 113 | + # choose a random one from the list and pop it out of the list |
| 114 | + possible_direction = directions_to_check.pop(random.randint(0, len(directions_to_check)-1)) |
| 115 | + # if we can move the chosen direction |
| 116 | + if self._can_move(possible_direction): |
| 117 | + # return the chosen direction |
| 118 | + return possible_direction |
| 119 | + |
| 120 | + # if we made it through all directions and couldn't move in any of them |
| 121 | + # then raise the SnakeStuckException |
| 122 | + raise SnakeAnimation.SnakeStuckException |
| 123 | + |
| 124 | + |
| 125 | + def draw(self): |
| 126 | + """ |
| 127 | + Draw the current frame of the animation |
| 128 | + """ |
| 129 | + # if the snake is currently the desired length |
| 130 | + if len(self.snake_pixels) == self.snake_length: |
| 131 | + # remove the last segment from the list and turn it's LED off |
| 132 | + self.pixel_grid[self.snake_pixels.pop()] = 0x000000 |
| 133 | + |
| 134 | + # if the snake is less than the desired length |
| 135 | + # e.g. because we removed one in the previous step |
| 136 | + if len(self.snake_pixels) < self.snake_length: |
| 137 | + # wrap with try to catch the SnakeStuckException |
| 138 | + try: |
| 139 | + # update the direction, could continue straight, or could change |
| 140 | + self.direction = self._choose_direction() |
| 141 | + |
| 142 | + # the location of the next tile where the head of the snake will move to |
| 143 | + next_tile = tuple(map(sum, zip(SnakeAnimation.DIRECTION_OFFSETS[self.direction], self.snake_pixels[0]))) |
| 144 | + |
| 145 | + # insert the next tile at list index 0 |
| 146 | + self.snake_pixels.insert(0, next_tile) |
| 147 | + |
| 148 | + # turn on the LED for the tile |
| 149 | + self.pixel_grid[next_tile] = self.color |
| 150 | + |
| 151 | + # if the snake exception is caught |
| 152 | + except SnakeAnimation.SnakeStuckException: |
| 153 | + # clear the snake to get rid of the old one |
| 154 | + self._clear_snake() |
| 155 | + |
| 156 | + # make a new snake |
| 157 | + self._new_snake() |
| 158 | + |
| 159 | + class SnakeStuckException(RuntimeError): |
| 160 | + """ |
| 161 | + Exception indicating the snake is stuck and can't move in any direction |
| 162 | + """ |
| 163 | + def __init__(self): |
| 164 | + super().__init__("SnakeStuckException") |
0 commit comments