Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/physics.py
diff options
context:
space:
mode:
Diffstat (limited to 'physics.py')
-rw-r--r--physics.py71
1 files changed, 45 insertions, 26 deletions
diff --git a/physics.py b/physics.py
index 5f80527..253ff1b 100644
--- a/physics.py
+++ b/physics.py
@@ -25,22 +25,29 @@ Code: git://git.sugarlabs.org/physics/mainline.git
"""
+import os
import sys
import math
+import gtk
+import logging
+
import pygame
from pygame.locals import *
from pygame.color import *
import sugargame
+
sys.path.append("lib/")
import pkg_resources
-# If your architecture is different, comment these lines and install the modules in your system.
+# If your architecture is different, comment these lines and install
+# the modules in your system.
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, activity):
@@ -56,6 +63,8 @@ class PhysicsGame:
# Set up the world (instance of Elements)
self.box2d = box2d
self.opening_queue = None
+ self.loop = True
+ self.pygame_started = False
def switch_off_fake_pygame_cursor_cb(self, panel, event):
self.show_fake_cursor = False
@@ -72,34 +81,44 @@ class PhysicsGame:
#Loading from journal
self.opening_queue = path
- def run(self):
+ def run(self, restart=False):
self.screen = pygame.display.get_surface()
- pygame.display.init()
+ if not restart:
+ pygame.init()
+ pygame.display.init()
+ # pygame.mixer.quit()
+ self.pygame_started = True
+
+ # Fake a Sugar cursor for the pyGame canvas area
+ self.show_fake_cursor = True
+ 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)
+
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 = True
- 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)
-
- while True:
+ if self.opening_queue:
+ path = self.opening_queue.encode('ascii', 'convert')
+ if os.path.exists(path):
+ self.world.json_load(path)
+
+ while self.loop:
while gtk.events_pending():
gtk.main_iteration()
- if self.opening_queue:
- self.world.json_load(self.opening_queue)
+ if not self.loop:
+ pygame.quit()
+ self.pygame_started = False
+ break
for event in pygame.event.get():
self.currentTool.handleEvents(event)
@@ -114,11 +133,11 @@ class PhysicsGame:
if type(body.userData) == type({}):
if body.userData.has_key('rollMotor'):
diff = body.userData['rollMotor'] \
- ['targetVelocity'] \
- - body.GetAngularVelocity()
+ ['targetVelocity'] \
+ - body.GetAngularVelocity()
body.ApplyTorque(body.userData['rollMotor'] \
- ['strength'] * diff \
- * body.getMassData().I)
+ ['strength'] * diff \
+ * body.getMassData().I)
# Update & Draw World
self.world.update()