Porting pygame games to the XO: Difference between revisions
Line 94: | Line 94: | ||
# begin code snippet - more code above |
# begin code snippet - more code above |
||
import olpcgames.util, os |
import olpcgames.util, os |
||
fname = os.path.join(olpcgames.get_activity_root(),’data’,’melons.hs’) |
fname = os.path.join(olpcgames.util.get_activity_root(),’data’,’melons.hs’) |
||
# end code snippet - more code below |
# end code snippet - more code below |
||
</pre> |
</pre> |
Revision as of 05:06, 19 March 2008
Introduction
This tutorial attempts to guide you through taking an existing (finished) Pygame game and producing an “Activity” for the OLPC Sugar platform. For information on how to make a Pygame game, see http://pygame.org. Our final goal is an OLPC Activity bundle (a .xo file) which can be automatically installed on a Sugar platform or an XO laptop. You can download the finished project from the following SVN repository: svn://www.imitationpickles.org/melons/branches/olpc
The tutorial was created by Phil Hassey during the process of porting the “Watermelons” game to the OLPC platform and checked/shinified by Mel Chua. Read it with a light-hearted, game-friendly voice...
Installing the OLPC goodies
- Get a Sugar environment running on your computer. If you have an XO, you're done (though you may want to upgrade to the latest stable build before proceeding, see Olpc-update) – if you don't have an XO, read on.
- If you're running Windows, download the OLPC QEMU emulator quick-start package at Emulating_the_XO/Quick_Start/Windows and follow the instructions. (Note: The files are big. If you're doing this in a group, get an USB key, save the files there, and pass it around to save bandwidth and sanity.)
- If you're running Ubuntu or Debian, there are packages available; instructions are here (the one for Ubuntu Gutsy is particularly easy to use – this is probably the most painless install method for getting Sugar on a non-XO machine at the time of this writing, March 2008).
- If you're running something else, the Developers/Setup page may have some information for you.
Logging into the (Virtual) OLPC
- Read the appropriate “how to use an OLPC” documentation, the OLPC Sugar software doesn't work much like most other GUI shells on any platform - http://www.laptop.org/en/laptop/start/
- You get to choose a name for your laptop and a color to display.
- You can press ALT-F, which does something like “add a frame to the screen”, whatever that means, maybe you could read about it somewhere
- Play around with Sugar for a while. Start some activities; note how they display in Home view, in the Journal, what resolution the graphics display at, and so forth – also (if you have an actual XO) how the keyboard, trackpad, and other physical aspects of the laptop affect playability.
Running your game on the XO
- First we need to make sure your Pygame game works on the XO (it should). to do this, in Sugar, open the Terminal activity (it’s the $-in-a-box icon).
- Download your game onto the XO / emulator (anywhere). If you're using your own computer, we'll assume you already know how to get files onto it. If you're using an XO, you can...
- Put your game on a USB stick and plug it into the XO, then open the Journal activity (it looks like a book) and drag files from the stick to your XO
- Use wget
- If you happen to use subversion for your code:
become_root # note: this is a special command instead of 'su' yum install subversion svn co <your-game's-repository> <pre> (in my case: "svn co svn://www.imitationpickles.org/melons/trunk melons" but that's probably more information than you really needed) #Try running your game from within the Terminal activity by typing “python your-game-file.py” (or whatever you usually use to run your game from the command line). #If it runs, that's great (and hey, that was easy). You're now ready to wrap your game into an Activity. k.But wait – before you do, make sure your main file (the your-game-file.py that you call with “python your-game-file.py” when running the game) has a main() function and the “if __name__ == ‘__main__’: main()” pattern (instead of just having it in the top level of your file). We'll see why this is important for wrapping shortly. #If your game doesn't run in Sugar, check to make sure it runs under Pygame outside Sugar (on your “normal” computer). If it runs outside of Sugar but not within Sugar, that's odd, and you should head to the IRC channel and ask for help (#olpc and/or #pygame at irc.freenode.net – see http://wiki.laptop.org/go/IRC if you're new to IRC).
Building your activity wrapper
- On your XO (or your Sugar emulator), download the latest version of the OLPCGames wrapper at http://dev.laptop.org/~mcfletch/OLPCGames/ (the current one as of this writing, March 2008, is version 1.6). Put it into your home folder and unzip it. One sequence of commands that does this is shown below.
cd ~ wget http://dev.laptop.org/~mcfletch/OLPCGames/OLPCGames-1.6.tar.gz tar -xvf OLPCGames-1.6.tar.gz
- Move into the OLPCGames-1.6/skeleton directory.
cd OLPCGames-1.6/skeleton
- Run the buildskel.py script. You'll need to know four things to do this:
- The name of your game (a short one) – in this case, “melons”. (We'll call this GAME_NAME).
- A human-readable name for your game (can be longer) – in this case “Watermelons,” but your game name and its human-readable name can be identical. (We'll call this HUMAN_READABLE_NAME).
- The name of your main module. If you run your game from the command line using “python your-main-file.py” this is “your-main-file” - in this case, “main” (because we use “python main.py”). (We'll call this MAIN_MODULE_NAME).
- The name of the main function within your name file. This is also usually “main”. (We'll call this MAIN_FUNCTION_NAME).
- The command to run is
python buildskel.py –n GAME_NAME –t HUMAN_READABLE_NAME –m MAIN_MODULE_NAME:MAIN_FUNCTION_NAME
- In the case of watermelons...
python buildskel.py –n melons –t “Watermelons” –m main:main
- You should now have an activity folder named “GAME_NAME.activity” - in our example, “melons.activity”. Go into that folder.
cd melons.activity
Wrapping Your Game
- If your game includes a setup.py file (which it should – it's good development practice), we'll sadly have to replace it by the one provided by OLPCGames. Delete or rename your current setup.py file to setup.py.old or something. Now...
- Copy the contents of your game folder into the activity folder (which should at this point be your current working directory). In our case...
cp -r ~/melons/* .
- If you didn't follow the first step and overwrote the folder's setup.py with your game's setup.py, that's ok; we can recover.
- ONLY if you've overwritten the setup.py file!
cp ../skel.activity/setup.py ./olpc-setup.py
- Now you're ready to register your game as an activity.
python olpc-setup.py dev
- We need to restart Sugar...
- On the XO, click Alt-Ctrl-Erase
- On a non-XO (Sugar emulator), just restart the emulator
- You should now find a hammer (the default OLPCGames icon) in the bottom of the frame in Home view, nestled alongside the other Activity icons.
Testing Your Game
- Click the icon (hammer) of your game.
- If it works, rejoice.
- If it doesn't work, (or even if it does work – it's still a good idea to see the logs, especially if you ) start up the Log Viewer Activity (it looks like three bullet-pointed list items in a box).
- In the Log Viewer Activity, click on your game. It is probably named “org.laptop.community.GAME_NAME-NUMBER.log” where GAME_NAME is the name of your game and NUMBER is the version number (usually 1). In our case, this is “org.laptop.community.melons-1.log”.
- You should see a traceback. Use it for debugging as needed.
Saving game data
- Although my game ran, I noticed that some errors were in the log. Apparently the OLPC laptop has some security restrictions such that python cannot write to your game's working directory. If your game likes to save things, you’ll have to use the OLPCGames API to get your “save game” directory.
- Watermelons writes out a record of how the game was played and high scores, so I’ll fix that... You’ll want to start up the Terminal Activity again and fix those bits of code. (To switch between tasks on the OLPC laptop press F3/Home to get back to Home view, and choose the other activity (Log Viewer vs Terminal) or, if you have an actual XO, use Alt-Tab to cycle between running programs.) Here's what I had to put in mine (game.py).
# begin code snippet - more code above import olpcgames.util, os fname = os.path.join(olpcgames.util.get_activity_root(),’data’,’melons.hs’) # end code snippet - more code below
You can write to that folder now.
Creating your icon
- Download inkscape from www.inkscape.org - it's available for most platforms
- You can start from an existing icon, like this one
- These Sugar folks are a bit picky about their Icons, specifications and all that, so read OLPC_Human_Interface_Guidelines/The_Sugar_Interface/Icons
- After you have created your new compliant and rather excessively inspiring icon, you must alter it so that it must change colors within sugar. Read Making_SVG_Icons_for_Sugar
- Save your finished icon into (your project)/activity/activity.svg
- Restart sugar (your laptop) and you’ll have the new icon!! Yaay!
Building your package!
- Emulators? Who cares about emulators, we want to see our code on a cute little laptop!
- If you want to check the whole package into svn, you’ll need to:
mv olpcgames olpcgames-x mkdir olpcgames cp –r olpcgames-x/* olpcgames
This makes a local copy of the olpcgames package for your game to include in its repository
- Add in all the other stuff .. (left as an exercise to the reader, *.py, etc)
- Edit MANIFEST.in and add in stuff like
- recursive-include data *
- TODO: add in recursive exclusion of .svn files somehow!
- or whatever data folders you have in your game
- Build the MANIFEST file from your cool MANIFEST.in file now:
python olpcgames/buildmanifest.py
And then...
- python olpc-setup.py dist
- (or if you were a bad person and didn’t have your own setup.py, just do python setup.py dist)
Play on a Little Computer
If you have an XO, you can test your game on it now.
- Copy the .xo file that is in the CWD, named “melons-1.xo” to your web server
- On the XO use wget to get your game and save it into ~/Activities
- You may need to create the Activities folder first
cd Activities unzip melons-1.xo
- Restart the XO... again.
- Your game should be installed! Let the good times roll!! Spend a few hours playing with it.
Cross platform issues
If you want to have your game run on both the OLPC and “regular “ machines, you might want to copy the skeleton into your code, instead of the code into the skeleton, that way you still have your .svn directories.
Detecting if you are using the OLPC
try: import olpcgames, olpcgames.util if not olpcgames.ACTIVITY: raise RuntimeError # Do something for the OLPC except (RuntimeError,ImportError): # do something as you would usually do that something
Other issues
If your game is not exactly the same resolution as the OLPC – it won’t be centered. If reasonable, change your game resolution to the OLPC res. If not, don’t worry – they are working on fixing it so that your game view will be properly centered on the screen!