1111Licensed under the MIT license. 
1212
1313All text above must be included in any redistribution. 
14- 
15- Requires CircuitPython 5.0 or later. 
1614""" 
1715
1816import  time 
3735
3836FIRST_CELL  =  CELL_1 
3937LAST_CELL  =  CELL_4 
40- 
4138NUMBER_OF_CELLS  =  (LAST_CELL  -  FIRST_CELL ) +  1 
4239
4340# A boolean array corresponding to the sprites, True if it's part of the animation sequence. 
44- ANIMATED  =  [_sprite  >=  FIRST_CELL  and  _sprite  <=  LAST_CELL  for  _sprite  in  range (NUMBER_OF_SPRITES )]
45- 
41+ ANIMATED  =  [FIRST_CELL  <=  _sprite  <=  LAST_CELL  for  _sprite  in  range (NUMBER_OF_SPRITES )]
4642
4743# The chance (out of 10) that toast will enter 
4844CHANCE_OF_NEW_TOAST  =  2 
4945
50- # How many sprites to styart  with 
46+ # How many sprites to start  with 
5147INITIAL_NUMBER_OF_SPRITES  =  4 
5248
53- # Global variables 
54- display  =  None 
55- tilegrid  =  None 
56- 
5749seed (int (time .monotonic ()))
5850
51+ 
5952def  make_display ():
6053    """Set up the display support. 
6154    Return the Display object. 
6255    """ 
6356    spi  =  board .SPI ()
6457    while  not  spi .try_lock ():
6558        pass 
66-     spi .configure (baudrate = 24000000 ) # Configure SPI for 24MHz 
59+     spi .configure (baudrate = 24000000 )   # Configure SPI for 24MHz 
6760    spi .unlock ()
6861    displayio .release_displays ()
6962    display_bus  =  displayio .FourWire (spi , command = board .D7 , chip_select = board .D10 , reset = board .D9 )
7063
7164    return  ST7789 (display_bus , width = 240 , height = 240 , rowstart = 80 , auto_refresh = True )
7265
66+ 
7367def  make_tilegrid ():
7468    """Construct and return the tilegrid.""" 
75-     group  =  displayio .Group (max_size = 10 )
69+     group  =  displayio .Group ()
7670
7771    sprite_sheet , palette  =  adafruit_imageload .load ("/spritesheet-2x.bmp" ,
7872                                                    bitmap = displayio .Bitmap ,
@@ -86,16 +80,19 @@ def make_tilegrid():
8680    display .show (group )
8781    return  grid 
8882
83+ 
8984def  random_cell ():
9085    return  randint (FIRST_CELL , LAST_CELL )
9186
87+ 
9288def  evaluate_position (row , col ):
93-     """Return whether how long of aa  toaster is placable  at the given location. 
89+     """Return whether how long of a  toaster is placeable  at the given location. 
9490    :param row: the tile row (0-9) 
9591    :param col: the tile column (0-9) 
9692    """ 
9793    return  tilegrid [col , row ] ==  EMPTY 
9894
95+ 
9996def  seed_toasters (number_of_toasters ):
10097    """Create the initial toasters so it doesn't start empty""" 
10198    for  _  in  range (number_of_toasters ):
@@ -106,21 +103,25 @@ def seed_toasters(number_of_toasters):
106103                break 
107104        tilegrid [col , row ] =  random_cell ()
108105
106+ 
109107def  next_sprite (sprite ):
110108    if  ANIMATED [sprite ]:
111109        return  (((sprite  -  FIRST_CELL ) +  1 ) %  NUMBER_OF_CELLS ) +  FIRST_CELL 
112110    return  sprite 
113111
112+ 
114113def  advance_animation ():
115114    """Cycle through animation cells each time.""" 
116115    for  tile_number  in  range (25 ):
117116        tilegrid [tile_number ] =  next_sprite (tilegrid [tile_number ])
118117
118+ 
119119def  slide_tiles ():
120120    """Move the tilegrid one pixel to the bottom-left.""" 
121121    tilegrid .x  -=  1 
122122    tilegrid .y  +=  1 
123123
124+ 
124125def  shift_tiles ():
125126    """Move tiles one spot to the left, and reset the tilegrid's position""" 
126127    for  row  in  range (4 , 0 , - 1 ):
@@ -132,20 +133,23 @@ def shift_tiles():
132133    tilegrid .x  =  0 
133134    tilegrid .y  =  - 64 
134135
136+ 
135137def  get_entry_row ():
136138    while  True :
137139        row  =  randint (0 , 4 )
138140        if  tilegrid [4 , row ] ==  EMPTY  and  tilegrid [3 , row ] ==  EMPTY :
139141            return  row 
140142
143+ 
141144def  get_entry_column ():
142145    while  True :
143146        col  =  randint (0 , 3 )
144147        if  tilegrid [col , 0 ] ==  EMPTY  and  tilegrid [col , 1 ] ==  EMPTY :
145148            return  col 
146149
150+ 
147151def  add_toaster_or_toast ():
148-     """Maybe add a new toaster or toast on the right and/or top at a randon  open location""" 
152+     """Maybe add a new toaster or toast on the right and/or top at a random  open location""" 
149153    if  randint (1 , 10 ) <=  CHANCE_OF_NEW_TOAST :
150154        tile  =  TOAST 
151155    else :
@@ -158,6 +162,7 @@ def add_toaster_or_toast():
158162        tile  =  random_cell ()
159163    tilegrid [get_entry_column (), 0 ] =  tile 
160164
165+ 
161166display  =  make_display ()
162167tilegrid  =  make_tilegrid ()
163168seed_toasters (INITIAL_NUMBER_OF_SPRITES )
0 commit comments