Game development HOWTO: Difference between revisions
(Make the preferred channel more obvious) |
(better categories) |
||
(35 intermediate revisions by 16 users not shown) | |||
Line 1: | Line 1: | ||
⚫ | |||
⚫ | This document describes how to use the [[Pygame]] library for Python to create a new |
||
<< [[Tutorials]] |
|||
⚫ | This document describes how to use the [[Pygame]] library for Python for [[game development]] -- to create a new game [[activity]] for OLPC's [[Sugar]] platform. Its intention is to allow a Python programmer who wants to learn (or already knows) Pygame to integrate their Pygame application into a Sugar-hosted activity using the OLPCGames [[Pygame wrapper]]. |
||
This HOWTO is current as of December 2007. |
|||
If you are looking to create a slightly more limited Activity, you may want to check out [[Pippy]]'s Pygame capabilities. |
|||
This HOWTO is current as of December 2007. More recent notes are available at [[Porting pygame games to the XO]] (from March 2008). |
|||
= Requirements = |
= Requirements = |
||
Line 11: | Line 16: | ||
* [[Pygame]] -- this is a Python wrapper around the Simple Direct-media Layer (SDL) library. It is used for lots of games coded in Python and can run on most machines (including Windows, Mac and Linux). If you are running on an OLPC-XO, Pygame should already be available. If not, use your system's package manager to install the Pygame distribution. |
* [[Pygame]] -- this is a Python wrapper around the Simple Direct-media Layer (SDL) library. It is used for lots of games coded in Python and can run on most machines (including Windows, Mac and Linux). If you are running on an OLPC-XO, Pygame should already be available. If not, use your system's package manager to install the Pygame distribution. |
||
** The [http://dev.laptop.org/~coderanger/ slides from Noah's lecture] at the start of the game jam are online at (both PDF and PowerPoint form). |
** The [http://dev.laptop.org/~coderanger/ slides from Noah's lecture] at the start of the game jam are online at (both PDF and PowerPoint form). |
||
** [[Mac setup instructions]] |
** [[Pygame/Mac setup instructions]] |
||
* [[Pygame wrapper|OLPCGames]] -- the OLPC Sugar specific library which provides the glue code that lets your Pygame game run inside a Sugar activity. It also gives you access to the various "special" features in the Sugar environment, such as the mesh network and the camera. If you are on an OLPC-XO, you can download the [http://dev.laptop.org/ |
* [[Pygame wrapper|OLPCGames]] -- the OLPC Sugar specific library which provides the glue code that lets your Pygame game run inside a Sugar activity. It also gives you access to the various "special" features in the Sugar environment, such as the mesh network and the camera. If you are on an OLPC-XO, you can download the [http://dev.laptop.org/~mcfletch/OLPCGames/ current OLPCGames distribution] and unpack it. |
||
** Note: The OLPCGames Pygame wrapper requires at least build 432 to work for version 1.0 and at least an Update.2 build (649) for version 1.1 and above. See the reference manual at [[Pygame wrapper]]. See also [[Game development]]. |
** ''Note'': The OLPCGames Pygame wrapper requires at least build 432 to work for version 1.0 and at least an Update.2 build (649) for version 1.1 and above. See the reference manual at [[Pygame wrapper]]. See also [[Game development]]. |
||
== Environment == |
== Environment == |
||
You will need a working Sugar [[Developers]] environment. If you are working directly on an OLPC-XO, you will need to know how to use a standard text editor, such as vi or nano, which are available within the |
You will need a working Sugar [[Developers/Setup|Developer's]] environment. If you are working directly on an OLPC-XO, you will need to know how to use a standard text editor, such as vi or nano, which are available within the [[Terminal Activity]] in your activity toolbar. |
||
* You'll need to set aside a few hours to [http://www.eng.hawaii.edu/Tutor/vi.html learn vi] before you start this HOWTO if you don't already know it and want to use it well) |
* You'll need to set aside a few hours to [http://www.eng.hawaii.edu/Tutor/vi.html learn vi] before you start this HOWTO if you don't already know it and want to use it well) |
||
* Nano is often considered easier to learn immediately because the major commands are all spelled out at the bottom of the screen (where you need to remember the vim commands yourself) |
* Nano is often considered easier to learn immediately because the major commands are all spelled out at the bottom of the screen (where you need to remember the vim commands yourself) |
||
Line 25: | Line 30: | ||
= Skeleton Setup = |
= Skeleton Setup = |
||
To start, you will likely want to download the [http://dev.laptop.org/ |
To start, you will likely want to download the [http://dev.laptop.org/~mcfletch/OLPCGames/ OLPCGames source package]. This package includes a skeleton script that lets you generate a new OLPCGames-based Pygame Activity with a single command. |
||
== Getting the Skeleton Script == |
== Getting the Skeleton Script == |
||
Line 31: | Line 36: | ||
To install the package, you will need to download the .zip or .tar.gz to your machine and extract it with either of: |
To install the package, you will need to download the .zip or .tar.gz to your machine and extract it with either of: |
||
unzip OLPCGames-1. |
unzip OLPCGames-1.4.zip |
||
or |
or |
||
tar -zxf OLPCGames-1. |
tar -zxf OLPCGames-1.4.tar.gz |
||
which will create a directory named OLPCGames-1. |
which will create a directory named OLPCGames-1.4. Change to the skeleton directory: |
||
cd OLPCGames-1.4/skeleton |
|||
Make sure that your python file has the required permissions to be used. |
|||
chmod a+x buildskel.py |
|||
And run the command: |
|||
./buildskel.py activityname "My Activity Name" |
./buildskel.py activityname "My Activity Name" |
||
to create a new activity instance. |
to create a new generic activity instance. |
||
== Installing and Testing == |
== Installing and Testing == |
||
Line 49: | Line 62: | ||
python setup.py dev |
python setup.py dev |
||
when you restart Sugar you should have a new activity in your Activity bar named "My Activity Name". Clicking on this activity should result in dark blue screen with a toolbar at the top of the window. |
when you restart Sugar you should have a new activity in your Activity bar named "My Activity Name". Clicking on this activity should result in dark blue screen with a toolbar at the top of the window. Type Esc to exit. |
||
== Testing Outside Sugar == |
== Testing Outside Sugar == |
||
The run.py script in the skeleton project is where you skeleton activity currently points for its "mainloop", particularly the "main" function within it. When you are just starting |
The run.py script in the skeleton project is where you skeleton activity currently points for its "mainloop", particularly the "main" function within it. When you are just starting you'll likely want to work within run.py to create new code and experiment. run.py is actually set up to be used as a python script via: |
||
python run.py |
python run.py |
||
Line 59: | Line 72: | ||
which will run on a non-Sugar environment (i.e. a normal Linux, Windows or Mac desktop with Pygame installed). You may, however, have to configure your system to have the current working directory in the Python path (this is the default on Sugar systems, including emulators and sugar-jhbuild shells). |
which will run on a non-Sugar environment (i.e. a normal Linux, Windows or Mac desktop with Pygame installed). You may, however, have to configure your system to have the current working directory in the Python path (this is the default on Sugar systems, including emulators and sugar-jhbuild shells). |
||
== |
== Customizing the Skeleton == |
||
Your Sugar-specific activity values are stored in two main locations; the activity.py file and the activity directory. The pydoc for the [http://www.vrplumber.com/sugar-docs/olpcgames.activity.html#PyGameActivity PyGameActivity class] describes the various attributes/settings available for your Activity object. These include changing the file-name and method-name for your mainloop function, and changing the title of your activity. |
Your Sugar-specific activity values are stored in two main locations; the '''activity.py''' file and the activity directory. The pydoc for the [http://www.vrplumber.com/sugar-docs/olpcgames.activity.html#PyGameActivity PyGameActivity class] describes the various attributes/settings available for your Activity object. These include changing the file-name and method-name for your mainloop function, and changing the title of your activity. |
||
The activity directory is used by Sugar to find things such as your svg icon, translated names and the like. See [[Activity Bundles]] for details. |
The activity directory is used by Sugar to find things such as your svg icon, translated names and the like. See [[Activity Bundles]] for details. |
||
Line 67: | Line 80: | ||
= Getting Started with Pygame = |
= Getting Started with Pygame = |
||
At this point, your OLPC Sugar activity is running as a host for a simple Pygame event loop. |
At this point, your OLPC Sugar activity is running as a host for a simple Pygame event loop. You should now, largely, be able to use standard Pygame code to produce graphics, play sounds, and process input. |
||
== Pygame Examples and Tutorials == |
|||
⚫ | |||
Example Activities: |
|||
⚫ | |||
* [http://dev.laptop.org/git?p=projects/games-misc;a=blob;f=FileTest.activity/run.py;hb=HEAD Journal/File Test] -- example of using the [[Journal]] to save/restore state for an activity |
|||
* [http://dev.laptop.org/git?p=projects/games-misc;a=blob;f=cameratest.activity/run.py;hb=HEAD Camera Test] -- example of using the <code>olpcgames.camera</code> module [http://dev.laptop.org/~mcfletch/testactivities/cameratest-1.xo XO] |
|||
* [http://dev.laptop.org/git?p=projects/games-misc;a=blob;f=soundtest.activity/run.py;hb=HEAD Sound Test] -- example showing simple multi-channel sound usage [http://dev.laptop.org/~mcfletch/testactivities/soundtest-1.xo XO] |
|||
* [http://dev.laptop.org/git?p=projects/games-misc;a=blob;f=svgspritetest.activity/run.py;hb=HEAD SVG Sprite Test] -- example showing use of the <code>olpcgames.svgsprite</code> module [http://dev.laptop.org/~mcfletch/testactivities/svgspritetest-1.xo XO] |
|||
⚫ | |||
* [http://dev.laptop.org/git?p=projects/games-misc;a=blob;f=videotest.activity/run.py;hb=HEAD Video Test] -- example showing use of the [[GStreamer]]-based <code>olpcgames.video</code> module [http://dev.laptop.org/~mcfletch/testactivities/videotest-2.xo XO] |
|||
⚫ | |||
⚫ | |||
''Tutorials'': |
|||
* [http://www.pygame.org/wiki/tutorials Pygame Tutorials] -- a wiki-based collection of tutorials for learning Pygame programming |
* [http://www.pygame.org/wiki/tutorials Pygame Tutorials] -- a wiki-based collection of tutorials for learning Pygame programming |
||
* [http://rene.f0o.com/mywiki/PythonGameProgramming 5-part Tutorial] -- a fairly extensive tutorial on Pygame usage |
|||
* [http://www.pygame.org/docs Pygame Documentation] -- the official collection of Pygame documentation, you will need this to get any Pygame programming done |
* [http://www.pygame.org/docs Pygame Documentation] -- the official collection of Pygame documentation, you will need this to get any Pygame programming done |
||
* [[Game templates]] -- serves as a starting point for creating Pygame games |
|||
⚫ | |||
*[[OLPCGames#Activities|OLPCGames-based Activities]] can often be read to find sample code. |
|||
⚫ | |||
The following Games/Activities use OLPCGames and can serve as example code (note that these projects are not yet finished): |
|||
⚫ | |||
* [[Story Builder]] -- Environment for creating story modules to be used in [[MaMaMedia]], uses the PGU GUI library extensively |
|||
⚫ | |||
* [[Games/Productive|Productive]] -- A Real-time Strategy game written explicitly for the OLPC platform. Includes networking via the mesh module (and raw Telepathy primitives). Graphics are via raw Pygame coding. |
|||
= Support = |
|||
If you have questions, suggestions or problems, please feel free to post to the [http://lists.laptop.org/listinfo/games OLPC Game Development mailing list]. This is a relatively low-traffic list with lots of Pygame users on it. Alternately, IRC channels are available on freenode as #pygame, #sugar and #olpc-content if you want more conversational support. Lastly, [[User:Mcfletch]] is the current maintainer of the wrapper. Contact him if you get stuck, but be aware he tends to be spread a bit thin, the mailing list is generally a better avenue. |
If you have questions, suggestions or problems, please feel free to post to the [http://lists.laptop.org/listinfo/games OLPC Game Development mailing list]. This is a relatively low-traffic list with lots of Pygame users on it. Alternately, IRC channels are available on freenode as #pygame, #sugar and #olpc-content if you want more conversational support. Lastly, [[User:Mcfletch]] is the current maintainer of the wrapper. Contact him if you get stuck, but be aware he tends to be spread a bit thin, the mailing list is generally a better avenue. |
||
= Reducing CPU Load = |
|||
The code in run.py does some trickery to make the event loop reasonably efficient, by limiting the number of frames rendered per second using a "pygame.time.Clock()" instance. It also uses a complex iteration mechanism: |
|||
events = pausescreen.get_events() |
|||
for event in events: |
|||
which allows your activity to go completely quiet if there are no pending events for a given time, but still processes all pending events in a timely manner. |
|||
⚫ | |||
⚫ | |||
* Read more about [[Monitoring System Load]] |
|||
== Eliminating Mouse-move Events == |
|||
If your activity does not use MOUSEMOTION events it is possible to reduce the overall number of events processed (you can combine this with using a <code>pygame.event.wait()</code> event loop as well. Keep in mind that with this optimization you cannot do mouse-over highlighting or the like. |
|||
An example code structure might look like this: |
|||
import sys |
|||
import pygame |
|||
from pygame.locals import * |
|||
def main(): |
|||
window = pygame.display.set_mode((400, 225)) |
|||
pygame.event.set_blocked(MOUSEMOTION) |
|||
pygame.init() |
|||
while True: |
|||
⚫ | |||
print event |
|||
if event.type == KEYUP: |
|||
# Quit on 'q' |
|||
if event.key == 113: sys.exit(0) |
|||
if __name__=="__main__": |
|||
main() |
|||
== Extending PyGame with C++ == |
|||
See [[Extending PyGame with C++]] for instructions on how to mix Python and C++ code for better performance. |
|||
=Troubleshooting= |
=Troubleshooting= |
||
Line 95: | Line 158: | ||
Check your log files. On modern Sugar, use the Log Viewer activity to view the log for your activity. Open this activity and find your activity in the list of activity instances on the left. The numeric suffixes increase as you run your activity multiple times. |
Check your log files. On modern Sugar, use the Log Viewer activity to view the log for your activity. Open this activity and find your activity in the list of activity instances on the left. The numeric suffixes increase as you run your activity multiple times. |
||
[[Category: |
[[Category:Python]] |
||
[[Category:HowTo]] |
[[Category:HowTo]] |
||
[[Category: |
[[Category:Developing games]] |
||
⚫ |
Latest revision as of 10:39, 20 November 2008
<< Tutorials
This document describes how to use the Pygame library for Python for game development -- to create a new game activity for OLPC's Sugar platform. Its intention is to allow a Python programmer who wants to learn (or already knows) Pygame to integrate their Pygame application into a Sugar-hosted activity using the OLPCGames Pygame wrapper.
If you are looking to create a slightly more limited Activity, you may want to check out Pippy's Pygame capabilities.
This HOWTO is current as of December 2007. More recent notes are available at Porting pygame games to the XO (from March 2008).
Requirements
This HOWTO assumes that you know the basics of computer programming, how to navigate a file-system, and how to edit files on your machine. It also assumes that you will largely learn Pygame programming through the large number of available Pygame references and tutorials. We focus here on how to integrate your Pygame games into the Sugar environment.
Components
- Pygame -- this is a Python wrapper around the Simple Direct-media Layer (SDL) library. It is used for lots of games coded in Python and can run on most machines (including Windows, Mac and Linux). If you are running on an OLPC-XO, Pygame should already be available. If not, use your system's package manager to install the Pygame distribution.
- The slides from Noah's lecture at the start of the game jam are online at (both PDF and PowerPoint form).
- Pygame/Mac setup instructions
- OLPCGames -- the OLPC Sugar specific library which provides the glue code that lets your Pygame game run inside a Sugar activity. It also gives you access to the various "special" features in the Sugar environment, such as the mesh network and the camera. If you are on an OLPC-XO, you can download the current OLPCGames distribution and unpack it.
- Note: The OLPCGames Pygame wrapper requires at least build 432 to work for version 1.0 and at least an Update.2 build (649) for version 1.1 and above. See the reference manual at Pygame wrapper. See also Game development.
Environment
You will need a working Sugar Developer's environment. If you are working directly on an OLPC-XO, you will need to know how to use a standard text editor, such as vi or nano, which are available within the Terminal Activity in your activity toolbar.
- You'll need to set aside a few hours to learn vi before you start this HOWTO if you don't already know it and want to use it well)
- Nano is often considered easier to learn immediately because the major commands are all spelled out at the bottom of the screen (where you need to remember the vim commands yourself)
If you are working in an emulated environment, or a sugar-jhbuild environment, you can use whatever text editor you prefer to create the files we will be working on. There are many text editors with some Python support, and full IDEs are also available.
Skeleton Setup
To start, you will likely want to download the OLPCGames source package. This package includes a skeleton script that lets you generate a new OLPCGames-based Pygame Activity with a single command.
Getting the Skeleton Script
To install the package, you will need to download the .zip or .tar.gz to your machine and extract it with either of:
unzip OLPCGames-1.4.zip
or
tar -zxf OLPCGames-1.4.tar.gz
which will create a directory named OLPCGames-1.4. Change to the skeleton directory:
cd OLPCGames-1.4/skeleton
Make sure that your python file has the required permissions to be used.
chmod a+x buildskel.py
And run the command:
./buildskel.py activityname "My Activity Name"
to create a new generic activity instance.
Installing and Testing
To test that you have your environment properly configured, we'll restart sugar and attempt to run the newly created (empty) activity. Change to the new activity directory (activityname.activity) and run:
python setup.py dev
when you restart Sugar you should have a new activity in your Activity bar named "My Activity Name". Clicking on this activity should result in dark blue screen with a toolbar at the top of the window. Type Esc to exit.
Testing Outside Sugar
The run.py script in the skeleton project is where you skeleton activity currently points for its "mainloop", particularly the "main" function within it. When you are just starting you'll likely want to work within run.py to create new code and experiment. run.py is actually set up to be used as a python script via:
python run.py
which will run on a non-Sugar environment (i.e. a normal Linux, Windows or Mac desktop with Pygame installed). You may, however, have to configure your system to have the current working directory in the Python path (this is the default on Sugar systems, including emulators and sugar-jhbuild shells).
Customizing the Skeleton
Your Sugar-specific activity values are stored in two main locations; the activity.py file and the activity directory. The pydoc for the PyGameActivity class describes the various attributes/settings available for your Activity object. These include changing the file-name and method-name for your mainloop function, and changing the title of your activity.
The activity directory is used by Sugar to find things such as your svg icon, translated names and the like. See Activity Bundles for details.
Getting Started with Pygame
At this point, your OLPC Sugar activity is running as a host for a simple Pygame event loop. You should now, largely, be able to use standard Pygame code to produce graphics, play sounds, and process input.
Pygame Examples and Tutorials
Example Activities:
- Journal/File Test -- example of using the Journal to save/restore state for an activity
- Camera Test -- example of using the
olpcgames.camera
module XO - Sound Test -- example showing simple multi-channel sound usage XO
- SVG Sprite Test -- example showing use of the
olpcgames.svgsprite
module XO
- Video Test -- example showing use of the GStreamer-based
olpcgames.video
module XO
Tutorials:
- Pygame Tutorials -- a wiki-based collection of tutorials for learning Pygame programming
- 5-part Tutorial -- a fairly extensive tutorial on Pygame usage
- Pygame Documentation -- the official collection of Pygame documentation, you will need this to get any Pygame programming done
- Game templates -- serves as a starting point for creating Pygame games
- OLPCGames-based Activities can often be read to find sample code.
Reference Links
- Pygame wrapper pydoc -- the OLPCGames wrapper's reference manual and pydoc, you'll want to familiarize yourself with this to understand what's different on the OLPC platform from regular Pygame
- If you are new to game and GUI programming, you may wish to use a Pygame GUI engine to simplify creating buttons, text entry boxes and the like.
Support
If you have questions, suggestions or problems, please feel free to post to the OLPC Game Development mailing list. This is a relatively low-traffic list with lots of Pygame users on it. Alternately, IRC channels are available on freenode as #pygame, #sugar and #olpc-content if you want more conversational support. Lastly, User:Mcfletch is the current maintainer of the wrapper. Contact him if you get stuck, but be aware he tends to be spread a bit thin, the mailing list is generally a better avenue.
Reducing CPU Load
The code in run.py does some trickery to make the event loop reasonably efficient, by limiting the number of frames rendered per second using a "pygame.time.Clock()" instance. It also uses a complex iteration mechanism:
events = pausescreen.get_events() for event in events:
which allows your activity to go completely quiet if there are no pending events for a given time, but still processes all pending events in a timely manner.
You can see the code that implements this in the olpcgames.pausescreen module. None of that machinery is OLPC or Sugar specific, incidentally, it's just good practice to reduce your processing load when running on an OLPC machine.
- Note: the event iteration mechanism reduces the cpu-load from 99% to 0.7 - 4% in our tests versus a simple pygame.event.get() loop.
- Read more about Monitoring System Load
Eliminating Mouse-move Events
If your activity does not use MOUSEMOTION events it is possible to reduce the overall number of events processed (you can combine this with using a pygame.event.wait()
event loop as well. Keep in mind that with this optimization you cannot do mouse-over highlighting or the like.
An example code structure might look like this:
import sys import pygame from pygame.locals import * def main(): window = pygame.display.set_mode((400, 225)) pygame.event.set_blocked(MOUSEMOTION) pygame.init() while True: for event in [ pygame.event.wait() ] + pygame.event.get( ): print event if event.type == KEYUP: # Quit on 'q' if event.key == 113: sys.exit(0) if __name__=="__main__": main()
Extending PyGame with C++
See Extending PyGame with C++ for instructions on how to mix Python and C++ code for better performance.
Troubleshooting
Ensure you are using at least an Update.2 (first manufacturing release) Sugar environment. Also ensure you are using a recent version of OLPCGames.
Check your log files. On modern Sugar, use the Log Viewer activity to view the log for your activity. Open this activity and find your activity in the list of activity instances on the left. The numeric suffixes increase as you run your activity multiple times.