-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
61 lines (52 loc) · 2.55 KB
/
score.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
52
53
54
55
56
57
58
59
60
61
from turtle import Turtle, Screen
from highscores import HighScores
class ScoreBoard:
def __init__(self):
self.scores = HighScores()
self.player_score = 0
self.text_turtle = Turtle()
self.screen = Screen()
# to be a read/write from a text file and allow high scores to transcend to other runs of the program
self.high_scores = self.scores.get_scores()
def display_score(self):
self.text_turtle.penup()
self.text_turtle.hideturtle()
self.text_turtle.goto(0, 280)
self.text_turtle.color("green")
self.text_turtle.write(f"Player score: {self.player_score}", move=False, align="center",
font=("Arial", 14, "normal"))
def update_score(self):
self.player_score += 1
self.text_turtle.clear()
self.text_turtle.write(f"Player score: {self.player_score}", move=False, align="center",
font=("Arial", 14, "normal"))
def check_high_score(self):
if self.player_score > self.high_scores[len(self.high_scores) - 1][1]:
player_name = self.screen.textinput("High score detected!",
"What name should we record the high score under?")
self.order_high_scores(player_name)
self.scores.record_scores(self.high_scores)
self.display_high_scores()
# this is weird, could be written cleaner
def order_high_scores(self, name):
for score in range(len(self.high_scores) - 1, -1, -1):
if self.high_scores[score][1] < self.player_score:
x = self.high_scores[score]
self.high_scores[score] = (name, self.player_score)
if score + 1 <= len(self.high_scores) - 1:
self.high_scores[score + 1] = x
def display_high_scores(self):
turtle_x = 0
turtle_y = 70
self.text_turtle.goto(turtle_x, turtle_y)
self.text_turtle.write(arg=f" Game Over!\nFinal score: {self.player_score}", align="center",
font=("Arial", 16, "normal"))
turtle_y -= 40
self.text_turtle.goto(turtle_x, turtle_y)
self.text_turtle.write(arg=f" HIGH SCORES!", align="center", font=("Arial", 20, "normal"))
turtle_y -= 20
for hscore in self.high_scores:
turtle_y -= 20
self.text_turtle.goto(turtle_x, turtle_y)
self.text_turtle.write(arg=f" {hscore[0]}: {hscore[1]}", align="center",
font=("Arial", 16, "normal"))