77import terminalio
88from displayio import TileGrid , Group , OnDiskBitmap
99from adafruit_display_text .bitmap_label import Label
10- import foamyguy_nvm_helper as nvm_helper
10+
11+ try :
12+ import foamyguy_nvm_helper as nvm_helper
13+ except ImportError :
14+ nvm_helper = None
15+ print ("Warning: missing foamyguy_nvm_helper, will not be able to use NVM for highscore" )
1116
1217
1318class OctopusGame (Group ):
@@ -34,6 +39,8 @@ class OctopusGame(Group):
3439 GAME_MODE_A_LBL_Y = 128 - 14
3540 GAME_MODE_B_LBL_Y = 128 - 4
3641
42+ # speed adjustment values. Value in seconds will be subtracted from game speed.
43+ # larger values will equate to faster game speed.
3744 GAME_MODE_A_SPEED_ADJUSTMENT = 0.0
3845 GAME_MODE_B_SPEED_ADJUSTMENT = 0.2
3946
@@ -42,8 +49,7 @@ class OctopusGame(Group):
4249 HIGH_SCORE_SDCARD = 1
4350 HIGH_SCORE_NVM = 2
4451
45- # State machine constant variables:
46-
52+ # -- State machine constant variables --
4753 STATE_WAITING_TO_PLAY = - 1 # Before game begins
4854
4955 STATE_NORMAL_GAMEPLAY = 0 # Standard game behavior
@@ -65,11 +71,13 @@ class OctopusGame(Group):
6571 # Seconds between game Octopus movements, will get faster as score increases
6672 SCORE_SPEED_FACTOR = 0.5
6773
74+ # name of the file on the SDCard where high score data will be stored
6875 SDCARD_HIGH_SCORE_FILE = "octopus_high_score.json"
6976
7077 def __init__ (self , display = None , high_score_type = HIGH_SCORE_DISABLED ):
7178 super ().__init__ ()
7279
80+ # if user did not pass display argument try to use built-in display.
7381 if not display and "DISPLAY" in dir (board ):
7482 display = board .DISPLAY
7583
@@ -95,6 +103,7 @@ def __init__(self, display=None, high_score_type=HIGH_SCORE_DISABLED):
95103 self .current_game_mode = OctopusGame .GAME_MODE_A
96104
97105 # Set up Background
106+ self .bg_with_sadow = True
98107 self .bg_bmp = OnDiskBitmap ("bg_with_shadow.bmp" )
99108 self .bg_tilegrid = TileGrid (self .bg_bmp , pixel_shader = self .bg_bmp .pixel_shader )
100109 self .append (self .bg_tilegrid )
@@ -175,25 +184,26 @@ def __init__(self, display=None, high_score_type=HIGH_SCORE_DISABLED):
175184
176185 # if we're using any highscore system
177186 if self .high_score_type :
187+ # set up the high score label to show scores to user
178188 self .high_score_label = Label (font = terminalio .FONT , scale = 2 , background_color = 0xF9E8C2 ,
179189 anchor_point = (0.5 , 0.5 ), color = 0x000000 ,
180190 anchored_position = (display .width // 2 , display .height // 2 ),
181191 padding_left = 16 , padding_right = 16 , line_spacing = 0.75 )
182- # self.high_score_label.text = "67\n33\n21\n13\n11"
192+
193+ self .high_score_label .hidden = True
183194 self .append (self .high_score_label )
184195 # if we're using SDCard high score system
185- if self .high_score_type == self .HIGH_SCORE_SDCARD :
196+ if self .high_score_type == OctopusGame .HIGH_SCORE_SDCARD :
186197 # setup the high score system with SDCard data
187198 import sdcardio
188199 import storage
189200 sd = sdcardio .SDCard (board .SPI (), board .SD_CS )
190201 vfs = storage .VfsFat (sd )
191202 storage .mount (vfs , '/sd' )
192- print (os .listdir ('/sd' ))
193203
194- if self .high_score_type == self .HIGH_SCORE_NVM :
195- pass
196- # set up nvm highscore
204+ elif self .high_score_type == OctopusGame .HIGH_SCORE_NVM :
205+ if not nvm_helper :
206+ raise ImportError ( "Cannot use NVM High Score system without foamyguy_nvm_helper library." )
197207
198208 @property
199209 def score (self ):
@@ -210,6 +220,10 @@ def score(self, new_score):
210220 :param new_score: new score value
211221 :return: None
212222 """
223+ if new_score == 200 or new_score == 500 :
224+ self .extra_lives = 2
225+ self .update_extra_lives ()
226+
213227 self ._score = new_score
214228 self .score_speed_factor = self ._score / 500
215229 self .score_lbl .text = str (self .score )
@@ -311,19 +325,27 @@ def left_button_press(self):
311325 self .player .move_backward ()
312326
313327 else : # we're in game over, or waiting to play state
314- # if high score is enabled
315- if self .high_score_type :
316- # get the current high score data
317- self .read_high_score_data ()
318- # toggle the high score visibility
319- self .high_score_label .hidden = not self .high_score_label .hidden
328+
329+ # swap to the alternate background image
330+ if not self .bg_with_sadow :
331+ self .remove (self .bg_tilegrid )
332+ self .bg_bmp = OnDiskBitmap ("bg_with_shadow.bmp" )
333+ self .bg_tilegrid = TileGrid (self .bg_bmp , pixel_shader = self .bg_bmp .pixel_shader )
334+
335+ self .insert (0 , self .bg_tilegrid )
336+ else :
337+ self .remove (self .bg_tilegrid )
338+ self .bg_bmp = OnDiskBitmap ("bg.bmp" )
339+ self .bg_tilegrid = TileGrid (self .bg_bmp , pixel_shader = self .bg_bmp .pixel_shader )
340+ self .insert (0 , self .bg_tilegrid )
341+ self .bg_with_sadow = not self .bg_with_sadow
320342
321343 def right_button_press (self ):
322344 """
323345 Right movement button action function. code.py will poll the hardware and call this.
324346 :return: None
325347 """
326- # check which state we're in to determin the appropriate action(s)
348+ # check which state we're in to determine the appropriate action(s)
327349 if self .current_state not in (
328350 OctopusGame .STATE_GAME_OVER , OctopusGame .STATE_WAITING_TO_PLAY ):
329351
@@ -349,6 +371,7 @@ def right_button_press(self):
349371 # increment the score
350372 self .score += 1
351373
374+
352375 else : # we're in game over, or waiting to play state
353376 # if high score is enabled
354377 if self .high_score_type :
@@ -560,7 +583,7 @@ def initialize_high_score(self):
560583 f .close ()
561584 elif self .high_score_type == OctopusGame .HIGH_SCORE_NVM :
562585 try :
563- read_data = nvm_helper .read_data (verbose = True )
586+ read_data = nvm_helper .read_data ()
564587 except EOFError :
565588 read_data = None
566589
@@ -587,7 +610,7 @@ def read_high_score_data(self):
587610
588611 elif self .high_score_type == OctopusGame .HIGH_SCORE_NVM :
589612 try :
590- read_data = nvm_helper .read_data (verbose = True )
613+ read_data = nvm_helper .read_data ()
591614 if "highscore_list" in read_data .keys ():
592615 self .update_high_score_text (read_data )
593616 return read_data
@@ -690,7 +713,7 @@ class Octopus(Group):
690713
691714 # maximum action speed delay value
692715 # larger value means slower speed
693- MAX_TICK_SPEED = BASE_TICK_DELAY - 0.2 # seconds
716+ MAX_TICK_SPEED = BASE_TICK_DELAY - 0.35 # seconds
694717
695718 # order that the tentacles will take actions
696719 TENTACLE_ORDER = [0 , 2 , 1 , 3 ]
0 commit comments