51
51
for color in COLORS :
52
52
leds [color ].direction = Direction .OUTPUT
53
53
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
-
68
54
# display setup
69
55
display = board .DISPLAY
70
56
main_group = Group ()
91
77
main_group .append (curscore_val )
92
78
93
79
# 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 )
95
81
highscore_val .anchor_point = (1.0 , 0.0 )
96
82
highscore_val .anchored_position = (display .width - 4 ,
97
83
highscore_lbl .bounding_box [1 ] +
@@ -142,6 +128,20 @@ def __init__(self, difficulty: int, led_off_time: int, led_on_time: int):
142
128
# to avoid accidental double presses.
143
129
self .btn_cooldown_time = - 1
144
130
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
+
145
145
146
146
async def player_action (game_state : GameState ):
147
147
"""
@@ -153,9 +153,6 @@ async def player_action(game_state: GameState):
153
153
"""
154
154
# pylint: disable=too-many-branches, too-many-statements
155
155
156
- # access the global highscore variable
157
- global highscore # pylint: disable=global-statement
158
-
159
156
# loop forever inside of this task
160
157
while True :
161
158
# get any events that have occurred from the keypad object
@@ -255,13 +252,13 @@ async def player_action(game_state: GameState):
255
252
game_over_lbl .hidden = False
256
253
257
254
# 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 :
259
256
260
257
# save new high score value to NVM storage
261
258
nvm_helper .save_data (("bls_hs" , game_state .score ), test_run = False )
262
259
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
265
262
266
263
# update the high score label
267
264
highscore_val .text = str (game_state .score )
@@ -353,6 +350,11 @@ async def main():
353
350
# initialize the Game State
354
351
game_state = GameState (1 , 500 , 500 )
355
352
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
+
356
358
# initialze player task
357
359
player_task = asyncio .create_task (player_action (game_state ))
358
360
0 commit comments