Game development HOWTO: Difference between revisions

From OLPC
Jump to navigation Jump to search
m (16bpp)
(Beginning of rework to cover approach to getting started quickly)
Line 1: Line 1:
This document describes how to use the [[Pygame]] library for Python to create a new Game activity for the OLPC ([[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.
This HOWTO is current as of December 2007.


= Requirements =
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. See the reference manual at [[Pygame wrapper]]. See also [[Game development]].


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.
=Crash Course on Pygame=
The slides from Noah's lecture at the start of the game jam are online at http://dev.laptop.org/~coderanger/ (both PDF and PowerPoint form).


== Components ==
=Development Environment=


* [[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.
Start with [[Pygame]]. If you are running Mac OS X, check out the [[Mac setup instructions]].
** 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 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/git?p=projects/games-misc;a=tree;f=olpcgames-src/dist;hb=HEAD 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. See the reference manual at [[Pygame wrapper]]. See also [[Game development]].

== Environment ==

You will need a working Sugar [[Development Environment|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 "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)
* 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 [http://dev.laptop.org/git?p=projects/games-misc;a=tree;f=olpcgames-src/dist;hb=HEAD OLPCGames 1.2 source package]. This package includes a skeleton package 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.2.zip

or

tar -zxf OLPCGames-1.2.tar.gz

which will create a directory named OLPCGames-1.2. Change to the OLPCGames-1.2/skeleton directory and run the command:

./buildskel.py activityname "My Activity Name"

to create a new 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.

== 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 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).

=Development Environment=


You don't need an XO laptop for simple Pygame development. When creating your Pygame game, use this new boilerplate:
You don't need an XO laptop for simple Pygame development. When creating your Pygame game, use this new boilerplate:

Revision as of 19:58, 10 December 2007

This document describes how to use the Pygame library for Python to create a new Game activity for the OLPC (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.

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.
  • 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. See the reference manual at Pygame wrapper. See also Game development.

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 "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 1.2 source package. This package includes a skeleton package 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.2.zip

or

tar -zxf OLPCGames-1.2.tar.gz

which will create a directory named OLPCGames-1.2. Change to the OLPCGames-1.2/skeleton directory and run the command:

./buildskel.py activityname "My Activity Name"

to create a new 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.

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 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).

Development Environment

You don't need an XO laptop for simple Pygame development. When creating your Pygame game, use this new boilerplate:

import sys
import pygame
from pygame.locals import *

def main():
	# Create a window 400x225 with 16 bpp
	window = pygame.display.set_mode((400, 225), 16)

	# Block MouseMotion event - Remove that line if you want to capture it
	pygame.event.set_blocked(MOUSEMOTION)

	# Get going
	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()

The 'main' method will be called by the activity wrapper later on, so it must be called 'main'. Using pygame.event.wait() instead of pygame.event.get() reduces the cpu-load from 99% to 0.7 - 4%.

Wrapping and Testing

If you want to test using laptop software, but you don't have a real XO, you could set up an emulated environment.

To make your game run as an Activity, you will need the 'olpcgames' wrapper, otherwise known simply as "the wrapper". Eventually, the wrapper will be included as part of the standard laptop software distribution, but for now you must include it in your Activity.

git clone git://dev.laptop.org/projects/games-misc

This will download several games-related projects. There are a few game Activities checked in, as well as the wrapper.

First construct an activity development bundle as in Sugar Activity Tutorial.

The wrapper requires the following boilerplate as the main class of your activity as specified in activity.info. Generally this is called 'activity.py' and your Pygame app retains its original name:

import olpcgames

# Class name must match 'class' property in activity/activity.info:
class ExampleActivity(olpcgames.PyGameActivity):
    """An example of using a Pygame game as a Sugar activity."""
    
    game_name = 'examplemodule:main'        # game_name must match name of your Pygame module
    game_title = 'Example'
    game_size = (1200,825)

The game_name parameter has the format "module:method". The module will be imported and the method called to start your Pygame application. Method is optional and defaults to main. (FIXME: Use Gettext to localize the game_title parameter.)

The game_size is what sets the display mode in Pygame.

The last step: you need to copy or link the 'olpcgames' directory from the Git repository you checked out into the root of your Activity. (Eventually, 'olpcgames' will be part of the main laptop build, so you will no longer need to do this.)

Hardware

The Pygame page has some information on hardware.

Camera

OLPCGames provides you with a simple wrapper class that will use GStreamer to snap an image with the built-in camera on the XO. The camera module works by creating a GStreamer pipeline that starts with the camera and terminates in a png or jpeg file. Alternately the module can allow you to take a "picture" of the test source, for those testing on non-XO hardware.

You will need to import the module:

from olpcgames import camera

when you want to take a picture with the camera, call snap_async, like so:

camera.snap_async( 'picture 32', source='test' )

the gstreamer pipeline will be created and will iterate until complete. When the picture is available it will be read as a pygame image and returned via a camera.CAMERA_LOAD or camera.CAMERA_LOAD_FAIL event in your Pygame mainloop. These events have the following members:

  • filename -- filename into which the file was saved
  • success -- boolean indicating whether we succeeded/failed
  • token -- the object passed in as the first argument to snap_async
  • image -- the loaded image as a Pygame surface (image), or None on failure
  • err -- if an error occurred, the Exception instance

There is a cameratest activity in GIT which demonstrates basic use of the snap_async function.

Note that the camera module exposes an older synchronous API for backwards compatibility. You likely should *not* use that api, as it can hang your entire activity waiting for the GStreamer stream to complete (which can take an arbitrary amount of time).

Sharing and the Mesh

Your activity can automatically be shared (through the user clicking "Share" on the Sugar toolbar). You should be using build >=530 (or so) for mesh related stuff. When the user does this, your activity will appear in the Neighborhood screen and others can join it.

In order to meaningfully communicate, you must

import olpcgames.mesh as mesh

Now, you can make use of events you receive that are mesh related. See Pygame_wrapper#Mesh for details.

Here is a simple example -- in your event loop, just add listening for certain mesh events:

    for evt in [ pygame.event.wait() ] + pygame.event.get( ):
        if evt.type == pygame.KEYDOWN:
            # ...

        elif evt.type == mesh.PARTICIPANT_ADD:
            # Somebody joined. Add them to our list of people, and send them a message:
            self.participants.append(evt.handle)
            mesh.send_to(evt.handle, str(self.game_state))

        elif evt.type == mesh.MESSAGE_UNI:
            # Received a message! Display it (and/or decode it):
            print "Got a message from %s: %s" % (evt.handle, evt.content)
            # Figure out the nick of whoever sent it:
            print "It was from buddy %s" % (mesh.get_buddy(evt.handle).props.nick)

See Also:

  • Discussion of how Productive uses the mesh module and raw Telepathy

Troubleshooting

Ensure you are using at least an Update.2 (first manufacturing release) Sugar environment. Also ensure you are using a checkout of the wrapper from the Git repository.

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.

User:Mcfletch is the current maintainer of the wrapper. Contact him if you get stuck.