diff options
-rw-r--r-- | activity.py | 20 | ||||
-rw-r--r-- | physics.py | 15 |
2 files changed, 28 insertions, 7 deletions
diff --git a/activity.py b/activity.py index 9524b11..0d0a720 100644 --- a/activity.py +++ b/activity.py @@ -45,6 +45,26 @@ class PhysicsActivity(olpcgames.PyGameActivity): super(PhysicsActivity, self).__init__(handle) self.metadata['mime_type'] = 'application/x-physics-activity' + def get_preview(self): + """Custom preview code to get image from pygame. + """ + surface = pygame.display.get_surface() + width, height = surface.get_width(), surface.get_height() + pixbuf = gtk.gdk.pixbuf_new_from_data(pygame.image.tostring(surface, "RGB"), + gtk.gdk.COLORSPACE_RGB, 0, 8, + width, height, + 3 * width) + pixbuf = pixbuf.scale_simple(300, 225, gtk.gdk.INTERP_BILINEAR) + + preview_data = [] + def save_func(buf, data): + data.append(buf) + + pixbuf.save_to_callback(save_func, 'png', user_data=preview_data) + preview_data = ''.join(preview_data) + + return preview_data + def write_file(self, file_path): """Over-ride olpcgames write_file so that title keeps working. """ @@ -63,7 +63,7 @@ class PhysicsGame: # 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)) + 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) @@ -82,17 +82,18 @@ class PhysicsGame: while self.running: for event in pygame.event.get(): self.currentTool.handleEvents(event) - # Clear Display - self.screen.fill((255,255,255)) #255 for white + + # Drive motors 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) + diff = body.userData['rollMotor']['targetVelocity'] - body.GetAngularVelocity() + body.ApplyTorque(body.userData['rollMotor']['strength'] * diff * body.getMassData().I) # Update & Draw World self.world.update() + self.screen.fill((255, 255, 255)) #255 for white self.world.draw() # draw output from tools @@ -108,7 +109,7 @@ class PhysicsGame: # Try to stay at 30 FPS self.clock.tick(30) # originally 50 - def setTool(self,tool): + def setTool(self, tool): self.currentTool.cancel() self.currentTool = self.toolList[tool] @@ -118,7 +119,7 @@ def main(): pygame.init() pygame.display.init() x,y = pygame.display.list_modes()[0] - screen = pygame.display.set_mode((x,y-toolbarheight-tabheight)) + screen = pygame.display.set_mode((x, y - toolbarheight - tabheight)) # create an instance of the game game = PhysicsGame(screen) # start the main loop |