-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoreboard.py
More file actions
58 lines (48 loc) · 1.75 KB
/
Copy pathscoreboard.py
File metadata and controls
58 lines (48 loc) · 1.75 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
from turtle import Turtle, Screen
ALIGNMENT = "center"
FONT = ("Courier", 15, "normal")
class Scoreboard(Turtle):
def __init__(self):
Turtle.__init__(self)
self.color("white")
self.hideturtle()
self.penup()
self.goto(0, 270)
self.player = Screen().textinput(title="Snake", prompt="Enter your name: ")
self.score = 0
self.high_score = self.set_high_score()
self.update()
def update(self):
self.clear()
self.write(f"Score: {self.score} High Score: {self.high_score}", align=ALIGNMENT, font=FONT)
def set_high_score(self):
high_score = self.fetch_high_score()
if high_score is None:
high_score = self.write_high_score('0')
return high_score
# helper function 1 for set_high_score
def fetch_high_score(self):
try:
with open(f"./data/score_{self.player}.txt") as file:
high_score = file.read()
except FileNotFoundError:
return None
else:
return high_score
# helper function 2 for set_high_score
def write_high_score(self, score):
if self.player is not None: # score is not recorded if player is None
with open(f"./data/score_{self.player}.txt", 'w') as file:
file.write(str(score))
return score
def increase_score(self):
self.score += 1
def reset(self):
if self.score > int(self.high_score):
self.high_score = self.score
self.write_high_score(self.high_score)
# self.score = 0 # alternative mechanism for restarting the game (part)
self.update()
def game_over(self):
self.goto(0, 0)
self.write("GAME OVER", align="center", font=FONT)