Difference between revisions of "Development Team/Sugargame"

From Sugar Labs
Jump to navigation Jump to search
 
(23 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
== Sugargame ==
 
== 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]  
It is fork of the olcpgames framework, which is no longer maintained.
+
programs to run well under Sugar.
 
+
It is a fork of olcpgames, 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 13: Line 11:
 
* Provides hooks for saving to and restoring from the Journal
 
* Provides hooks for saving to and restoring from the Journal
  
=== Differences between Sugargame and olpcgames ===
+
Get it from:
  
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.
+
http://github.com/sugarlabs/sugargame
  
Sugargame takes a simpler approach; it simply offers a way to embed Pygame into a GTK widgetThe Sugar APIs are used to interact with Sugar, the Pygame APIs are used for the game.
+
And place it within your activity sourceIt is not part of Sugar.  Remember to check back for updates when you are continuing development of your activity.
  
Sugargame advantages:
+
You can find it in some activities already.  It is so small that the duplication is unimportant.
  
* Simpler code
+
== Using Sugargame ==
* More elegant interface between Pygame and GTK
 
* Runs as a single thread: no thread related segfaults
 
* Possible to use Sugar widgets with Pygame
 
  
Sugargame limitations:
+
See also [[Development Team/Sugargame/Examples]].
  
* No support for Pango or SVG sprites (yet)
+
==== Wrapping a Pygame program ====
 
 
== Using Sugargame ==
 
 
=== Porting 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 into it. For an example, see the directory named test, inside the Sugargame repository.  It is in fact a Sugargame activity.  
  
 
The activity directory should look something like this:
 
The activity directory should look something like this:
Line 44: Line 35:
 
   setup.py            - Install script
 
   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.
 
  
 +
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():
 
       gtk.main_iteration()
 
       gtk.main_iteration()
  
=== Adding Pygame to an activity ===
+
==== Adding Pygame to a PyGTK 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
 +
                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() 
 +
           
 +
            # Try to stay at 30 FPS
 +
            self.clock.tick(30)
  
 
== Support ==
 
== Support ==
Line 68: Line 105:
  
 
: sugar-devel@lists.sugarlabs.org
 
: sugar-devel@lists.sugarlabs.org
 
== Author ==
 
  
 
Sugargame is developed by Wade Brainerd <wadetb@gmail.com>.   
 
Sugargame is developed by Wade Brainerd <wadetb@gmail.com>.   
  
 
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.
+
the One Laptop per Child project.
 +
 
 +
== Changelog ==
 +
 
 +
===v1.1===
 +
* Fix bugs in event handling.  (Pablo Moleri)
 +
* Remove reference to gtk.Socket.get_window() method, which is missing in older versions of PyGTK.
 +
 
 +
===v1.0===
 +
* Initial version of Sugargame
 +
 
 +
== 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
 +
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 advantages:
 +
 
 +
* Simpler code
 +
* More elegant interface between Pygame and GTK
 +
* Runs as a single thread: no thread related segfaults
 +
* Possible to use Sugar widgets with Pygame
 +
 
 +
Sugargame limitations:
 +
 
 +
* No support for Pango or SVG sprites (yet)

Latest revision as of 02:06, 12 July 2017

Sugargame

Sugargame is a Python package which allows Pygame programs to run well under Sugar. It is a fork of olcpgames, which is no longer maintained.

What it does:

  • Wraps a Sugar activity around an existing Pygame program with few changes
  • Allows Sugar toolbars and other widgets to be added to the activity UI
  • Provides hooks for saving to and restoring from the Journal

Get it from:

http://github.com/sugarlabs/sugargame

And place it within your activity source. It is not part of Sugar. Remember to check back for updates when you are continuing development of your activity.

You can find it in some activities already. It is so small that the duplication is unimportant.

Using Sugargame

See also Development Team/Sugargame/Examples.

Wrapping a Pygame program

To use Sugargame to Sugarize a Pygame program, set up an activity directory and copy the Sugargame package into it. For an example, see the directory named test, inside the Sugargame repository. It is in fact a Sugargame activity.

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.

The activity must create a single PygameCanvas widget:

self._canvas = sugargame.canvas.PygameCanvas(self)
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.
self._canvas.run_pygame(self.game.run)

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

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

Adding Pygame to a PyGTK activity

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
               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()  
            
           # Try to stay at 30 FPS
           self.clock.tick(30)

Support

For help with Sugargame, please email the Sugar Labs development list:

sugar-devel@lists.sugarlabs.org

Sugargame is developed by Wade Brainerd <wadetb@gmail.com>.

It is loosely based on the source code to the olpcgames framework, developed by the One Laptop per Child project.

Changelog

v1.1

  • Fix bugs in event handling. (Pablo Moleri)
  • Remove reference to gtk.Socket.get_window() method, which is missing in older versions of PyGTK.

v1.0

  • Initial version of Sugargame

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 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 advantages:

  • Simpler code
  • More elegant interface between Pygame and GTK
  • Runs as a single thread: no thread related segfaults
  • Possible to use Sugar widgets with Pygame

Sugargame limitations:

  • No support for Pango or SVG sprites (yet)