From b091432a4cbbfbf8e899145ee9d3dbfe3fce91e1 Mon Sep 17 00:00:00 2001 From: Manuel QuiƱones Date: Mon, 30 Sep 2013 15:37:00 +0000 Subject: Remove trailing spaces --- diff --git a/README.txt b/README.txt index c0ac366..e26f309 100644 --- a/README.txt +++ b/README.txt @@ -1,7 +1,7 @@ == Sugargame == -Sugargame is a Python package which allows [http://www.pygame.org/ Pygame] -programs to run well under Sugar. +Sugargame is a Python package which allows [http://www.pygame.org/ Pygame] +programs to run well under Sugar. It is fork of the olcpgames framework, which is no longer maintained. http://git.sugarlabs.org/projects/sugargame @@ -14,15 +14,15 @@ What it does: ==== Differences between Sugargame and olpcgames ==== -The olpcgames framework provides a wrapper around Pygame which attempts to -allow a Pygame program to run mostly unmodified under Sugar. To this end, -the Pygame program is run in a separate thread with its own Pygame message -loop while the main thread runs the GTK message loop. Also, olpcgames wraps +The olpcgames framework provides a wrapper around Pygame which attempts to +allow a Pygame program to run mostly unmodified under Sugar. To this end, +the Pygame program is run in a separate thread with its own Pygame message +loop while the main thread runs the GTK message loop. Also, olpcgames wraps Sugar APIs such as the journal and mesh into a Pygame-like API. -Sugargame takes a simpler approach; it provides a way to embed Pygame into a -GTK widget. The Sugar APIs are used to interact with Sugar, the Pygame APIs -are used for the game. +Sugargame takes a simpler approach; it provides a way to embed Pygame into a +GTK widget. The Sugar APIs are used to interact with Sugar, the Pygame APIs +are used for the game. Sugargame advantages: @@ -41,26 +41,26 @@ See also [[Development Team/Sugargame/Examples]]. ==== Wrapping a Pygame program ==== -To use Sugargame to Sugarize a Pygame program, set up an activity directory and +To use Sugargame to Sugarize a Pygame program, set up an activity directory and copy the Sugargame package to it. The activity directory should look something like this: - + activity/ - Activity directory: activity.info, SVG icon, etc. sugargame/ - Sugargame package MyActivity.py - Activity class mygame.py - Pygame code setup.py - Install script -To make the Activity class, start with test/TestActivity.py from the Sugargame -distribution. +To make the Activity class, start with test/TestActivity.py from the Sugargame +distribution. The activity should create a single PygameCanvas widget and call run_pygame on it. Pass the main loop function of the Pygame program. self._canvas = sugargame.canvas.PygameCanvas(self) self.set_canvas(self._canvas) - + # Start the game running. self._canvas.run_pygame(self.game.run) @@ -72,14 +72,14 @@ In your Pygame main loop, pump the GTK message loop: ==== Adding Pygame to a PyGTK activity ==== To add Pygame to an existing Sugar activity, create a PygameCanvas widget and call -run_pygame on it. +run_pygame on it. widget = sugargame.canvas.PygameCanvas(self) vbox.pack_start(widget) - + widget.run_pygame(self.game.run) -Due to limitations of Pygame and SDL, there can only be one PygameCanvas in the +Due to limitations of Pygame and SDL, there can only be one PygameCanvas in the entire activity. The argument to run_pygame is a function structured like a Pygame program. In the @@ -88,31 +88,31 @@ main loop, remember to dispatch GTK messages using gtk.main_iteration(). def main_loop(): clock = pygame.time.Clock() screen = pygame.display.get_surface() - + while self.running: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() - + # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.VIDEORESIZE: pygame.display.set_mode(event.size, pygame.RESIZABLE) - + # Check the mouse position x, y = pygame.mouse.get_pos() - + # Clear Display screen.fill((255,255,255)) #255 for white - + # Draw stuff here ................. - + # Flip Display - pygame.display.flip() - + pygame.display.flip() + # Try to stay at 30 FPS self.clock.tick(30) @@ -122,9 +122,9 @@ For help with Sugargame, please email the Sugar Labs development list: : sugar-devel@lists.sugarlabs.org -Sugargame is developed by Wade Brainerd . +Sugargame is developed by Wade Brainerd . -It is loosely based on the source code to the olpcgames framework, developed by +It is loosely based on the source code to the olpcgames framework, developed by the One Laptop Per Child project. === Changelog === diff --git a/sugargame/canvas.py b/sugargame/canvas.py index 1ce0250..0db4220 100644 --- a/sugargame/canvas.py +++ b/sugargame/canvas.py @@ -7,7 +7,7 @@ import event CANVAS = None class PygameCanvas(Gtk.EventBox): - + """ mainwindow is the activity intself. """ @@ -20,11 +20,11 @@ class PygameCanvas(Gtk.EventBox): # Initialize Events translator before widget gets "realized". self.translator = event.Translator(mainwindow, self) - + self._mainwindow = mainwindow self.set_can_focus(True) - + self._socket = Gtk.Socket() self.add(self._socket) self.show_all() @@ -32,18 +32,18 @@ class PygameCanvas(Gtk.EventBox): def run_pygame(self, main_fn): # Run the main loop after a short delay. The reason for the delay is that the # Sugar activity is not properly created until after its constructor returns. - # If the Pygame main loop is called from the activity constructor, the + # If the Pygame main loop is called from the activity constructor, the # constructor never returns and the activity freezes. GObject.idle_add(self._run_pygame_cb, main_fn) def _run_pygame_cb(self, main_fn): assert pygame.display.get_surface() is None, "PygameCanvas.run_pygame can only be called once." - + # Preinitialize Pygame with the X window ID. assert pygame.display.get_init() == False, "Pygame must not be initialized before calling PygameCanvas.run_pygame." os.environ['SDL_WINDOWID'] = str(self._socket.get_id()) pygame.init() - + # Restore the default cursor. self._socket.props.window.set_cursor(None) diff --git a/sugargame/event.py b/sugargame/event.py index 431a600..8cd0ee9 100644 --- a/sugargame/event.py +++ b/sugargame/event.py @@ -3,7 +3,7 @@ from gi.repository import Gdk from gi.repository import GObject import pygame import pygame.event -import logging +import logging class _MockEvent(object): def __init__(self, keyval): @@ -29,7 +29,7 @@ class Translator(object): 'KP_Right' : pygame.K_KP6, } - + mod_map = { pygame.K_LALT: pygame.KMOD_LALT, pygame.K_RALT: pygame.KMOD_RALT, @@ -38,7 +38,7 @@ class Translator(object): pygame.K_LSHIFT: pygame.KMOD_LSHIFT, pygame.K_RSHIFT: pygame.KMOD_RSHIFT, } - + def __init__(self, mainwindow, inner_evb): """Initialise the Translator with the windows to which to listen""" self._mainwindow = mainwindow @@ -50,7 +50,7 @@ class Translator(object): Gdk.EventMask.KEY_PRESS_MASK | \ Gdk.EventMask.KEY_RELEASE_MASK \ ) - + self._inner_evb.set_events( Gdk.EventMask.POINTER_MOTION_MASK | \ Gdk.EventMask.POINTER_MOTION_HINT_MASK | \ @@ -61,7 +61,7 @@ class Translator(object): self._mainwindow.set_can_focus(True) self._inner_evb.set_can_focus(True) - + # Callback functions to link the event systems self._mainwindow.connect('unrealize', self._quit_cb) self._inner_evb.connect('key_press_event', self._keydown_cb) @@ -71,7 +71,7 @@ class Translator(object): self._inner_evb.connect('motion-notify-event', self._mousemove_cb) self._inner_evb.connect('draw', self._draw_cb) self._inner_evb.connect('configure-event', self._resize_cb) - + # Internal data self.__stopped = False self.__keystate = [0] * 323 @@ -88,7 +88,7 @@ class Translator(object): pygame.key.set_repeat = self._set_repeat pygame.mouse.get_pressed = self._get_mouse_pressed pygame.mouse.get_pos = self._get_mouse_pos - + def _draw_cb(self, widget, event): if pygame.display.get_init(): pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE)) @@ -113,9 +113,9 @@ class Translator(object): self.__held_last_time[key] = pygame.time.get_ticks() self.__held_time_left[key] = self.__repeat[0] self.__held.add(key) - + return self._keyevent(widget, event, pygame.KEYDOWN) - + def _keyup_cb(self, widget, event): key = event.keyval if self.__repeat[0] is not None: @@ -126,19 +126,19 @@ class Translator(object): self.__held.discard(key) return self._keyevent(widget, event, pygame.KEYUP) - + def _keymods(self): mod = 0 for key_val, mod_val in self.mod_map.iteritems(): mod |= self.__keystate[key_val] and mod_val return mod - + def _keyevent(self, widget, event, type): key = Gdk.keyval_name(event.keyval) if key is None: # No idea what this key is. return False - + keycode = None if key in self.key_trans: keycode = self.key_trans[key] @@ -151,7 +151,7 @@ class Translator(object): self._mainwindow.view_source() else: print 'Key %s unrecognized' % key - + if keycode is not None: if type == pygame.KEYDOWN: mod = self._keymods() @@ -163,7 +163,7 @@ class Translator(object): ukey = '' evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod) self._post(evt) - + return True def _get_pressed(self): @@ -179,12 +179,12 @@ class Translator(object): def _mouseup_cb(self, widget, event): self.__button_state[event.button-1] = 0 return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP) - + def _mouseevent(self, widget, event, type): evt = pygame.event.Event(type, button=event.button, pos=(event.x, event.y)) self._post(evt) return True - + def _mousemove_cb(self, widget, event): # From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/ # if this is a hint, then let's get all the necessary @@ -198,38 +198,38 @@ class Translator(object): rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1]) self.__mouse_pos = (x, y) - + self.__button_state = [ state & Gdk.ModifierType.BUTTON1_MASK and 1 or 0, state & Gdk.ModifierType.BUTTON2_MASK and 1 or 0, state & Gdk.ModifierType.BUTTON3_MASK and 1 or 0, ] - + evt = pygame.event.Event(pygame.MOUSEMOTION, pos=self.__mouse_pos, rel=rel, buttons=self.__button_state) self._post(evt) return True - + def _tick_cb(self): cur_time = pygame.time.get_ticks() for key in self.__held: delta = cur_time - self.__held_last_time[key] self.__held_last_time[key] = cur_time - + self.__held_time_left[key] -= delta if self.__held_time_left[key] <= 0: self.__held_time_left[key] = self.__repeat[1] self._keyevent(None, _MockEvent(key), pygame.KEYDOWN) - + return True - + def _set_repeat(self, delay=None, interval=None): if delay is not None and self.__repeat[0] is None: self.__tick_id = GObject.timeout_add(10, self._tick_cb) elif delay is None and self.__repeat[0] is not None: GObject.source_remove(self.__tick_id) self.__repeat = (delay, interval) - + def _get_mouse_pos(self): return self.__mouse_pos -- cgit v0.9.1