Skip to content

Commit 340954e

Browse files
committed
Scrolling Clouds Update
Scrolling Clouds Update
1 parent 4d4d780 commit 340954e

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

CircuitPython_Scrolling_Clouds/code.py

Lines changed: 28 additions & 24 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()
@@ -142,4 +146,4 @@ def add_cloud():
142146
slide_tiles()
143147
shift_tiles()
144148
extend_clouds()
145-
add_cloud()
149+
add_cloud()
6.07 KB
Binary file not shown.

0 commit comments

Comments
 (0)