MCPcopy Index your code
hub / github.com/geekcomputers/Python / Scoreboard

Class Scoreboard

Snake Game Using Turtle/scoreboard.py:14–79  ·  view source on GitHub ↗

This class maintains the scoreboard, high score, and game messages.

Source from the content-addressed store, hash-verified

12INSTRUCTION_FONT = ("Lucida Sans", 16, "normal")
13
14class Scoreboard(Turtle):
15 """ This class maintains the scoreboard, high score, and game messages. """
16 def __init__(self):
17 super().__init__()
18 self.screen = Screen() # Get access to the screen object
19 self.score = 0
20 self.high_score = self.load_high_score()
21 self.penup()
22 self.hideturtle()
23 self.update_scoreboard()
24
25 def load_high_score(self):
26 """Loads high score from highscore.txt. Returns 0 if not found."""
27 try:
28 with open("highscore.txt", mode="r") as file:
29 return int(file.read())
30 except (FileNotFoundError, ValueError):
31 return 0
32
33 def update_scoreboard(self):
34 """Clears and rewrites the score and high score in the top-left corner."""
35 self.clear()
36 self.color(colors.SCORE_COLOR)
37 # Dynamically calculate position to be well-placed in the header
38 x_pos = -self.screen.window_width() / 2 + 30
39 y_pos = self.screen.window_height() / 2 - 60
40 self.goto(x_pos, y_pos)
41 self.write(f"Score: {self.score} | High Score: {self.high_score}", align=ALIGNMENT, font=SCORE_FONT)
42
43 def increase_score(self):
44 """Increases score and updates the display."""
45 self.score += 1
46 self.update_scoreboard()
47
48 def reset(self):
49 """Checks for new high score, saves it, and resets the score."""
50 if self.score > self.high_score:
51 self.high_score = self.score
52 with open("highscore.txt", mode="w") as file:
53 file.write(str(self.high_score))
54 self.score = 0
55 self.update_scoreboard()
56
57 def game_over(self):
58 """Displays the Game Over message and instructions."""
59 self.goto(0, 40)
60 self.color(colors.GAME_OVER_COLOR)
61 self.write("GAME OVER", align="center", font=MESSAGE_FONT)
62 self.goto(0, -40)
63 self.write("Click 'Restart' or Press 'R'", align="center", font=INSTRUCTION_FONT)
64
65 def display_pause(self):
66 """Displays the PAUSED message."""
67 self.goto(0, 40)
68 self.color(colors.MESSAGE_COLOR)
69 self.write("PAUSED", align="center", font=MESSAGE_FONT)
70 self.goto(0, -40)
71 self.write("Click 'Resume' or Press 'Space'", align="center", font=INSTRUCTION_FONT)

Callers 1

main.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected