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
|
#!/usr/bin/python
"""
This file is part of the 'Physics' Project
Physics is a 2D Physics Playground for Kids (supporting Box2D2)
Physics Copyright (C) 2008, Alex Levenson, Brian Jordan
Elements Copyright (C) 2008, The Elements Team, <elements@linuxuser.at>
Wiki: http://wiki.laptop.org/wiki/Physics
IRC: #olpc-physics on irc.freenode.org
Code: http://dev.laptop.org/git?p=activities/physics
git clone git://dev.laptop.org/activities/physics
License: GPLv3 http://gplv3.fsf.org/
"""
import sys
import math
import pygame
from pygame.locals import *
from pygame.color import *
import olpcgames
sys.path.append("lib/")
import pkg_resources
sys.path.append("lib/Elements-0.13-py2.5.egg")
sys.path.append("lib/Box2D-2.0.2b1-py2.5-linux-i686.egg")
import Box2D as box2d
import elements
import tools
from helpers import *
import gtk
class PhysicsGame:
def __init__(self,screen):
self.screen = screen
# get everything set up
self.trace_screen = pygame.Surface(self.screen.get_size())
self.trace_screen.fill((255,255,255))
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 24) # font object
self.canvas = olpcgames.ACTIVITY.canvas
# create the name --> instance map for components
self.toolList = {}
for c in tools.allTools:
self.toolList[c.name] = c(self)
self.currentTool = self.toolList[tools.allTools[0].name]
# set up the world (instance of Elements)
self.box2d = box2d
self.world = elements.Elements(self.screen.get_size())
self.world.renderer.set_surface(self.screen)
# set up static environment
self.world.add.ground()
# Fake a Sugar cursor for the pyGame canvas area
self.show_fake_cursor = False
pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
self.cursor_picture = pygame.image.load('standardcursor.png')
self.cursor_picture.convert_alpha()
self.canvas.connect("enter_notify_event", self.switch_on_fake_pygame_cursor_cb)
self.canvas.connect("leave_notify_event", self.switch_off_fake_pygame_cursor_cb)
self.canvas.add_events(gtk.gdk.ENTER_NOTIFY_MASK
| gtk.gdk.LEAVE_NOTIFY_MASK)
def switch_off_fake_pygame_cursor_cb(self, panel, event):
self.show_fake_cursor = False
def switch_on_fake_pygame_cursor_cb(self, panel, event):
self.show_fake_cursor = True
def run(self):
self.running = True
while self.running:
for event in pygame.event.get():
self.currentTool.handleEvents(event)
# Clear Display
self.screen.fill((255,255,255)) #255 for white
self.screen.blit(self.trace_screen, (0,0)) #blit on the traces
#Loop trough objects to apply impulses
if self.world.run_physics:
for body in self.world.world.GetBodyList():
if type(body.userData) == type({}):
if body.userData.has_key('rollMotor'):
diff = body.userData['rollMotor']['targetVelocity']- body.GetAngularVelocity()
body.ApplyTorque(body.userData['rollMotor']['strength']*diff*body.getMassData().I)
# Update & Draw World
self.world.update()
self.world.draw()
#Loop trough objects to draw extra stuff
if self.world.run_physics:
for body in self.world.world.GetBodyList():
if type(body.userData) == type({}):
if body.userData.has_key('paint'):
color = body.userData['paint']['color']
rel_pos = body.GetWorldVector(body.userData['paint']['pos']).tuple()
x = (body.GetPosition().tuple()[0]+rel_pos[0])*self.world.ppm
y = (body.GetPosition().tuple()[1]+rel_pos[1])*self.world.ppm
pos = self.world.to_screen((x,y))
pygame.draw.circle(self.trace_screen, color, pos, 2, 0)
pygame.draw.circle(self.screen, (255,208,0), pos, 2, 0)
# draw output from tools
self.currentTool.draw()
# Show Sugar like cursor for UI consistancy
if self.show_fake_cursor:
self.screen.blit(self.cursor_picture, pygame.mouse.get_pos())
# Flip Display
pygame.display.flip()
# Try to stay at 30 FPS
self.clock.tick(30) # originally 50
def setTool(self,tool):
self.currentTool.cancel()
self.currentTool = self.toolList[tool]
def main():
toolbarheight = 75
tabheight = 45
pygame.init()
pygame.display.init()
x,y = pygame.display.list_modes()[0]
screen = pygame.display.set_mode((x,y-toolbarheight-tabheight))
# create an instance of the game
game = PhysicsGame(screen)
# start the main loop
game.run()
# make sure that main get's called
if __name__ == '__main__':
main()
|