-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboard.py
51 lines (41 loc) · 1.43 KB
/
scoreboard.py
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
from turtle import Turtle
def fetch_high_score():
with open("Highscore.txt", "r") as file:
return file.read()
# Constants
ALIGNMENT = "center"
SCORE_FONT = ("Courier", 20, "bold")
GAMEOVER_FONT = ("Courier", 28, "bold")
SCORE_COLOR = "blue"
SCORE_COORDS = (0, 270)
START_SCORE = 0
HIGH_SCORE = int(fetch_high_score())
class ScoreBoard(Turtle):
def __init__(self):
# Inheriting from Turtle Super-Class
super().__init__()
self.score = START_SCORE
self.high_score = HIGH_SCORE
self.color(SCORE_COLOR)
self.penup()
self.hideturtle()
self.update_scoreboard()
def update_scoreboard(self):
"""Updates the scoreboard by writing new score"""
self.clear()
self.goto(SCORE_COORDS)
self.write(f"Score: {self.score} High Score: {self.high_score}", align=ALIGNMENT,
font=SCORE_FONT)
def game_restart(self):
"""Saves the HighScore to Highscore.txt and resets the score"""
if self.score > self.high_score:
self.high_score = self.score
with open("Highscore.txt", "w") as file:
file.write(str(self.high_score))
self.score = 0
self.update_scoreboard()
def increase_score(self):
"""Increases score count by 1 at each call"""
self.score += 1
# clear the old written score, before writing new updated score
self.update_scoreboard()