-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpygame_framework.py
440 lines (368 loc) · 15 KB
/
pygame_framework.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# C++ version Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
# Python version Copyright (c) 2010 kne / sirkne at gmail dot com
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
"""
Global Keys:
F1 - toggle menu (can greatly improve fps)
Space - shoot projectile
Z/X - zoom
Escape - quit
Other keys can be set by the individual test.
Mouse:
Left click - select/drag body (creates mouse joint)
Right click - pan
Shift+Left - drag to create a directed projectile
Scroll - zoom
"""
import pygame
import framework
from pygame.locals import *
from framework import *
try:
from pygame_gui import (fwGUI, gui)
GUIEnabled = True
except:
print('Unable to load PGU; menu disabled.')
GUIEnabled = False
class PygameDraw(b2DrawExtended):
"""
This debug draw class accepts callbacks from Box2D (which specifies what to draw)
and handles all of the rendering.
If you are writing your own game, you likely will not want to use debug drawing.
Debug drawing, as its name implies, is for debugging.
"""
surface = None
axisScale = 50.0
def __init__(self, **kwargs):
b2DrawExtended.__init__(self, **kwargs)
self.flipX = False
self.flipY = True
self.convertVertices = True
def StartDraw(self):
self.zoom=self.test.viewZoom
self.center=self.test.viewCenter
self.offset=self.test.viewOffset
self.screenSize=self.test.screenSize
def EndDraw(self): pass
def DrawPoint(self, p, size, color):
"""
Draw a single point at point p given a pixel size and color.
"""
self.DrawCircle(p, size/self.zoom, color, drawwidth=0)
def DrawAABB(self, aabb, color):
"""
Draw a wireframe around the AABB with the given color.
"""
points = [ (aabb.lowerBound.x, aabb.lowerBound.y ),
(aabb.upperBound.x, aabb.lowerBound.y ),
(aabb.upperBound.x, aabb.upperBound.y ),
(aabb.lowerBound.x, aabb.upperBound.y ) ]
pygame.draw.aalines(self.surface, color, True, points)
def DrawSegment(self, p1, p2, color):
"""
Draw the line segment from p1-p2 with the specified color.
"""
pygame.draw.aaline(self.surface, color.bytes, p1, p2)
def DrawTransform(self, xf):
"""
Draw the transform xf on the screen
"""
p1 = xf.position
p2 = self.to_screen(p1 + self.axisScale * xf.R.col1)
p3 = self.to_screen(p1 + self.axisScale * xf.R.col2)
p1 = self.to_screen(p1)
pygame.draw.aaline(self.surface, (255,0,0), p1, p2)
pygame.draw.aaline(self.surface, (0,255,0), p1, p3)
def DrawCircle(self, center, radius, color, drawwidth=1):
"""
Draw a wireframe circle given the center, radius, axis of orientation and color.
"""
radius *= self.zoom
if radius < 1: radius = 1
else: radius = int(radius)
pygame.draw.circle(self.surface, color.bytes, center, radius, drawwidth)
def DrawSolidCircle(self, center, radius, axis, color):
"""
Draw a solid circle given the center, radius, axis of orientation and color.
"""
radius *= self.zoom
if radius < 1: radius = 1
else: radius = int(radius)
pygame.draw.circle(self.surface, (color/2).bytes+[127], center, radius, 0)
pygame.draw.circle(self.surface, color.bytes, center, radius, 1)
pygame.draw.aaline(self.surface, (255,0,0), center, (center[0] - radius*axis[0], center[1] + radius*axis[1]))
def DrawPolygon(self, vertices, color):
"""
Draw a wireframe polygon given the screen vertices with the specified color.
"""
if not vertices:
return
if len(vertices) == 2:
pygame.draw.aaline(self.surface, color.bytes, vertices[0], vertices)
else:
pygame.draw.polygon(self.surface, color.bytes, vertices, 1)
def DrawSolidPolygon(self, vertices, color):
"""
Draw a filled polygon given the screen vertices with the specified color.
"""
if not vertices:
return
if len(vertices) == 2:
pygame.draw.aaline(self.surface, color.bytes, vertices[0], vertices[1])
else:
pygame.draw.polygon(self.surface, (color/2).bytes+[127], vertices, 0)
pygame.draw.polygon(self.surface, color.bytes, vertices, 1)
# the to_screen conversions are done in C with b2DrawExtended, leading to
# an increase in fps.
# You can also use the base b2Draw and implement these yourself, as the
# b2DrawExtended is implemented:
# def to_screen(self, point):
# """
# Convert from world to screen coordinates.
# In the class instance, we store a zoom factor, an offset indicating where
# the view extents start at, and the screen size (in pixels).
# """
# x=(point.x * self.zoom)-self.offset.x
# if self.flipX:
# x = self.screenSize.x - x
# y=(point.y * self.zoom)-self.offset.y
# if self.flipY:
# y = self.screenSize.y-y
# return (x, y)
class PygameFramework(FrameworkBase):
TEXTLINE_START=30
def setup_keys(self):
keys = [s for s in dir(pygame.locals) if s.startswith('K_')]
for key in keys:
value=getattr(pygame.locals, key)
setattr(Keys, key, value)
def __reset(self):
# Screen/rendering-related
self._viewZoom = 3.0
self._viewCenter = None
self._viewOffset = None
self.screenSize = None
self.rMouseDown = False
self.textLine = 30
self.font = None
self.fps = 0
# GUI-related (PGU)
self.gui_app =None
self.gui_table=None
self.setup_keys()
def __init__(self):
super(PygameFramework, self).__init__()
if fwSettings.onlyInit: # testing mode doesn't initialize pygame
return
self.__reset()
print('Initializing pygame framework...')
# Pygame Initialization
pygame.init()
caption= "Python Box2D Testbed - " + self.name
pygame.display.set_caption(caption)
# Screen and debug draw
self.screen = pygame.display.set_mode( (1024,768) )
self.screenSize = b2Vec2(*self.screen.get_size())
self.renderer = PygameDraw(surface=self.screen, test=self)
self.world.renderer=self.renderer
try:
self.font = pygame.font.Font(None, 15)
except IOError:
try:
self.font = pygame.font.Font("freesansbold.ttf", 15)
except IOError:
print("Unable to load default font or 'freesansbold.ttf'")
print("Disabling text drawing.")
self.Print = lambda *args: 0
self.DrawStringAt = lambda *args: 0
# GUI Initialization
if GUIEnabled:
self.gui_app = gui.App()
self.gui_table=fwGUI(self.settings)
container = gui.Container(align=1,valign=-1)
container.add(self.gui_table,0,0)
self.gui_app.init(container)
self.viewCenter = (0,20.0)
self.groundbody = self.world.CreateBody()
def setCenter(self, value):
"""
Updates the view offset based on the center of the screen.
Tells the debug draw to update its values also.
"""
self._viewCenter = b2Vec2( *value )
self._viewCenter *= self._viewZoom
self._viewOffset = self._viewCenter - self.screenSize/2
def setZoom(self, zoom):
self._viewZoom = zoom
viewZoom = property(lambda self: self._viewZoom, setZoom,
doc='Zoom factor for the display')
viewCenter = property(lambda self: self._viewCenter/self._viewZoom, setCenter,
doc='Screen center in camera coordinates')
viewOffset = property(lambda self: self._viewOffset,
doc='The offset of the top-left corner of the screen')
def checkEvents(self):
"""
Check for pygame events (mainly keyboard/mouse events).
Passes the events onto the GUI also.
"""
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == Keys.K_ESCAPE):
return False
elif event.type == KEYDOWN:
self._Keyboard_Event(event.key, down=True)
elif event.type == KEYUP:
self._Keyboard_Event(event.key, down=False)
elif event.type == MOUSEBUTTONDOWN:
p = self.ConvertScreenToWorld(*event.pos)
if event.button == 1: # left
mods = pygame.key.get_mods()
if mods & KMOD_LSHIFT:
self.ShiftMouseDown( p )
else:
self.MouseDown( p )
elif event.button == 2: #middle
pass
elif event.button == 3: #right
self.rMouseDown = True
elif event.button == 4:
self.viewZoom *= 1.1
elif event.button == 5:
self.viewZoom /= 1.1
elif event.type == MOUSEBUTTONUP:
p = self.ConvertScreenToWorld(*event.pos)
if event.button == 3: #right
self.rMouseDown = False
else:
self.MouseUp(p)
elif event.type == MOUSEMOTION:
p = self.ConvertScreenToWorld(*event.pos)
self.MouseMove(p)
if self.rMouseDown:
self.viewCenter -= (event.rel[0]/5.0, -event.rel[1]/5.0)
if GUIEnabled:
self.gui_app.event(event) #Pass the event to the GUI
return True
def run(self):
"""
Main loop.
Continues to run while checkEvents indicates the user has
requested to quit.
Updates the screen and tells the GUI to paint itself.
"""
# If any of the test constructors update the settings, reflect
# those changes on the GUI before running
if GUIEnabled:
self.gui_table.updateGUI(self.settings)
running = True
clock = pygame.time.Clock()
while running:
running = self.checkEvents()
self.screen.fill( (0,0,0) )
# Check keys that should be checked every loop (not only on initial keydown)
self.CheckKeys()
# Run the simulation loop
self.SimulationLoop()
if GUIEnabled and self.settings.drawMenu:
self.gui_app.paint(self.screen)
pygame.display.flip()
clock.tick(self.settings.hz)
self.fps = clock.get_fps()
self.world.contactListener = None
self.world.destructionListener=None
self.world.renderer=None
def _Keyboard_Event(self, key, down=True):
"""
Internal keyboard event, don't override this.
Checks for the initial keydown of the basic testbed keys. Passes the unused
ones onto the test via the Keyboard() function.
"""
if down:
if key==Keys.K_z: # Zoom in
self.viewZoom = min(1.1 * self.viewZoom, 50.0)
elif key==Keys.K_x: # Zoom out
self.viewZoom = max(0.9 * self.viewZoom, 0.02)
elif key==Keys.K_SPACE: # Launch a bomb
self.LaunchRandomBomb()
elif key==Keys.K_F1: # Toggle drawing the menu
self.settings.drawMenu = not self.settings.drawMenu
elif key==Keys.K_F2: # Do a single step
self.settings.singleStep=True
if GUIEnabled:
self.gui_table.updateGUI(self.settings)
else: # Inform the test of the key press
self.Keyboard(key)
else:
self.KeyboardUp(key)
def CheckKeys(self):
"""
Check the keys that are evaluated on every main loop iteration.
I.e., they aren't just evaluated when first pressed down
"""
pygame.event.pump()
self.keys = keys = pygame.key.get_pressed()
if keys[Keys.K_LEFT]:
self.viewCenter -= (0.5, 0)
elif keys[Keys.K_RIGHT]:
self.viewCenter += (0.5, 0)
if keys[Keys.K_UP]:
self.viewCenter += (0, 0.5)
elif keys[Keys.K_DOWN]:
self.viewCenter -= (0, 0.5)
if keys[Keys.K_HOME]:
self.viewZoom = 1.0
self.viewCenter = (0.0, 20.0)
def Step(self, settings):
if GUIEnabled:
# Update the settings based on the GUI
self.gui_table.updateSettings(self.settings)
super(PygameFramework, self).Step(settings)
if GUIEnabled:
# In case during the step the settings changed, update the GUI reflecting
# those settings.
self.gui_table.updateGUI(self.settings)
def ConvertScreenToWorld(self, x, y):
return b2Vec2((x + self.viewOffset.x) / self.viewZoom,
((self.screenSize.y - y + self.viewOffset.y) / self.viewZoom))
def DrawStringAt(self, x, y, str, color=(229,153,153,255)):
"""
Draw some text, str, at screen coordinates (x, y).
"""
self.screen.blit(self.font.render(str, True, color), (x,y))
def Print(self, str, color=(229,153,153,255)):
"""
Draw some text at the top status lines
and advance to the next line.
"""
self.screen.blit(self.font.render(str, True, color), (5,self.textLine))
self.textLine += 15
def Keyboard(self, key):
"""
Callback indicating 'key' has been pressed down.
The keys are mapped after pygame's style.
from framework import Keys
if key == Keys.K_z:
...
"""
pass
def KeyboardUp(self, key):
"""
Callback indicating 'key' has been released.
See Keyboard() for key information
"""
pass