Skip to content

Commit 86822cb

Browse files
authored
Merge pull request #1614 from lesamouraipourpre/flying-toasters-mario-clouds
Flying_Toasters + Scrolling_Clouds: Update for CP7
2 parents b79095e + 0c99576 commit 86822cb

File tree

2 files changed

+33
-23
lines changed
  • CircuitPython_Flying_Toasters
  • CircuitPython_Scrolling_Clouds

2 files changed

+33
-23
lines changed

CircuitPython_Flying_Toasters/code.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
Licensed under the MIT license.
1212
1313
All text above must be included in any redistribution.
14-
15-
Requires CircuitPython 5.0 or later.
1614
"""
1715

1816
import time
@@ -37,42 +35,38 @@
3735

3836
FIRST_CELL = CELL_1
3937
LAST_CELL = CELL_4
40-
4138
NUMBER_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
4844
CHANCE_OF_NEW_TOAST = 2
4945

50-
# How many sprites to styart with
46+
# How many sprites to start with
5147
INITIAL_NUMBER_OF_SPRITES = 4
5248

53-
# Global variables
54-
display = None
55-
tilegrid = None
56-
5749
seed(int(time.monotonic()))
5850

51+
5952
def 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+
7367
def 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+
8984
def random_cell():
9085
return randint(FIRST_CELL, LAST_CELL)
9186

87+
9288
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.
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+
9996
def 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+
109107
def next_sprite(sprite):
110108
if ANIMATED[sprite]:
111109
return (((sprite - FIRST_CELL) + 1) % NUMBER_OF_CELLS) + FIRST_CELL
112110
return sprite
113111

112+
114113
def 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+
119119
def slide_tiles():
120120
"""Move the tilegrid one pixel to the bottom-left."""
121121
tilegrid.x -= 1
122122
tilegrid.y += 1
123123

124+
124125
def 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+
135137
def 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+
141144
def 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+
147151
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"""
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+
161166
display = make_display()
162167
tilegrid = make_tilegrid()
163168
seed_toasters(INITIAL_NUMBER_OF_SPRITES)

CircuitPython_Scrolling_Clouds/code.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Continuously scroll randomly generated Mario style clouds.
3-
Designed fr an ItsyBitsy M4 Express and a 1.3" 240x240 TFT
3+
Designed for an ItsyBitsy M4 Express and a 1.3" 240x240 TFT
44
55
Adafruit invests time and resources providing this open source code.
66
Please support Adafruit and open source hardware by purchasing
@@ -20,6 +20,7 @@
2020
from adafruit_st7789 import ST7789
2121
import adafruit_imageload
2222

23+
2324
# Sprite cell values
2425
EMPTY = 0
2526
LEFT = 1
@@ -35,30 +36,28 @@
3536
# The chance an existing cloud gets extended
3637
CHANCE_OF_EXTENDING_A_CLOUD = 5
3738

38-
# Global variables
39-
display = None
40-
tilegrid = None
41-
4239
seed(int(time.monotonic()))
4340

41+
4442
def make_display():
4543
"""Set up the display support.
4644
Return the Display object.
4745
"""
4846
spi = board.SPI()
4947
while not spi.try_lock():
5048
pass
51-
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
49+
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
5250
spi.unlock()
5351

5452
displayio.release_displays()
5553
display_bus = displayio.FourWire(spi, command=board.D7, chip_select=board.D10, reset=board.D9)
5654

5755
return ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=True)
5856

57+
5958
def make_tilegrid():
6059
"""Construct and return the tilegrid."""
61-
group = displayio.Group(max_size=10)
60+
group = displayio.Group()
6261

6362
sprite_sheet, palette = adafruit_imageload.load("/tilesheet-2x.bmp",
6463
bitmap=displayio.Bitmap,
@@ -71,8 +70,9 @@ def make_tilegrid():
7170
display.show(group)
7271
return grid
7372

73+
7474
def evaluate_position(row, col):
75-
"""Return how long of a cloud is placable at the given location.
75+
"""Return how long of a cloud is placeable at the given location.
7676
:param row: the tile row (0-4)
7777
:param col: the tile column (0-8)
7878
"""
@@ -83,6 +83,7 @@ def evaluate_position(row, col):
8383
end_col += 1
8484
return min([4, end_col - col])
8585

86+
8687
def seed_clouds(number_of_clouds):
8788
"""Create the initial clouds so it doesn't start empty"""
8889
for _ in range(number_of_clouds):
@@ -99,6 +100,7 @@ def seed_clouds(number_of_clouds):
99100
tilegrid[col, row] = MIDDLE
100101
tilegrid[col + 1, row] = RIGHT
101102

103+
102104
def slide_tiles():
103105
"""Move the tilegrid to the left, one pixel at a time, a full time width"""
104106
for _ in range(32):
@@ -114,6 +116,7 @@ def shift_tiles():
114116
tilegrid[8, row] = EMPTY
115117
tilegrid.x = 0
116118

119+
117120
def extend_clouds():
118121
"""Extend any clouds on the right edge, either finishing them with a right
119122
end or continuing them with a middle piece
@@ -125,8 +128,9 @@ def extend_clouds():
125128
else:
126129
tilegrid[8, row] = RIGHT
127130

131+
128132
def add_cloud():
129-
"""Maybe add a new cloud on the right at a randon open row"""
133+
"""Maybe add a new cloud on the right at a random open row"""
130134
if randint(1, 10) > CHANCE_OF_NEW_CLOUD:
131135
count = 0
132136
while True:
@@ -138,6 +142,7 @@ def add_cloud():
138142
break
139143
tilegrid[8, row] = LEFT
140144

145+
141146
display = make_display()
142147
tilegrid = make_tilegrid()
143148
seed_clouds(5)

0 commit comments

Comments
 (0)