-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIManager.py
142 lines (107 loc) · 3.77 KB
/
UIManager.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import tkinter as tk
import mp3play
class Window:
def __init__(self):
self.SCREEN_WIDTH = 800
self.SCREEN_HEIGHT = 600
self.tk = tk.Tk()
self.tk.geometry(str(self.SCREEN_WIDTH)+'x'+str(self.SCREEN_HEIGHT))
self.tk.resizable(False, False)
self.tk.config(cursor="none")
self.canv = None
self.game_is_on = False
def set_fullscreen(self):
self.tk.attributes('-fullscreen', True)
self.SCREEN_WIDTH = self.tk.winfo_screenwidth()
self.SCREEN_HEIGHT = self.tk.winfo_screenheight()
def set_screen_resolution(self, x, y):
self.SCREEN_WIDTH = x
self.SCREEN_HEIGHT = y
self.tk.geometry(str(self.SCREEN_WIDTH)+'x'+str(self.SCREEN_HEIGHT))
def start_menu(self):
pass
def show_canvas(self):
self.canv = tk.Canvas(self.tk, bg='white')
self.canv.pack(fill=tk.BOTH, expand=1)
self.tk.config(cursor="none")
self.game_is_on = True
def pause(self):
self.canv.destroy()
self.game_is_on = False
@staticmethod
def close():
quit()
class SoundManager:
def __init__(self):
self.sounds = {}
def add(self, name, path):
self.sounds[name] = {'id': mp3play.load(path), 'path': path}
def play(self, name, **kwargs):
self.sounds[name]['state'] = 'on'
self.sounds[name]['id'].play(**kwargs)
def stop(self, name, **kwargs):
self.sounds[name]['state'] = 'off'
self.sounds[name]['id'].stop(**kwargs)
def isPlaying(self, name):
return self.sounds[name]['id'].isplaying()
class ImageManager:
def __init__(self):
self.images = {}
def add(self, name, path):
self.images[name] = {'id': tk.PhotoImage(file=path), 'path': path}
self.images[name]['id'].image = self.images[name]['id']
def draw(self, name, x=0, y=0):
self.images[name]['img'] = tk.Label(image=self.images[name]['id'])
self.images[name]['img'].place(x=x, y=y)
def undraw(self, name):
self.images[name]['img'].destroy()
def place(self, name, x, y):
self.images[name]['img'].place(x=x, y=y)
class Button:
def __init__(self, window, text='', x=0, y=0):
self.id = tk.Button(window.tk, text=text)
self.text = text
self.place = (x, y)
self.id.place(x=x, y=y)
def set_place(self, x, y):
self.id.place(x=x, y=y)
self.place = (x, y)
def bind(self, command, function):
self.id.bind(command, function)
class Label:
def __init__(self, text='', width=20, height=2, bg='grey', font='30', x=0, y=0):
self.text = text
self.place = (x, y)
self.width = width
self.height = height
self.bg = bg
self.font = font
self.id = tk.Label(width=width, height=height, bg=bg, text=text, font=font)
self.id.place(x=x, y=y)
def set_place(self, x, y):
self.id.place(x=x, y=y)
self.place = (x, y)
class BindingManager:
def __init__(self, window):
self.bindings = {}
self.window = window
self.pressed = set()
window.tk.bind('<KeyPress>', self._keyPressed)
window.tk.bind('<KeyRelease>', self._keyReleased)
def bind(self, name, *keys):
self.bindings[name] = {*keys}
def add_keys(self, name, *keys):
self.bindings[name].update([*keys])
def del_keys(self, name, *keys):
self.bindings[name].discard([*keys])
def isPressed(self, name):
for key in self.bindings[name]:
if key not in self.pressed:
return False
return True
def _keyPressed(self, event):
self.pressed.add(event.keycode)
pass
def _keyReleased(self, event):
self.pressed.discard(event.keycode)
pass