Development Team/Sugargame: Difference between revisions

add fix for keyboard focus to generate pygame keypress events
Line 42: Line 42:
==== Wrapping a Pygame program ====
==== 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.
copy the Sugargame package to it.


The activity directory should look something like this:
The activity directory should look something like this:
Line 53: Line 52:
   setup.py            - Install script
   setup.py            - Install script


To make the Activity class, start with test/TestActivity.py from the Sugargame  
To make the Activity class, start with test/TestActivity.py from the Sugargame distribution.
distribution
 
The activity should create a single PygameCanvas widget and call run_pygame on it. 
Pass the main loop function of the Pygame program.


The activity must create a single PygameCanvas widget:
  self._canvas = sugargame.canvas.PygameCanvas(self)
  self._canvas = sugargame.canvas.PygameCanvas(self)
  self.set_canvas(self._canvas)
  self.set_canvas(self._canvas)
       
 
The activity may assign keyboard focus to the PygameCanvas widget, so that keyboard events generate pygame events:
self._canvas.grab_focus()
 
The activity must call the run_pygame method of the PygameCanvas widget, passing the main loop function of the Pygame program.
  # Start the game running.
  # Start the game running.
  self._canvas.run_pygame(self.game.run)
  self._canvas.run_pygame(self.game.run)


In your Pygame main loop, pump the GTK message loop:
In your Pygame main loop, you must pump the GTK event loop:


   while gtk.events_pending():
   while gtk.events_pending():