11
11
Licensed under the MIT license.
12
12
13
13
All text above must be included in any redistribution.
14
-
15
- Requires CircuitPython 5.0 or later.
16
14
"""
17
15
18
16
import time
37
35
38
36
FIRST_CELL = CELL_1
39
37
LAST_CELL = CELL_4
40
-
41
38
NUMBER_OF_CELLS = (LAST_CELL - FIRST_CELL ) + 1
42
39
43
40
# 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 )]
46
42
47
43
# The chance (out of 10) that toast will enter
48
44
CHANCE_OF_NEW_TOAST = 2
49
45
50
- # How many sprites to styart with
46
+ # How many sprites to start with
51
47
INITIAL_NUMBER_OF_SPRITES = 4
52
48
53
- # Global variables
54
- display = None
55
- tilegrid = None
56
-
57
49
seed (int (time .monotonic ()))
58
50
51
+
59
52
def make_display ():
60
53
"""Set up the display support.
61
54
Return the Display object.
62
55
"""
63
56
spi = board .SPI ()
64
57
while not spi .try_lock ():
65
58
pass
66
- spi .configure (baudrate = 24000000 ) # Configure SPI for 24MHz
59
+ spi .configure (baudrate = 24000000 ) # Configure SPI for 24MHz
67
60
spi .unlock ()
68
61
displayio .release_displays ()
69
62
display_bus = displayio .FourWire (spi , command = board .D7 , chip_select = board .D10 , reset = board .D9 )
70
63
71
64
return ST7789 (display_bus , width = 240 , height = 240 , rowstart = 80 , auto_refresh = True )
72
65
66
+
73
67
def make_tilegrid ():
74
68
"""Construct and return the tilegrid."""
75
- group = displayio .Group (max_size = 10 )
69
+ group = displayio .Group ()
76
70
77
71
sprite_sheet , palette = adafruit_imageload .load ("/spritesheet-2x.bmp" ,
78
72
bitmap = displayio .Bitmap ,
@@ -86,16 +80,19 @@ def make_tilegrid():
86
80
display .show (group )
87
81
return grid
88
82
83
+
89
84
def random_cell ():
90
85
return randint (FIRST_CELL , LAST_CELL )
91
86
87
+
92
88
def 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.
94
90
:param row: the tile row (0-9)
95
91
:param col: the tile column (0-9)
96
92
"""
97
93
return tilegrid [col , row ] == EMPTY
98
94
95
+
99
96
def seed_toasters (number_of_toasters ):
100
97
"""Create the initial toasters so it doesn't start empty"""
101
98
for _ in range (number_of_toasters ):
@@ -106,21 +103,25 @@ def seed_toasters(number_of_toasters):
106
103
break
107
104
tilegrid [col , row ] = random_cell ()
108
105
106
+
109
107
def next_sprite (sprite ):
110
108
if ANIMATED [sprite ]:
111
109
return (((sprite - FIRST_CELL ) + 1 ) % NUMBER_OF_CELLS ) + FIRST_CELL
112
110
return sprite
113
111
112
+
114
113
def advance_animation ():
115
114
"""Cycle through animation cells each time."""
116
115
for tile_number in range (25 ):
117
116
tilegrid [tile_number ] = next_sprite (tilegrid [tile_number ])
118
117
118
+
119
119
def slide_tiles ():
120
120
"""Move the tilegrid one pixel to the bottom-left."""
121
121
tilegrid .x -= 1
122
122
tilegrid .y += 1
123
123
124
+
124
125
def shift_tiles ():
125
126
"""Move tiles one spot to the left, and reset the tilegrid's position"""
126
127
for row in range (4 , 0 , - 1 ):
@@ -132,20 +133,23 @@ def shift_tiles():
132
133
tilegrid .x = 0
133
134
tilegrid .y = - 64
134
135
136
+
135
137
def get_entry_row ():
136
138
while True :
137
139
row = randint (0 , 4 )
138
140
if tilegrid [4 , row ] == EMPTY and tilegrid [3 , row ] == EMPTY :
139
141
return row
140
142
143
+
141
144
def get_entry_column ():
142
145
while True :
143
146
col = randint (0 , 3 )
144
147
if tilegrid [col , 0 ] == EMPTY and tilegrid [col , 1 ] == EMPTY :
145
148
return col
146
149
150
+
147
151
def 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"""
149
153
if randint (1 , 10 ) <= CHANCE_OF_NEW_TOAST :
150
154
tile = TOAST
151
155
else :
@@ -158,6 +162,7 @@ def add_toaster_or_toast():
158
162
tile = random_cell ()
159
163
tilegrid [get_entry_column (), 0 ] = tile
160
164
165
+
161
166
display = make_display ()
162
167
tilegrid = make_tilegrid ()
163
168
seed_toasters (INITIAL_NUMBER_OF_SPRITES )
0 commit comments