Development Team/Sugargame: Difference between revisions

Wade (talk | contribs)
Wade (talk | contribs)
No edit summary
Line 3: Line 3:
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.
It is fork of the olcpgames framework, which is no longer maintained.
Sugargame embeds the Pygame window into a GTK window, and translates GTK
events to Pygame events.


What it does:
What it does:
Line 17: Line 14:
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.
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 simply offers 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:
Sugargame advantages:
Line 32: Line 29:
== Using Sugargame ==
== Using Sugargame ==
   
   
=== Porting a Pygame program ===
=== Wrapping a Pygame program ===


To use Sugargame to Sugarize a Pygame program, set up an activity directory and copy the Sugargame package to it.
To use Sugargame to Sugarize a Pygame program, set up an activity directory and copy the Sugargame package to it.
Line 46: Line 43:
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.
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._canvas = sugargame.canvas.PygameCanvas(self)
Line 61: Line 58:
=== Adding Pygame to an activity ===
=== Adding Pygame to an activity ===


TODO
To add Pygame to an existing Sugar activity, create a PygameCanvas widget and call 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 entire activity.
 
The argument to run_pygame is a function structured like a Pygame program.  In the 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
            # Clear Display
            screen.fill((255,255,255)) #255 for white
            # Draw stuff here
            .................
            # Flip Display
            pygame.display.flip() 
           
            # Try to stay at 30 FPS
            self.clock.tick(30)


== Support ==
== Support ==