-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
98 lines (78 loc) · 2.6 KB
/
main.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
"""main module, starts game and main loop"""
import pygame
import game, gfx, snd, txt
import allmodules
import gamepref
import gameplay
from pygame.locals import *
#at this point, all needed pygame modules should
#be imported, so they can be initialized at the
#start of main()
def main(args):
try:
gamemain(args)
except KeyboardInterrupt:
print 'Keyboard Interrupt...'
print 'Exiting'
def gamemain(args):
#initialize all our code (not load resources)
pygame.init()
game.clock = pygame.time.Clock()
gamepref.load_prefs()
gfx.initialize(game.size)
pygame.display.set_caption('Py1945')
#input.init()
snd.initialize()
if not txt.initialize():
raise pygame.error, "Pygame Font Module Unable to Initialize"
#create the starting game handler
from gameinit import GameInit
from gamefinish import GameFinish
game.handler = GameInit(GameFinish(None))
gamestart = pygame.time.get_ticks()
numframes = 0
#main game loop
lasthandler = None
while game.handler:
numframes += 1
handler = game.handler
if handler != lasthandler:
lasthandler = handler
if hasattr(handler, 'starting'):
handler.starting()
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
fps = game.clock.get_fps()
continue
elif event.type == pygame.ACTIVEEVENT:
if event.state == 4 and event.gain:
#uniconified, lets try to kick the screen
pygame.display.update()
elif event.state == 2:
if hasattr(game.handler, 'gotfocus'):
if event.gain:
game.handler.gotfocus()
else:
game.handler.lostfocus()
continue
elif event.type == pygame.KEYUP or event.type == pygame.KEYDOWN:
handler.input(event)
elif event.type == pygame.QUIT:
game.handler = None
break
handler.event(event)
handler.run()
game.clockticks = game.clock.tick(40)
gfx.update()
while not pygame.display.get_active():
pygame.time.wait(100)
pygame.event.pump()
gameend = pygame.time.get_ticks()
runtime = (gameend - gamestart) / 1000.0
#game is finished at this point, do any
#uninitialization needed
gamepref.save_prefs()
pygame.quit()
if __name__ == '__main__':
import sys
main(sys.argv)