-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoreboard.py
More file actions
53 lines (42 loc) · 1.52 KB
/
scoreboard.py
File metadata and controls
53 lines (42 loc) · 1.52 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
import turtle
from main import SCREEN_SIZE_XY_PX
FONT_TYPE = "Arial"
FONT_SIZE = 20
FONT_STYLE = "normal"
class Scoreboard(turtle.Turtle):
def __init__(self, color):
super().__init__()
self.score = 0
with open("data.txt", mode="r") as datafile:
self.highscore = int(datafile.read())
self.penup()
self.color(color)
self.hideturtle()
self.update()
def update(self):
self.clear()
self.write_score()
self.write_highscore()
def write_score(self):
x_position = 0
y_position = SCREEN_SIZE_XY_PX / 2 - 2 * FONT_SIZE
self.goto(x_position, y_position)
self.write(arg=f"Score:{self.score}", align="center", font=(FONT_STYLE, FONT_SIZE, FONT_STYLE))
def write_highscore(self):
x_position = 0
y_position = SCREEN_SIZE_XY_PX / 2 - 3 * FONT_SIZE
self.goto(x_position, y_position)
self.write(arg=f"Highscore:{self.highscore}", align="center",
font=(FONT_STYLE, round(FONT_SIZE / 2), FONT_STYLE))
def increase_score(self):
self.score += 1
def update_highscore(self):
if self.score > self.highscore:
with open("data.txt", mode="w") as datafile:
datafile.write(str(self.score))
self.highscore = int(self.score)
def game_over(self):
self.goto(0, 0)
self.write(arg=f"Game Over!", align="center", font=(FONT_STYLE, FONT_SIZE * 2, FONT_STYLE))
def reset_score(self):
self.score = 0