Skip to content

Commit fc00c15

Browse files
committed
Add code and spritsheet
1 parent 6e75fac commit fc00c15

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""
2+
This test will initialize the display using displayio and draw a solid green
3+
background, a smaller purple rectangle, and some yellow text.
4+
"""
5+
6+
#pylint:disable=redefined-outer-name
7+
import time
8+
from random import seed, randint
9+
import board
10+
import displayio
11+
from adafruit_st7789 import ST7789
12+
import adafruit_imageload
13+
14+
# Sprite cell values
15+
EMPTY = 0
16+
LEFT = 1
17+
MIDDLE = 2
18+
RIGHT = 3
19+
20+
# These constants determine what happens when tiles are shifted.
21+
# if randint(1, 10) > the value, the thing happens
22+
23+
# The chance a new cloud will enter
24+
CHANCE_OF_NEW_CLOUD = 4
25+
26+
# The chance an existing cloud gets extended
27+
CHANCE_OF_EXTENDING_A_CLOUD = 5
28+
29+
# Global variables
30+
display = None
31+
tilegrid = None
32+
33+
seed(int(time.monotonic()))
34+
35+
def make_display():
36+
"""Set up the display support.
37+
Return the Display object.
38+
"""
39+
spi = board.SPI()
40+
while not spi.try_lock():
41+
pass
42+
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
43+
spi.unlock()
44+
tft_cs = board.D10
45+
tft_dc = board.D7
46+
47+
displayio.release_displays()
48+
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)
49+
50+
return ST7789(display_bus, width=240, height=240, rowstart=80)
51+
52+
def make_tilegrid():
53+
"""Construct and return the tilegrid."""
54+
group = displayio.Group(max_size=10)
55+
56+
sprite_sheet, palette = adafruit_imageload.load("/tilesheet.bmp",
57+
bitmap=displayio.Bitmap,
58+
palette=displayio.Palette)
59+
grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
60+
width=16, height=10,
61+
tile_height=24, tile_width=16,
62+
default_tile=EMPTY)
63+
group.append(grid)
64+
display.show(group)
65+
return grid
66+
67+
def evaluate_position(row, col):
68+
"""Return how long of a cloud is placable at the given location.
69+
:param row: the tile row (0-9)
70+
:param col: the tile column (0-14)
71+
"""
72+
if tilegrid[col, row] != EMPTY or tilegrid[col + 1, row] != EMPTY:
73+
return 0
74+
end_col = col + 1
75+
while end_col < 16 and tilegrid[end_col, row] == EMPTY:
76+
end_col += 1
77+
return min([4, end_col - col])
78+
79+
def seed_clouds(number_of_clouds):
80+
"""Create the initial clouds so it doesn't start empty"""
81+
for _ in range(number_of_clouds):
82+
while True:
83+
row = randint(0, 9)
84+
col = randint(0,14)
85+
cloud_length = evaluate_position(row, col)
86+
if cloud_length > 0:
87+
break
88+
l = randint(1, cloud_length)
89+
tilegrid[col, row] = LEFT
90+
for _ in range(l - 2):
91+
col += 1
92+
tilegrid[col, row] = MIDDLE
93+
tilegrid[col + 1, row] = RIGHT
94+
95+
def slide_tiles():
96+
"""Move the tilegrid to the left, one pixel at a time, a full time width"""
97+
for _ in range(16):
98+
tilegrid.x -= 1
99+
display.refresh_soon()
100+
display.wait_for_frame()
101+
102+
def shift_tiles():
103+
"""Move tiles one spot to the left, and reset the tilegrid's position"""
104+
for row in range(10):
105+
for col in range(15):
106+
tilegrid[col, row] = tilegrid[col + 1, row]
107+
tilegrid[15, row] = EMPTY
108+
tilegrid.x = 0
109+
110+
def extend_clouds():
111+
"""Extend any clouds on the right edge, either finishing them with a right
112+
end or continuing them with a middle piece
113+
"""
114+
for row in range(10):
115+
if tilegrid[14, row] == LEFT or tilegrid[14, row] == MIDDLE:
116+
if randint(1, 10) > CHANCE_OF_EXTENDING_A_CLOUD:
117+
tilegrid[15, row] = MIDDLE
118+
else:
119+
tilegrid[15, row] = RIGHT
120+
121+
def add_cloud():
122+
"""Maybe add a new cloud on the right at a randon open row"""
123+
if randint(1, 10) > CHANCE_OF_NEW_CLOUD:
124+
while True:
125+
row = randint(0, 9)
126+
if tilegrid[14, row] == EMPTY and tilegrid[15, row] == EMPTY:
127+
break
128+
tilegrid[15, row] = LEFT
129+
130+
display = make_display()
131+
tilegrid = make_tilegrid()
132+
seed_clouds(5)
133+
134+
while True:
135+
slide_tiles()
136+
shift_tiles()
137+
extend_clouds()
138+
add_cloud()
1.57 KB
Binary file not shown.

0 commit comments

Comments
 (0)