Skip to content

Commit c93c2ae

Browse files
committed
Merge remote-tracking branch 'adafruit/master'
2 parents 6e75fac + af5623b commit c93c2ae

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
"""
2+
Continuously scroll randomly generated Mario style clouds.
3+
Designed fr an ItsyBitsy M4 Express and a 1.3" 240x240 TFT
4+
5+
Adafruit invests time and resources providing this open source code.
6+
Please support Adafruit and open source hardware by purchasing
7+
products from Adafruit!
8+
9+
Written by Dave Astels for Adafruit Industries
10+
Copyright (c) 2019 Adafruit Industries
11+
Licensed under the MIT license.
12+
13+
All text above must be included in any redistribution.
14+
"""
15+
16+
import time
17+
from random import seed, randint
18+
import board
19+
import displayio
20+
from adafruit_st7789 import ST7789
21+
import adafruit_imageload
22+
23+
# Sprite cell values
24+
EMPTY = 0
25+
LEFT = 1
26+
MIDDLE = 2
27+
RIGHT = 3
28+
29+
# These constants determine what happens when tiles are shifted.
30+
# if randint(1, 10) > the value, the thing happens
31+
32+
# The chance a new cloud will enter
33+
CHANCE_OF_NEW_CLOUD = 4
34+
35+
# The chance an existing cloud gets extended
36+
CHANCE_OF_EXTENDING_A_CLOUD = 5
37+
38+
# Global variables
39+
display = None
40+
tilegrid = None
41+
42+
seed(int(time.monotonic()))
43+
44+
def make_display():
45+
"""Set up the display support.
46+
Return the Display object.
47+
"""
48+
spi = board.SPI()
49+
while not spi.try_lock():
50+
pass
51+
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
52+
spi.unlock()
53+
tft_cs = board.D10
54+
tft_dc = board.D7
55+
56+
displayio.release_displays()
57+
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)
58+
59+
return ST7789(display_bus, width=240, height=240, rowstart=80)
60+
61+
def make_tilegrid():
62+
"""Construct and return the tilegrid."""
63+
group = displayio.Group(max_size=10)
64+
65+
sprite_sheet, palette = adafruit_imageload.load("/tilesheet.bmp",
66+
bitmap=displayio.Bitmap,
67+
palette=displayio.Palette)
68+
grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
69+
width=16, height=10,
70+
tile_height=24, tile_width=16,
71+
default_tile=EMPTY)
72+
group.append(grid)
73+
display.show(group)
74+
return grid
75+
76+
def evaluate_position(row, col):
77+
"""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)
80+
"""
81+
if tilegrid[col, row] != EMPTY or tilegrid[col + 1, row] != EMPTY:
82+
return 0
83+
end_col = col + 1
84+
while end_col < 16 and tilegrid[end_col, row] == EMPTY:
85+
end_col += 1
86+
return min([4, end_col - col])
87+
88+
def seed_clouds(number_of_clouds):
89+
"""Create the initial clouds so it doesn't start empty"""
90+
for _ in range(number_of_clouds):
91+
while True:
92+
row = randint(0, 9)
93+
col = randint(0,14)
94+
cloud_length = evaluate_position(row, col)
95+
if cloud_length > 0:
96+
break
97+
l = randint(1, cloud_length)
98+
tilegrid[col, row] = LEFT
99+
for _ in range(l - 2):
100+
col += 1
101+
tilegrid[col, row] = MIDDLE
102+
tilegrid[col + 1, row] = RIGHT
103+
104+
def slide_tiles():
105+
"""Move the tilegrid to the left, one pixel at a time, a full time width"""
106+
for _ in range(16):
107+
tilegrid.x -= 1
108+
display.refresh_soon()
109+
display.wait_for_frame()
110+
111+
def shift_tiles():
112+
"""Move tiles one spot to the left, and reset the tilegrid's position"""
113+
for row in range(10):
114+
for col in range(15):
115+
tilegrid[col, row] = tilegrid[col + 1, row]
116+
tilegrid[15, row] = EMPTY
117+
tilegrid.x = 0
118+
119+
def extend_clouds():
120+
"""Extend any clouds on the right edge, either finishing them with a right
121+
end or continuing them with a middle piece
122+
"""
123+
for row in range(10):
124+
if tilegrid[14, row] == LEFT or tilegrid[14, row] == MIDDLE:
125+
if randint(1, 10) > CHANCE_OF_EXTENDING_A_CLOUD:
126+
tilegrid[15, row] = MIDDLE
127+
else:
128+
tilegrid[15, row] = RIGHT
129+
130+
def add_cloud():
131+
"""Maybe add a new cloud on the right at a randon open row"""
132+
if randint(1, 10) > CHANCE_OF_NEW_CLOUD:
133+
while True:
134+
row = randint(0, 9)
135+
if tilegrid[14, row] == EMPTY and tilegrid[15, row] == EMPTY:
136+
break
137+
tilegrid[15, row] = LEFT
138+
139+
display = make_display()
140+
tilegrid = make_tilegrid()
141+
seed_clouds(5)
142+
143+
while True:
144+
slide_tiles()
145+
shift_tiles()
146+
extend_clouds()
147+
add_cloud()
1.57 KB
Binary file not shown.

0 commit comments

Comments
 (0)