-
Notifications
You must be signed in to change notification settings - Fork 1
/
scoreboard.py
61 lines (41 loc) · 1.33 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
52
53
54
55
56
57
58
59
60
61
from configuration import *
# takes highscore from game over screen an tests if it good enough for top ten
def write_highscore(score, name):
file = open(LOCATION_HIGHSCORE, 'r')
lines = file.readlines()
file.close()
scores = []
for line in lines:
highscore, scorer = line.strip('\n').split(',')
highscore = int(highscore)
scores.append((highscore, scorer))
# Iterate trough scores
# if higher than some value it gets added
# lowest value gets dropped
for x, y in scores:
if x < score:
scores.append((score, name))
scores.sort(reverse=True)
newscores = scores[:-1]
file = open(LOCATION_HIGHSCORE, 'w')
for i, j in newscores:
file.write(str(i) + "," + j + "\n")
file.close()
return True
return False
# Resets Highscore
def reset():
file = open(LOCATION_HIGHSCORE, 'w')
for i in range(10):
file.write(str(0) + ',' + "XXXXXX\n")
file.close()
# Reads textfile end transforms it in a better format
def read():
file = open(LOCATION_HIGHSCORE, 'r')
lines = file.readlines()
file.close()
highscore = []
for line in lines:
score, scorer = line.strip('\n').split(',')
highscore.append((scorer, score))
return highscore