From 324fb287d7b540bbb09a58350e2614507ed5c530 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 11 Dec 2013 20:16:42 +0000 Subject: massive commit to reconstruct world before commits were lost --- (limited to 'physics.py') diff --git a/physics.py b/physics.py index 47eb464..20502bd 100644 --- a/physics.py +++ b/physics.py @@ -25,54 +25,51 @@ 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 olpcgames +import sugargame + sys.path.append("lib/") import pkg_resources -sys.path.append("lib/Elements-0.13-py2.5.egg") +# If your architecture is different, comment these lines and install +# the modules in your system. 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 + def __init__(self, activity): # Get everything set up self.clock = pygame.time.Clock() - self.canvas = olpcgames.ACTIVITY.canvas + self.canvas = activity.canvas self.in_focus = True # Create the name --> instance map for components self.toolList = {} for c in tools.allTools: + if c.name == tools.EraseAllTool.name: + self.toolList[c.name] = c(self, activity) + continue 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() + self.opening_queue = None + self.loop = True + self.pygame_started = False - # 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) + self.full_pos_list = [] + self.tracked_bodies = [] def switch_off_fake_pygame_cursor_cb(self, panel, event): self.show_fake_cursor = False @@ -80,23 +77,84 @@ class PhysicsGame: def switch_on_fake_pygame_cursor_cb(self, panel, event): self.show_fake_cursor = True - def run(self): - while True: + def write_file(self, path): + #Saving to journal + self.world.add.remove_mouseJoint() + self.world.json_save(path) + + def read_file(self, path): + #Loading from journal + self.opening_queue = path + + def run(self, restart=False): + self.screen = pygame.display.get_surface() + 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) + self.world.add.ground() + + 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 not self.loop: + pygame.quit() + self.pygame_started = False + break + for event in pygame.event.get(): self.currentTool.handleEvents(event) + if event.type == MOUSEBUTTONUP: + if event.button == 1: + self.show_fake_cursor = True if self.in_focus: # Drive motors if self.world.run_physics: for body in self.world.world.GetBodyList(): if type(body.userData) == type({}): + if body.userData.has_key('track_index'): + trackdex = body.userData['track_index'] + tupled_pos = tuple_to_int( + (body.position.x, body.position.y)) + posx = tupled_pos[0] + posy = tupled_pos[1] + try: + self.full_pos_list[trackdex].append(posx) + self.full_pos_list[trackdex].append(posy) + except: + self.full_pos_list.append([posx, posy]) + 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() @@ -109,7 +167,7 @@ class PhysicsGame: # Show Sugar like cursor for UI consistancy if self.show_fake_cursor: self.screen.blit(self.cursor_picture, - pygame.mouse.get_pos()) + pygame.mouse.get_pos()) # Flip Display pygame.display.flip() @@ -121,16 +179,9 @@ class PhysicsGame: self.currentTool.cancel() self.currentTool = self.toolList[tool] -def main(): - toolbarheight = 75 - tabheight = 45 - pygame.display.init() - video_info = pygame.display.Info() - x = video_info.current_w - y = video_info.current_h - screen = pygame.display.set_mode((x, y - toolbarheight - tabheight)) - game = PhysicsGame(screen) - game.run() +def main(activity): + game = PhysicsGame(activity) + return game # Make sure that main get's called if __name__ == '__main__': -- cgit v0.9.1