5151for color in COLORS :
5252 leds [color ].direction = Direction .OUTPUT
5353
54- # a global variable to hold the eventual high-score
55- highscore = None
56-
57- try :
58- # read data from NVM storage
59- read_data = nvm_helper .read_data ()
60- # if we found data check if it's a high-score value
61- if isinstance (read_data , list ) and read_data [0 ] == "bls_hs" :
62- # it is a high-score so populate the label with its value
63- highscore = read_data [1 ]
64- except EOFError :
65- # no high-score data
66- pass
67-
6854# display setup
6955display = board .DISPLAY
7056main_group = Group ()
9177main_group .append (curscore_val )
9278
9379# Label to show the high score numerical value
94- highscore_val = Label (terminalio .FONT , text = "" if highscore is None else str ( highscore ) , scale = 4 )
80+ highscore_val = Label (terminalio .FONT , text = "0" , scale = 4 )
9581highscore_val .anchor_point = (1.0 , 0.0 )
9682highscore_val .anchored_position = (display .width - 4 ,
9783 highscore_lbl .bounding_box [1 ] +
@@ -142,6 +128,20 @@ def __init__(self, difficulty: int, led_off_time: int, led_on_time: int):
142128 # to avoid accidental double presses.
143129 self .btn_cooldown_time = - 1
144130
131+ # a variable to hold the eventual high-score
132+ self .highscore = None
133+
134+ try :
135+ # read data from NVM storage
136+ read_data = nvm_helper .read_data ()
137+ # if we found data check if it's a high-score value
138+ if isinstance (read_data , list ) and read_data [0 ] == "bls_hs" :
139+ # it is a high-score so populate the label with its value
140+ self .highscore = read_data [1 ]
141+ except EOFError :
142+ # no high-score data
143+ pass
144+
145145
146146async def player_action (game_state : GameState ):
147147 """
@@ -153,9 +153,6 @@ async def player_action(game_state: GameState):
153153 """
154154 # pylint: disable=too-many-branches, too-many-statements
155155
156- # access the global highscore variable
157- global highscore # pylint: disable=global-statement
158-
159156 # loop forever inside of this task
160157 while True :
161158 # get any events that have occurred from the keypad object
@@ -255,13 +252,13 @@ async def player_action(game_state: GameState):
255252 game_over_lbl .hidden = False
256253
257254 # if the player's current score is higher than the highscore
258- if highscore is None or game_state .score > highscore :
255+ if game_state . highscore is None or game_state .score > game_state . highscore :
259256
260257 # save new high score value to NVM storage
261258 nvm_helper .save_data (("bls_hs" , game_state .score ), test_run = False )
262259
263- # update global highscore variable to the players score
264- highscore = game_state .score
260+ # update highscore variable to the players score
261+ game_state . highscore = game_state .score
265262
266263 # update the high score label
267264 highscore_val .text = str (game_state .score )
@@ -353,6 +350,11 @@ async def main():
353350 # initialize the Game State
354351 game_state = GameState (1 , 500 , 500 )
355352
353+ # if there is a saved highscore
354+ if game_state .highscore is not None :
355+ # set the highscore into it's label to show on the display
356+ highscore_val .text = str (game_state .highscore )
357+
356358 # initialze player task
357359 player_task = asyncio .create_task (player_action (game_state ))
358360
0 commit comments