Skip to content

Commit 36220d6

Browse files
authored
Merge pull request #921 from adafruit/scrolling-clouds-update
Scrolling clouds update
2 parents 4b2568e + b627521 commit 36220d6

File tree

5 files changed

+49
-53
lines changed

5 files changed

+49
-53
lines changed

CircuitPython_Flying_Toasters/code.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@
4444
ANIMATED = [_sprite >= FIRST_CELL and _sprite <= LAST_CELL for _sprite in range(NUMBER_OF_SPRITES)]
4545

4646

47-
# The chance (out of 10) that a new toaster, or toast will enter
48-
CHANCE_OF_NEW_TOASTER = 5
47+
# The chance (out of 10) that toast will enter
4948
CHANCE_OF_NEW_TOAST = 2
5049

5150
# How many sprites to styart with
52-
INITIAL_NUMBER_OF_SPRITES= 5
51+
INITIAL_NUMBER_OF_SPRITES = 4
5352

5453
# Global variables
5554
display = None
@@ -66,25 +65,22 @@ def make_display():
6665
pass
6766
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
6867
spi.unlock()
69-
tft_cs = board.D10
70-
tft_dc = board.D7
71-
7268
displayio.release_displays()
73-
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)
69+
display_bus = displayio.FourWire(spi, command=board.D7, chip_select=board.D10, reset=board.D9)
7470

7571
return ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=True)
7672

7773
def make_tilegrid():
7874
"""Construct and return the tilegrid."""
7975
group = displayio.Group(max_size=10)
8076

81-
sprite_sheet, palette = adafruit_imageload.load("/spritesheet.bmp",
77+
sprite_sheet, palette = adafruit_imageload.load("/spritesheet-2x.bmp",
8278
bitmap=displayio.Bitmap,
8379
palette=displayio.Palette)
8480
grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
85-
width=9, height=9,
86-
tile_height=32, tile_width=32,
87-
x=0, y=-32,
81+
width=5, height=5,
82+
tile_height=64, tile_width=64,
83+
x=0, y=-64,
8884
default_tile=EMPTY)
8985
group.append(grid)
9086
display.show(group)
@@ -104,8 +100,8 @@ def seed_toasters(number_of_toasters):
104100
"""Create the initial toasters so it doesn't start empty"""
105101
for _ in range(number_of_toasters):
106102
while True:
107-
row = randint(0, 8)
108-
col = randint(0, 8)
103+
row = randint(0, 4)
104+
col = randint(0, 4)
109105
if evaluate_position(row, col):
110106
break
111107
tilegrid[col, row] = random_cell()
@@ -117,7 +113,7 @@ def next_sprite(sprite):
117113

118114
def advance_animation():
119115
"""Cycle through animation cells each time."""
120-
for tile_number in range(81):
116+
for tile_number in range(25):
121117
tilegrid[tile_number] = next_sprite(tilegrid[tile_number])
122118

123119
def slide_tiles():
@@ -127,43 +123,39 @@ def slide_tiles():
127123

128124
def shift_tiles():
129125
"""Move tiles one spot to the left, and reset the tilegrid's position"""
130-
for row in range(8, 0, -1):
131-
for col in range(8):
126+
for row in range(4, 0, -1):
127+
for col in range(4):
132128
tilegrid[col, row] = tilegrid[col + 1, row - 1]
133-
tilegrid[8, row] = EMPTY
134-
for col in range(9):
129+
tilegrid[4, row] = EMPTY
130+
for col in range(5):
135131
tilegrid[col, 0] = EMPTY
136132
tilegrid.x = 0
137-
tilegrid.y = -32
133+
tilegrid.y = -64
138134

139135
def get_entry_row():
140136
while True:
141-
row = randint(0, 8)
142-
if tilegrid[8, row] == EMPTY and tilegrid[7, row] == EMPTY:
137+
row = randint(0, 4)
138+
if tilegrid[4, row] == EMPTY and tilegrid[3, row] == EMPTY:
143139
return row
144140

145141
def get_entry_column():
146142
while True:
147-
col = randint(0, 8)
143+
col = randint(0, 3)
148144
if tilegrid[col, 0] == EMPTY and tilegrid[col, 1] == EMPTY:
149145
return col
150146

151147
def add_toaster_or_toast():
152148
"""Maybe add a new toaster or toast on the right and/or top at a randon open location"""
153149
if randint(1, 10) <= CHANCE_OF_NEW_TOAST:
154150
tile = TOAST
155-
elif randint(1, 10) <= CHANCE_OF_NEW_TOASTER:
156-
tile = random_cell()
157151
else:
158-
tile = EMPTY
159-
tilegrid[8, get_entry_row()] = tile
152+
tile = random_cell()
153+
tilegrid[4, get_entry_row()] = tile
160154

161155
if randint(1, 10) <= CHANCE_OF_NEW_TOAST:
162156
tile = TOAST
163-
elif randint(1, 8) <= CHANCE_OF_NEW_TOASTER:
164-
tile = random_cell()
165157
else:
166-
tile = EMPTY
158+
tile = random_cell()
167159
tilegrid[get_entry_column(), 0] = tile
168160

169161
display = make_display()
@@ -172,7 +164,7 @@ def add_toaster_or_toast():
172164
display.refresh()
173165

174166
while True:
175-
for _ in range(32):
167+
for _ in range(64):
176168
display.refresh(target_frames_per_second=80)
177169
advance_animation()
178170
slide_tiles()
36.1 KB
Binary file not shown.
-9.12 KB
Binary file not shown.

CircuitPython_Scrolling_Clouds/code.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,45 @@ def make_display():
5050
pass
5151
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
5252
spi.unlock()
53-
tft_cs = board.D10
54-
tft_dc = board.D7
5553

5654
displayio.release_displays()
57-
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)
55+
display_bus = displayio.FourWire(spi, command=board.D7, chip_select=board.D10, reset=board.D9)
5856

59-
return ST7789(display_bus, width=240, height=240, rowstart=80)
57+
return ST7789(display_bus, width=240, height=240, rowstart=80, auto_refresh=True)
6058

6159
def make_tilegrid():
6260
"""Construct and return the tilegrid."""
6361
group = displayio.Group(max_size=10)
6462

65-
sprite_sheet, palette = adafruit_imageload.load("/tilesheet.bmp",
63+
sprite_sheet, palette = adafruit_imageload.load("/tilesheet-2x.bmp",
6664
bitmap=displayio.Bitmap,
6765
palette=displayio.Palette)
6866
grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
69-
width=16, height=10,
70-
tile_height=24, tile_width=16,
67+
width=9, height=5,
68+
tile_height=48, tile_width=32,
7169
default_tile=EMPTY)
7270
group.append(grid)
7371
display.show(group)
7472
return grid
7573

7674
def evaluate_position(row, col):
7775
"""Return how long of a cloud is placable at the given location.
78-
:param row: the tile row (0-9)
79-
:param col: the tile column (0-14)
76+
:param row: the tile row (0-4)
77+
:param col: the tile column (0-8)
8078
"""
8179
if tilegrid[col, row] != EMPTY or tilegrid[col + 1, row] != EMPTY:
8280
return 0
8381
end_col = col + 1
84-
while end_col < 16 and tilegrid[end_col, row] == EMPTY:
82+
while end_col < 9 and tilegrid[end_col, row] == EMPTY:
8583
end_col += 1
8684
return min([4, end_col - col])
8785

8886
def seed_clouds(number_of_clouds):
8987
"""Create the initial clouds so it doesn't start empty"""
9088
for _ in range(number_of_clouds):
9189
while True:
92-
row = randint(0, 9)
93-
col = randint(0,14)
90+
row = randint(0, 4)
91+
col = randint(0, 7)
9492
cloud_length = evaluate_position(row, col)
9593
if cloud_length > 0:
9694
break
@@ -103,36 +101,42 @@ def seed_clouds(number_of_clouds):
103101

104102
def slide_tiles():
105103
"""Move the tilegrid to the left, one pixel at a time, a full time width"""
106-
for _ in range(16):
104+
for _ in range(32):
107105
tilegrid.x -= 1
106+
display.refresh(target_frames_per_second=60)
107+
108108

109109
def shift_tiles():
110110
"""Move tiles one spot to the left, and reset the tilegrid's position"""
111-
for row in range(10):
112-
for col in range(15):
111+
for row in range(5):
112+
for col in range(8):
113113
tilegrid[col, row] = tilegrid[col + 1, row]
114-
tilegrid[15, row] = EMPTY
114+
tilegrid[8, row] = EMPTY
115115
tilegrid.x = 0
116116

117117
def extend_clouds():
118118
"""Extend any clouds on the right edge, either finishing them with a right
119119
end or continuing them with a middle piece
120120
"""
121-
for row in range(10):
122-
if tilegrid[14, row] == LEFT or tilegrid[14, row] == MIDDLE:
121+
for row in range(5):
122+
if tilegrid[7, row] == LEFT or tilegrid[7, row] == MIDDLE:
123123
if randint(1, 10) > CHANCE_OF_EXTENDING_A_CLOUD:
124-
tilegrid[15, row] = MIDDLE
124+
tilegrid[8, row] = MIDDLE
125125
else:
126-
tilegrid[15, row] = RIGHT
126+
tilegrid[8, row] = RIGHT
127127

128128
def add_cloud():
129129
"""Maybe add a new cloud on the right at a randon open row"""
130130
if randint(1, 10) > CHANCE_OF_NEW_CLOUD:
131+
count = 0
131132
while True:
132-
row = randint(0, 9)
133-
if tilegrid[14, row] == EMPTY and tilegrid[15, row] == EMPTY:
133+
count += 1
134+
if count == 50:
135+
return
136+
row = randint(0, 4)
137+
if tilegrid[7, row] == EMPTY and tilegrid[8, row] == EMPTY:
134138
break
135-
tilegrid[15, row] = LEFT
139+
tilegrid[8, row] = LEFT
136140

137141
display = make_display()
138142
tilegrid = make_tilegrid()
6.07 KB
Binary file not shown.

0 commit comments

Comments
 (0)