1111import displayio
1212import adafruit_imageload
1313from adafruit_macropad import MacroPad
14- #from adafruit_display_shapes.rect import Rect
15- #from adafruit_display_text import label
14+ from adafruit_bitmap_font import bitmap_font
15+ from adafruit_display_text import label
16+ from adafruit_progressbar .progressbar import HorizontalProgressBar
17+
1618
1719
1820# CONFIGURABLES ------------------------
@@ -33,8 +35,8 @@ def __init__(self, column, start_time):
3335 self .prev_pos = 0
3436
3537# List of Sprite objects, appended and popped as needed. Same is done with
36- # the GROUP list (below), but they're not 1:1 -- latter has extra elements
37- # for background, score, etc.
38+ # the PLAY_GROUP list (below), but they're not 1:1 -- latter has extra
39+ # elements for background, score, etc.
3840SPRITES = []
3941
4042# ONE-TIME INITIALIZATION --------------
@@ -43,24 +45,38 @@ def __init__(self, column, start_time):
4345MACROPAD .display .auto_refresh = False
4446MACROPAD .pixels .auto_write = False
4547
46- GROUP = displayio .Group (max_size = MAX_EGGS + 10 )
48+ TITLE_GROUP = displayio .Group (max_size = 1 )
49+ # Bitmap containing title screen
50+ TITLE_BITMAP , TITLE_PALETTE = adafruit_imageload .load (
51+ 'dragondrop/title.bmp' , bitmap = displayio .Bitmap , palette = displayio .Palette )
52+ TITLE_GROUP .append (displayio .TileGrid (TITLE_BITMAP , pixel_shader = TITLE_PALETTE , width = 1 ,
53+ height = 1 , tile_width = TITLE_BITMAP .width ,
54+ tile_height = TITLE_BITMAP .height ))
55+
56+ PLAY_GROUP = displayio .Group (max_size = MAX_EGGS + 10 )
4757
4858# Bitmap containing five shadow tiles (no shadow through max shadow)
49- BITMAP , PALETTE = adafruit_imageload .load (
50- 'shadow.bmp' , bitmap = displayio .Bitmap , palette = displayio .Palette )
59+ SHADOW_BITMAP , SHADOW_PALETTE = adafruit_imageload .load (
60+ 'dragondrop/ shadow.bmp' , bitmap = displayio .Bitmap , palette = displayio .Palette )
5161# Tilegrid containing four shadows; one per column
52- SHADOW = displayio .TileGrid (BITMAP , pixel_shader = PALETTE , width = 4 , height = 1 ,
53- tile_width = 16 , tile_height = BITMAP .height ,
54- x = 0 , y = MACROPAD .display .height - BITMAP .height )
55- GROUP .append (SHADOW )
62+ SHADOW = displayio .TileGrid (SHADOW_BITMAP , pixel_shader = SHADOW_PALETTE , width = 4 , height = 1 ,
63+ tile_width = 16 , tile_height = SHADOW_BITMAP .height ,
64+ x = 0 , y = MACROPAD .display .height - SHADOW_BITMAP .height )
65+ PLAY_GROUP .append (SHADOW )
5666
5767# Bitmap containing eggs, hatchling and fireballs
5868SPRITE_BITMAP , SPRITE_PALETTE = adafruit_imageload .load (
59- 'sprites.bmp' , bitmap = displayio .Bitmap , palette = displayio .Palette )
69+ 'dragondrop/ sprites.bmp' , bitmap = displayio .Bitmap , palette = displayio .Palette )
6070SPRITE_PALETTE .make_transparent (0 )
6171
62- MACROPAD .display .show (GROUP )
63- MACROPAD .display .refresh ()
72+ FONT = bitmap_font .load_font ("/dragondrop/cursive-smart.pcf" )
73+ SCORE_LABEL = label .Label (FONT , text = '0' , max_glyphs = 10 , color = 0xFFFFFF , anchor_point = (0.5 , 0.0 ), anchored_position = (MACROPAD .display .width // 2 , 10 ))
74+ PLAY_GROUP .append (SCORE_LABEL )
75+
76+ LIFE_BAR = HorizontalProgressBar ((0 , 0 ), (MACROPAD .display .width , 7 ), value = 100 , min_value = 0 , max_value = 100 , bar_color = 0xFFFFFF , outline_color = 0xFFFFFF , fill_color = 0 , margin_size = 1 )
77+ PLAY_GROUP .append (LIFE_BAR )
78+
79+
6480
6581# Sprite states include falling, paused for catch and paused for breakage.
6682# Following pause, sprite is removed.
@@ -70,6 +86,15 @@ def __init__(self, column, start_time):
7086
7187START_TIME = time .monotonic ()
7288
89+ MACROPAD .display .show (TITLE_GROUP )
90+ MACROPAD .display .refresh ()
91+ while not MACROPAD .keys .events .get ():
92+ pass
93+
94+ MACROPAD .display .show (PLAY_GROUP )
95+ MACROPAD .display .refresh ()
96+ SCORE = 0
97+
7398while True :
7499 NOW = time .monotonic ()
75100 SPEED = 10 + (NOW - START_TIME ) / 30
@@ -92,13 +117,17 @@ def __init__(self, column, start_time):
92117 for i in range (len (SPRITES ) - 1 , - 1 , - 1 ):
93118 sprite = SPRITES [i ]
94119 COLUMN = sprite .column
95- TILE = GROUP [i + 1 ] # Corresponding TileGroup for sprite
120+ TILE = PLAY_GROUP [i + 1 ] # Corresponding TileGroup for sprite
96121 ELAPSED = NOW - sprite .start_time # Time since add or pause event
97122
123+ if sprite .is_fire :
124+ # Animate fire sprites
125+ TILE [0 ] = 3 + int ((NOW * 6 ) % 2.0 )
126+
98127 if sprite .paused :
99128 if ELAPSED > 0.75 : # Hold position for 3/4 second
100129 SPRITES .pop (i )
101- GROUP .pop (i + 1 )
130+ PLAY_GROUP .pop (i + 1 )
102131 continue
103132 if not sprite .is_fire :
104133 COLUMN_MAX [COLUMN ] = max (COLUMN_MAX [COLUMN ], MACROPAD .display .height - 22 )
@@ -107,44 +136,49 @@ def __init__(self, column, start_time):
107136 COLUMN_MIN [COLUMN ] = min (COLUMN_MIN [COLUMN ], y )
108137 if not sprite .is_fire :
109138 COLUMN_MAX [COLUMN ] = max (COLUMN_MAX [COLUMN ], y )
110- TILE .y = int (y ) # Sprite's vertical position in GROUP list
139+ TILE .y = int (y ) # Sprite's vertical position in PLAY_GROUP list
111140
112- if sprite .is_fire :
113- if y >= MACROPAD .display .height :
114- SPRITES .pop (i )
115- GROUP .pop (i + 1 )
116- continue
141+ if sprite .is_fire :
142+ if y >= MACROPAD .display .height :
143+ SPRITES .pop (i )
144+ PLAY_GROUP .pop (i + 1 )
145+ continue
146+ else :
147+ # Animate fire sprites
148+ TILE [0 ] = 3 + int ((NOW * 6 ) % 2.0 )
149+ # Fire catch logic
150+ if y >= MACROPAD .display .height - 40 and COLUMN_PRESSED [COLUMN ]:
151+ sprite .paused = True
152+ sprite .start_time = NOW
153+ TILE .y = MACROPAD .display .height - 20
154+ LIFE_BAR .value = max (0 , LIFE_BAR .value - 4 )
117155 else :
118- # Animate fire sprites
119- TILE [0 ] = 3 + int ((NOW * 6 ) % 2.0 )
120- # Fire catch logic
121- if y >= MACROPAD .display .height - 40 and COLUMN_PRESSED [COLUMN ]:
122- sprite .paused = True
123- sprite .start_time = NOW
124- TILE .y = MACROPAD .display .height - 20
125- else :
126- if y >= MACROPAD .display .height - 22 :
127- # Egg hit ground
128- TILE .y = MACROPAD .display .height - 22
129- TILE [0 ] = 1 # Broken egg
130- sprite .paused = True
131- sprite .start_time = NOW
132- elif y >= MACROPAD .display .height - 40 :
133- if COLUMN_PRESSED [COLUMN ]:
134- # Egg caught at right time
156+ if y >= MACROPAD .display .height - 22 :
157+ # Egg hit ground
135158 TILE .y = MACROPAD .display .height - 22
136- TILE [0 ] = 2 # Dragon hatchling
137- sprite .paused = True
138- sprite .start_time = NOW
139- elif y >= MACROPAD .display .height - 58 :
140- if COLUMN_PRESSED [COLUMN ]:
141- # Egg caught too soon
142- TILE .y = MACROPAD .display .height - 40
143159 TILE [0 ] = 1 # Broken egg
144160 sprite .paused = True
145161 sprite .start_time = NOW
146-
147- sprite .prev_pos = y
162+ LIFE_BAR .value = max (0 , LIFE_BAR .value - 4 )
163+ elif y >= MACROPAD .display .height - 40 :
164+ if COLUMN_PRESSED [COLUMN ]:
165+ # Egg caught at right time
166+ TILE .y = MACROPAD .display .height - 22
167+ TILE [0 ] = 2 # Dragon hatchling
168+ SCORE += 10
169+ SCORE_LABEL .text = str (SCORE )
170+ sprite .paused = True
171+ sprite .start_time = NOW
172+ elif y >= MACROPAD .display .height - 58 :
173+ if COLUMN_PRESSED [COLUMN ]:
174+ # Egg caught too soon
175+ TILE .y = MACROPAD .display .height - 40
176+ TILE [0 ] = 1 # Broken egg
177+ sprite .paused = True
178+ sprite .start_time = NOW
179+ LIFE_BAR .value = max (0 , LIFE_BAR .value - 4 )
180+
181+ sprite .prev_pos = y
148182
149183 # Select shadow bitmaps based on each column's lowest sprite
150184 for i in range (4 ):
@@ -159,7 +193,7 @@ def __init__(self, column, start_time):
159193 continue
160194 # Found a spot. Add sprite and break loop
161195 SPRITES .append (Sprite (COLUMN , NOW ))
162- GROUP . append ( displayio .TileGrid (SPRITE_BITMAP ,
196+ PLAY_GROUP . insert ( - 2 , displayio .TileGrid (SPRITE_BITMAP ,
163197 pixel_shader = SPRITE_PALETTE ,
164198 width = 1 , height = 1 ,
165199 tile_width = 16 ,
0 commit comments