-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.py
97 lines (74 loc) · 2.55 KB
/
ui.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
#For the handling the UI
#Author list:
# Luke Martin <[email protected]>
# Michael Milkovic <[email protected]>
# Derek Onay <[email protected]>
# Ryan Wiesjahn <[email protected]>
import somber as somber_engine
import logging
import config
import json
import time
class UI_Group:
def __init__(self,somber,level,sprite_group):
self.somber = somber
self.level = level
self.sprite_group = sprite_group
self.elements = []
def create_element(self,sprite,name,x=0,y=0):
_element = UI_Element(self.somber,self.level,sprite,self.sprite_group,name,x=x,y=y)
_element.parent = self
self.elements.append(_element)
return _element
def remove_element(self,element):
logging.debug('[Somber] Removed ui element \'%s\'.' % element.name)
self.elements.remove(element)
def get_element(self,name):
for element in self.elements:
if element.name == name:
return element
raise 'Element \'%s\' does not exist.' % name
def get_clicked_elements(self):
_clicked_elements = []
for element in self.elements:
if element.collides_with_point(self.somber.mouse_pos):
_clicked_elements.append(element)
return _clicked_elements
class UI_Element(somber_engine.Static_UI):
def __init__(self,somber,level,sprite,group,name,x=0,y=0):
somber_engine.Static_UI.__init__(self,sprite,somber=somber,pos=(x,y))
self.name = name
level.add_object(self,group)
def kill(self):
self.parent.remove_element(self)
somber_engine.Static_UI.kill(self)
def load_highscores():
try:
config.HIGHSCORES = json.loads(open('highscores.txt','r').readline())
logging.info('[Somber] Loaded highscores')
except IOError:
save_highscores()
def save_highscores():
with open('highscores.txt','w') as e:
e.write(json.dumps(config.HIGHSCORES))
logging.info('[Somber] Saved highscores!')
def add_highscore(score,kills):
date = time.strftime('%m/%d/%y')
_added = False
if not len(config.HIGHSCORES):
config.HIGHSCORES.append({'score': score,'kills': kills,'date': date})
logging.info('[Somber] New highscore!')
_added = True
else:
for entry in config.HIGHSCORES:
if score>entry['score']:
config.HIGHSCORES.insert(config.HIGHSCORES.index(entry),{'score': score,'kills': kills,'date': date})
logging.info('[Somber] New highscore!')
_added = True
break
#if len(config.HIGHSCORES)<5:
#if not _added:
config.HIGHSCORES.append({'score': score,'kills': kills,'date': date})
#lse:
if len(config.HIGHSCORES)>=5:
config.HIGHSCORES.pop()