44"""
55SnakeAnimation helper class
66"""
7+ import random
78from micropython import const
89
910from adafruit_led_animation .animation import Animation
1011from adafruit_led_animation .grid import PixelGrid , HORIZONTAL
11- import random
12+
1213
1314
1415class SnakeAnimation (Animation ):
@@ -34,7 +35,8 @@ def __init__(self, pixel_object, speed, color, width, height, snake_length=3):
3435 self .snake_length = snake_length
3536
3637 # 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+ self .pixel_grid = PixelGrid (pixel_object , width , height ,
39+ orientation = HORIZONTAL , alternating = False )
3840
3941 # size variables
4042 self .width = width
@@ -43,6 +45,8 @@ def __init__(self, pixel_object, speed, color, width, height, snake_length=3):
4345 # list that will hold locations of snake segments
4446 self .snake_pixels = []
4547
48+ self .direction = None
49+
4650 # initialize the snake
4751 self ._new_snake ()
4852
@@ -75,7 +79,8 @@ def _can_move(self, direction):
7579 returns true if the snake can move in the given direction
7680 """
7781 # 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 ])))
82+ next_tile = tuple (map (sum , zip (
83+ SnakeAnimation .DIRECTION_OFFSETS [direction ], self .snake_pixels [0 ])))
7984
8085 # if the tile is one of the snake segments
8186 if next_tile in self .snake_pixels :
@@ -111,7 +116,8 @@ def _choose_direction(self):
111116 # loop over the copied list of directions to check
112117 while len (directions_to_check ) > 0 :
113118 # 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 ))
119+ possible_direction = directions_to_check .pop (
120+ random .randint (0 , len (directions_to_check )- 1 ))
115121 # if we can move the chosen direction
116122 if self ._can_move (possible_direction ):
117123 # return the chosen direction
@@ -140,7 +146,8 @@ def draw(self):
140146 self .direction = self ._choose_direction ()
141147
142148 # 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 ])))
149+ next_tile = tuple (map (sum , zip (
150+ SnakeAnimation .DIRECTION_OFFSETS [self .direction ], self .snake_pixels [0 ])))
144151
145152 # insert the next tile at list index 0
146153 self .snake_pixels .insert (0 , next_tile )
0 commit comments