move highscore to GameState object instead of global var
This commit is contained in:
parent
e810d3f23d
commit
40a0ccbdf3
1 changed files with 23 additions and 21 deletions
|
|
@ -51,20 +51,6 @@ leds = {
|
||||||
for color in COLORS:
|
for color in COLORS:
|
||||||
leds[color].direction = Direction.OUTPUT
|
leds[color].direction = Direction.OUTPUT
|
||||||
|
|
||||||
# a global variable to hold the eventual high-score
|
|
||||||
highscore = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
# read data from NVM storage
|
|
||||||
read_data = nvm_helper.read_data()
|
|
||||||
# if we found data check if it's a high-score value
|
|
||||||
if isinstance(read_data, list) and read_data[0] == "bls_hs":
|
|
||||||
# it is a high-score so populate the label with its value
|
|
||||||
highscore = read_data[1]
|
|
||||||
except EOFError:
|
|
||||||
# no high-score data
|
|
||||||
pass
|
|
||||||
|
|
||||||
# display setup
|
# display setup
|
||||||
display = board.DISPLAY
|
display = board.DISPLAY
|
||||||
main_group = Group()
|
main_group = Group()
|
||||||
|
|
@ -91,7 +77,7 @@ curscore_val.anchored_position = (4,
|
||||||
main_group.append(curscore_val)
|
main_group.append(curscore_val)
|
||||||
|
|
||||||
# Label to show the high score numerical value
|
# Label to show the high score numerical value
|
||||||
highscore_val = Label(terminalio.FONT, text="" if highscore is None else str(highscore), scale=4)
|
highscore_val = Label(terminalio.FONT, text="0", scale=4)
|
||||||
highscore_val.anchor_point = (1.0, 0.0)
|
highscore_val.anchor_point = (1.0, 0.0)
|
||||||
highscore_val.anchored_position = (display.width - 4,
|
highscore_val.anchored_position = (display.width - 4,
|
||||||
highscore_lbl.bounding_box[1] +
|
highscore_lbl.bounding_box[1] +
|
||||||
|
|
@ -142,6 +128,20 @@ class GameState:
|
||||||
# to avoid accidental double presses.
|
# to avoid accidental double presses.
|
||||||
self.btn_cooldown_time = -1
|
self.btn_cooldown_time = -1
|
||||||
|
|
||||||
|
# a variable to hold the eventual high-score
|
||||||
|
self.highscore = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# read data from NVM storage
|
||||||
|
read_data = nvm_helper.read_data()
|
||||||
|
# if we found data check if it's a high-score value
|
||||||
|
if isinstance(read_data, list) and read_data[0] == "bls_hs":
|
||||||
|
# it is a high-score so populate the label with its value
|
||||||
|
self.highscore = read_data[1]
|
||||||
|
except EOFError:
|
||||||
|
# no high-score data
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def player_action(game_state: GameState):
|
async def player_action(game_state: GameState):
|
||||||
"""
|
"""
|
||||||
|
|
@ -153,9 +153,6 @@ async def player_action(game_state: GameState):
|
||||||
"""
|
"""
|
||||||
# pylint: disable=too-many-branches, too-many-statements
|
# pylint: disable=too-many-branches, too-many-statements
|
||||||
|
|
||||||
# access the global highscore variable
|
|
||||||
global highscore # pylint: disable=global-statement
|
|
||||||
|
|
||||||
# loop forever inside of this task
|
# loop forever inside of this task
|
||||||
while True:
|
while True:
|
||||||
# get any events that have occurred from the keypad object
|
# get any events that have occurred from the keypad object
|
||||||
|
|
@ -255,13 +252,13 @@ async def player_action(game_state: GameState):
|
||||||
game_over_lbl.hidden = False
|
game_over_lbl.hidden = False
|
||||||
|
|
||||||
# if the player's current score is higher than the highscore
|
# if the player's current score is higher than the highscore
|
||||||
if highscore is None or game_state.score > highscore:
|
if game_state.highscore is None or game_state.score > game_state.highscore:
|
||||||
|
|
||||||
# save new high score value to NVM storage
|
# save new high score value to NVM storage
|
||||||
nvm_helper.save_data(("bls_hs", game_state.score), test_run=False)
|
nvm_helper.save_data(("bls_hs", game_state.score), test_run=False)
|
||||||
|
|
||||||
# update global highscore variable to the players score
|
# update highscore variable to the players score
|
||||||
highscore = game_state.score
|
game_state.highscore = game_state.score
|
||||||
|
|
||||||
# update the high score label
|
# update the high score label
|
||||||
highscore_val.text = str(game_state.score)
|
highscore_val.text = str(game_state.score)
|
||||||
|
|
@ -353,6 +350,11 @@ async def main():
|
||||||
# initialize the Game State
|
# initialize the Game State
|
||||||
game_state = GameState(1, 500, 500)
|
game_state = GameState(1, 500, 500)
|
||||||
|
|
||||||
|
# if there is a saved highscore
|
||||||
|
if game_state.highscore is not None:
|
||||||
|
# set the highscore into it's label to show on the display
|
||||||
|
highscore_val.text = str(game_state.highscore)
|
||||||
|
|
||||||
# initialze player task
|
# initialze player task
|
||||||
player_task = asyncio.create_task(player_action(game_state))
|
player_task = asyncio.create_task(player_action(game_state))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue