Repository navigation
Expand file tree
/
Copy pathscoreboard.py
More file actions
60 lines (49 loc) · 1.82 KB
/
Copy pathscoreboard.py
File metadata and controls
60 lines (49 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from turtle import Turtle
## Resources
# Code is from and inspired by with some improvements:
# 100 Days of Code: The Complete Python Pro Bootcamp
# https://www.udemy.com/share/103J8C3@0R0_ZWb7kJ4do0X-4zRsulmwTyefJndLSF7-DCQhp_3ojIbfiWmdBIvIMOalNv_ECg==/
ALIGNMENT = "left"
FONT = ("Courier", 24, "normal")
GAME_DATA = "turtle_crossing/turtle_crossing_data.txt"
class Scoreboard(Turtle):
"""
A class representing the scoreboard in the Turtle Crossing game.
Inherits from the Turtle class and handles the display and updating
of the game level.
Attributes:
level (int): The current level of the game.
Methods:
update_scoreboard(): Clears the current score and updates the display
with the latest level.
increase_level(): Increments the game level by 1 and updates the
scoreboard.
game_over(): Displays the "GAME OVER" message at the center of the
screen.
"""
def __init__(self):
super().__init__()
self.level = 1
with open(GAME_DATA) as data:
self.high_score = int(data.read())
self.hideturtle()
self.penup()
self.goto(-280, 250)
self.update_scoreboard()
def update_scoreboard(self):
self.clear()
self.write(f"Level: {self.level}, High Score: {self.high_score}",
align=ALIGNMENT, font=FONT)
def reset(self):
if self.score > self.high_score:
self.high_score = self.score
with open(GAME_DATA, mode="w") as data:
data.write(f"{self.high_score}")
self.score = 0
self.update_scoreboard()
def increase_level(self):
self.level += 1
self.update_scoreboard()
def game_over(self):
self.goto(0, 0)
self.write(f"GAME OVER", align="center", font=FONT)