Skip to content

Commit 9c3afac

Browse files
Game WIP
1 parent d6f8f93 commit 9c3afac

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"""
2+
Dragon Drop: a simple game for Adafruit MACROPAD. Uses OLED display in
3+
portrait (vertical) orientation. Tap one of four keys across a row to
4+
catch falling eggs before they hit the ground.
5+
"""
6+
7+
# pylint: disable=import-error, unused-import
8+
import random
9+
import time
10+
import displayio
11+
import adafruit_imageload
12+
from adafruit_macropad import MacroPad
13+
#from adafruit_display_shapes.rect import Rect
14+
#from adafruit_display_text import label
15+
16+
17+
# CONFIGURABLES ------------------------
18+
19+
MAX_EGGS = 20
20+
21+
22+
# UTILITY FUNCTIONS AND CLASSES --------
23+
24+
class Sprite:
25+
def __init__(self, tile):
26+
self.column = 0
27+
self.is_fire = 0
28+
self.state = 0
29+
self.y = 0.0
30+
self.v = 0.0
31+
self.tile = tile
32+
self.start_time = 0.0
33+
34+
# ONE-TIME INITIALIZATION --------------
35+
36+
MACROPAD = MacroPad(rotation=90)
37+
MACROPAD.display.auto_refresh = False
38+
MACROPAD.pixels.auto_write = False
39+
40+
GROUP = displayio.Group(max_size=MAX_EGGS + 10)
41+
42+
# Bitmap containing five shadow tiles (no shadow through max shadow)
43+
bitmap, palette = adafruit_imageload.load(
44+
'shadow.bmp', bitmap=displayio.Bitmap, palette=displayio.Palette)
45+
# Tilegrid containing four shadows; one per column
46+
shadow = displayio.TileGrid(bitmap, pixel_shader=palette, width=4, height=1,
47+
tile_width=16, tile_height=bitmap.height, default_tile=0, x=0,
48+
y=MACROPAD.display.height - bitmap.height)
49+
GROUP.append(shadow)
50+
51+
# Bitmap containing eggs, hatchling and fireballs
52+
bitmap, palette = adafruit_imageload.load(
53+
'sprites.bmp', bitmap=displayio.Bitmap, palette=displayio.Palette)
54+
palette.make_transparent(0)
55+
56+
ACTIVE_SPRITES = 0
57+
SPRITE = []
58+
for i in range(MAX_EGGS):
59+
GROUP.append(displayio.TileGrid(bitmap, pixel_shader=palette, width=1,
60+
height=1, tile_width=16,
61+
tile_height=bitmap.height, default_tile=4,
62+
x=MACROPAD.display.width,
63+
y=MACROPAD.display.height))
64+
SPRITE.append(Sprite(GROUP[len(GROUP) - 1]))
65+
66+
67+
MACROPAD.display.show(GROUP)
68+
MACROPAD.display.refresh()
69+
70+
71+
# MAIN LOOP ----------------------------
72+
73+
COLUMN_PRESSED = [False] * 4
74+
COLUMN_MIN = [0] * 4
75+
COLUMN_MAX = [0] * 4
76+
77+
while True:
78+
NOW = time.monotonic()
79+
80+
# Find the min/max vertical bounds of sprites per-column
81+
for i in range(4):
82+
COLUMN_MIN[i], COLUMN_MAX[i] = MACROPAD.display.height, 0
83+
for i in range(ACTIVE_SPRITES):
84+
column = SPRITE[i].column
85+
COLUMN_MIN[column] = min(COLUMN_MIN[column], SPRITE[i].y)
86+
COLUMN_MAX[column] = max(COLUMN_MAX[column], SPRITE[i].y)
87+
# Select shadow bitmaps based on each column's lowest sprite
88+
for i in range(4):
89+
shadow[i] = int(5 * COLUMN_MAX[i] / MACROPAD.display.height)
90+
91+
# Time to introduce a new sprite?
92+
if ACTIVE_SPRITES < MAX_EGGS and random.random() < 0.01:
93+
if max(COLUMN_MIN) > 16: # At least one column has space
94+
while True:
95+
column = random.randint(0, 3)
96+
if COLUMN_MIN[column] <= 16:
97+
continue
98+
# Found a spot. Add sprite and break loop
99+
SPRITE[ACTIVE_SPRITES].column = column
100+
SPRITE[ACTIVE_SPRITES].is_fire = (random.random() < 0.25)
101+
SPRITE[i].tile[0] = 0 # Egg
102+
SPRITE[ACTIVE_SPRITES].y = 0.0
103+
SPRITE[ACTIVE_SPRITES].v = 0.0
104+
SPRITE[ACTIVE_SPRITES].start_time = NOW
105+
ACTIVE_SPRITES += 1
106+
break
107+
108+
# Coalese any/all queued-up keypress events per column
109+
for x in range(4):
110+
COLUMN_PRESSED[x] = False
111+
while True:
112+
EVENT = MACROPAD.keys.events.get()
113+
if not EVENT:
114+
break
115+
if EVENT.pressed:
116+
COLUMN_PRESSED[EVENT.key_number % 4] = True
117+
if True in COLUMN_PRESSED:
118+
print(COLUMN_PRESSED)
119+
120+
for i in range(ACTIVE_SPRITES):
121+
if SPRITE[i].is_fire:
122+
SPRITE[i].tile[0] = 3 + int((NOW * 4) % 2.0)
123+
SPRITE[i].y += SPRITE[i].v
124+
# Ugh, this isn't right, need to reorder group too
125+
if SPRITE[i].y >= MACROPAD.display.height:
126+
SPRITE[i].tile[0] = 0
127+
SPRITE[i].x = MACROPAD.display.width # Move offscreen
128+
SPRITE[ACTIVE_SPRITES - 1], SPRITE[i] = SPRITE[i], SPRITE[ACTIVE_SPRITES - 1]
129+
ACTIVE_SPRITES -= 1
130+
SPRITE[i].v += 0.1
131+
SPRITE[i].tile.x = SPRITE[i].column * 16
132+
SPRITE[i].tile.y = int(SPRITE[i].y)
133+
134+
MACROPAD.display.refresh()
208 Bytes
Binary file not shown.
828 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)