Skip to content

Commit

Permalink
Can switch the debug pannel on and off.
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 7, 2021
1 parent c8f2213 commit 89d4a13
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ def handle_input(c, game_state, scene, snake, apples):
Returns
-------
quit, reset, new_game_state : bool, int, int
a signal telling if the game is over, a reset integer for the score and the new state of the game.
quit, reset, new_game_state, swicth_debug : bool, int, int, bool
a signal telling if the game is over, a reset integer for the score, the new state of the game and a debug switching signal.
"""
# quit the game.
if c in [27, ord('q')]:
return True, None, None
return True, None, None, False

# respawn the objects if 'r' is pressed whilst not playing
reset = 0
new_game_state = game_state
reset, new_game_state, switch_debug = 0, game_state, False
if c == ord('r'):
if game_state == LOST:
snake.spawn(*scene)
Expand All @@ -78,7 +77,11 @@ def handle_input(c, game_state, scene, snake, apples):
if c == 10:
new_game_state = MENU if game_state == PLAY else PLAY

return False, 1 - reset, new_game_state
# switches the debug pannel.
if c == ord('d'):
switch_debug = True

return False, 1 - reset, new_game_state, switch_debug


def menu():
Expand Down Expand Up @@ -134,7 +137,7 @@ def play(c, snake, apples, score):
return score + code, None, None


def blit(stdscr, borders, snake, apples, score, game_state, debug_msg, fps):
def blit(stdscr, borders, snake, apples, score, game_state, debug, debug_msg, fps):
"""
Blits all the objects of the game onto the screen.
Expand All @@ -152,6 +155,8 @@ def blit(stdscr, borders, snake, apples, score, game_state, debug_msg, fps):
the current score in the game.
game_state : int
the current state of the game.
debug : bool
triggers the printing of the debug message.
debug_msg : str
the current debug message.
fps ; int
Expand Down Expand Up @@ -179,8 +184,9 @@ def blit(stdscr, borders, snake, apples, score, game_state, debug_msg, fps):
# f"{len(snake.body) = }",
f"{score = }",
f"{game_state = }",
f"{debug_msg = }"
]
if debug:
msgs += [f"{debug_msg = }"]
h = 0 if snake.get_head()[0] > curses.LINES // 2 else curses.LINES - len(msgs)
for row, msg in zip(range(len(msgs)), list(map(str, msgs))):
stdscr.addstr(h + row, 0, msg)
Expand Down
6 changes: 4 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def main(stdscr):

# internal variables of the game.
game_state = PLAY
game_debug = False
game_debug_msg = ''
game_score = 0

Expand All @@ -72,9 +73,10 @@ def main(stdscr):
c = stdscr.getch()

# handle the game input.
quit_game, reset, game_state = handle_input(c, game_state, scene, snake, apples)
quit_game, reset, game_state, switch_debug = handle_input(c, game_state, scene, snake, apples)
if quit_game:
break
game_debug ^= switch_debug
game_score *= reset

# run the current game state code snippet.
Expand All @@ -87,7 +89,7 @@ def main(stdscr):
game_debug_msg = new_debug_msg if new_debug_msg else game_debug_msg

# blit all the objects on the screen.
blit(stdscr, borders, snake, apples, game_score, game_state, game_debug_msg, fps)
blit(stdscr, borders, snake, apples, game_score, game_state, game_debug, game_debug_msg, fps)


if __name__ == "__main__":
Expand Down

0 comments on commit 89d4a13

Please sign in to comment.