-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
178 lines (141 loc) · 4.22 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# ------------------------------------------------------------------
# main.py
#
# Initialise stuff, handle events, main loop
# ------------------------------------------------------------------
import pygame
import pygtk
import gtk
import threading
import os
import sys
import traceback
from vec2d import Vec2d
import shared
import userspace
import gui
import levelmanager
import objects
background_color = (0,0,0) #black
frame_rate = 30.0
savedata_filename = os.path.join('userdata', 'savefile')
shared.stop_game = False
initial_help_text = """
#
# Welcome to thegame!
#
# Select a level. If you're new to thegame, you might want to start with
# 'Basics 1'. Have fun!
#
"""
def init():
# set WM_CLASS under X
os.environ['SDL_VIDEO_X11_WMCLASS'] = "thegame"
gtk.gdk.set_program_class("thegame")
# initialise gtk
gtk.threads_init()
shared.gtkwin = gtk.Window(gtk.WINDOW_TOPLEVEL)
# window sizes/positions
gtkscreen = shared.gtkwin.get_screen()
dim = Vec2d(gtkscreen.get_width(),
gtkscreen.get_height())
pygame_pos = dim * 0.01
pygame_dim = Vec2d(800, 640)
gtk_dim = Vec2d(640, 480)
gtk_pos = dim - gtk_dim - Vec2d(70, 70)
shared.gtkwin.move(int(gtk_pos.x), int(gtk_pos.y))
shared.gtkwin.resize(int(gtk_dim.x), int(gtk_dim.y))
shared.dim = pygame_dim
userspace.space['window_dim'] = pygame_dim
# initialise pygame
pygame.init()
global clock
clock = pygame.time.Clock()
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (pygame_pos.x, pygame_pos.y)
shared.canvas = pygame.display.set_mode((int(pygame_dim.x),
int(pygame_dim.y)))
# set window titles
shared.gtkwin.show()
shared.gtkwin.set_title("thegame - code editor")
pygame.display.set_caption("thegame", "thegame")
# set keybindings
userspace.reset_keybindings()
# make the code editor gui
shared.gui = gui.Gui()
# load levels and restore saved level data (if exists)
shared.levelmgr = levelmanager.LevelManager()
if not os.path.exists('userdata'):
os.makedirs('userdata')
try:
f = open(savedata_filename, 'r')
shared.levelmgr.load_level_data(f)
except IOError:
pass
# set initial help text
shared.gui.help_page.set_text(initial_help_text)
# start the game!
shared.levelmgr.start()
def handle_events():
# pygame events
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
shared.levelmgr.event(event)
if event.type == pygame.KEYDOWN:
# some temporary key binds
if event.key == pygame.K_ESCAPE and shared.levelmgr.current_level != 0:
shared.levelmgr.request_goto_level(0)
elif event.key == pygame.K_F2:
shared.levelmgr.request_next_level()
userspace.do_key(event.key)
return True
def loop():
# run gtk main loop in separate thread
class GtkLoop(threading.Thread):
def run(self):
gtk.main()
global gtk_thread
gtk_thread = GtkLoop()
gtk_thread.start()
# game loop
while not shared.stop_game:
elapsed = 1 / frame_rate
# events
if not handle_events():
break
# action!
# TODO: fix elapsed time handling!
shared.levelmgr.step(elapsed)
map(lambda o: o.step(elapsed), objects.world)
shared.gui.step(elapsed)
# handle requests
objects.handle_requests()
shared.levelmgr.handle_requests()
# draw pretty things
shared.canvas.fill(background_color)
map(lambda o: o.draw(), objects.world)
pygame.display.update()
# not so fast
clock.tick(frame_rate)
def end():
# stop the game and save level data
shared.levelmgr.stop()
f = open(savedata_filename, 'w')
shared.levelmgr.save_level_data(f)
# stop the gui
shared.gui.quit()
gtk.threads_enter()
gtk.main_quit()
gtk.threads_leave()
gtk_thread.join()
# stop pygame
pygame.quit()
# play the game!
if __name__ == '__main__':
try:
init()
loop()
except Exception, e:
tb = sys.exc_info()[2]
traceback.print_exception(e.__class__, e, tb)
end()