-
Notifications
You must be signed in to change notification settings - Fork 0
/
highScoreScreen.py
98 lines (83 loc) · 2.99 KB
/
highScoreScreen.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from Tkinter import *
from stage import Stage
from state import State
import titleScreen
import pickle
hs_file = "high_scores.txt"
class Score:
def __init__(self, name, points):
self.name = name
self.points = points
class HighScores:
def __init__(self, **kargs):
self.scores = []
self.max_size = kargs.get("max_size", 10)
def add(self, score):
idx = self.potential_position(score)
if idx != None:
self.scores.insert(idx, score)
if len(self.scores) > self.max_size:
self.scores = self.scores[:self.max_size]
def get(self, idx):
if idx >= len(self.scores):
return None
else:
return self.scores[idx]
def potential_position(self, score):
"""
returns the index that the given score would have if inserted into the list.
returns None if the score is not high enough.
"""
for idx, cur_score in enumerate(self.scores):
if score.points >= cur_score.points:
return idx
#score didn't beat any existing ones,
#but we may have room to add it to the end
if len(self.scores) >= self.max_size:
return None
else:
return len(self.scores)
def load_high_scores():
try:
with open(hs_file) as file:
return pickle.load(file)
except IOError:
return HighScores()
def save_high_scores(data):
with open(hs_file, "w") as file:
pickle.dump(data, file)
class HighScoreScreen(Stage):
def __init__(self, parent, score_to_add = None):
Stage.__init__(self, parent)
self.score_to_add = score_to_add
self.high_scores = load_high_scores()
self.new_idx = None
if self.score_to_add != None:
s = Score("???", self.score_to_add)
self.new_idx = self.high_scores.potential_position(s)
if self.new_idx != None:
self.high_scores.add(s)
score_frame = Frame(self)
score_frame.pack()
for i in range(self.high_scores.max_size):
score = self.high_scores.get(i)
if score != None:
score = self.high_scores.scores[i]
name = score.name
points = str(score.points)
else:
name = "---"
points = "---"
if i == self.new_idx:
self.new_entry = Entry(score_frame)
self.new_entry.grid(row=i, column=0)
else:
Label(score_frame, text=name).grid(row=i,column=0)
Label(score_frame, text=points).grid(row=i,column=1)
b = Button(self, text="return to main", command=self.button_clicked)
b.pack()
def button_clicked(self):
if self.new_idx != None:
self.high_scores.scores[self.new_idx].name = self.new_entry.get()
save_high_scores(self.high_scores)
self.replace(titleScreen.TitleScreen)