-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
88 lines (74 loc) · 2.24 KB
/
app.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
import pygame
from assets import ASSET_DICT, load_all_assets
from util import log
class ExitMain(StandardError):
"""
signals that we want to exit the main loop of the app
"""
pass
class App(object):
"""
takes care of a lot of the boring bits of our standard app, like loading
the assets and sizing up the screen.
"""
def __init__(self, title):
pygame.init()
pygame.display.set_caption(title)
pygame.display.set_mode((20, 20))
load_all_assets()
asset_names = ASSET_DICT.keys()
# set screen size to always fit the assets
BORDER_W = 10
BORDER_H = 10
max_w = 0
max_h = 0
for name in asset_names:
asset = ASSET_DICT[name]
w = asset.get_width()
h = asset.get_height()
if h > max_h:
max_h = h
if w > max_w:
max_w = w
screen = pygame.display.set_mode(
(BORDER_W + max_w + BORDER_W,
BORDER_H + max_h + BORDER_H)
)
log('calculated window size ({width}, {height}) from {count} assets'.format(
width=screen.get_width(), height=screen.get_height(), count=len(asset_names)),
'APP')
self.screen = screen
self.offset = (BORDER_W, BORDER_H)
return self.screen, self.offset
def handle_event(self, event):
"""
called on each event in the main loop
"""
pass
def quit_if_needed(self, event):
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
raise ExitMain('escape')
elif (event.type == pygame.QUIT):
raise ExitMain('quit event')
def handle_all_events(self):
events = pygame.event.get()
for event in events:
self.handle_event(event)
def render(self):
"""
run each frame.
"""
# self.screen.fill(0)
pass
def clean_up(self):
pass
def main(self):
while True:
try:
self.handle_all_events()
self.render()
pygame.display.flip()
except ExitMain:
break
self.clean_up()
pygame.display.quit()