Pyrge Tutorial 1: Up and Running

in Programming

Now that Pyrge is officially released, I need to write some documentation for it. So this is the first in a series of tutorials about using my glorious creation. First up, we’ll get everything installed, then we’ll start making a game that does nothing. I know, I know. Bear with me here. This is mainly for people that haven’t coded much, if at all.

Installing Pyrge

The first thing we need to do is download Pyrge. The link to the latest version should be in the sidebar of this post. Now, extract that ZIP (or tar.gz) file wherever you want.

Make sure that you have Python (2.5, 2.6, or 2.7) and Pygame (preferably 1.9.1) installed, because Pyrge needs those to run. If you have everything set up just right, then running the command “python setup.py install” in a command prompt or terminal should do everything you need. If not, check that you have all the necessary requirements, and that you typed the command correctly. If something’s still wrong, let me know and I’ll see what I can do.

Alternative: No Installation Needed

(Feel free to skip this section if the above installation worked for you.)

If you can’t install Pyrge for whatever reason, don’t worry, because there’s another way to use it. Move the whole installation to a subdirectory called “pyrge” under the main directory where your game code will live. So if you want to put your game code in the “C:\Documents\mygame” folder, Pyrge needs to be in the folder “C:\Documents\mygame\pyrge”. The tutorials and examples won’t work, but anything you run will. (The problem with this approach is that you might need multiple copies of Pyrge, one for each game, and that other users on your computer can’t access Pyrge.)

Does It Work?

Now that you have Pyrge installed, let’s see if it actually works. Our first game, if it even deserves the term, is as simple as this:

from pyrge import *
theGame = World()
theGame.loop()

Save that as a Python file (I called it “tutorial1.py”), and run it. It should pop up a black screen that does absolutely nothing until you close it. Not much of a game, I’ll admit, but it does let us know that Pyrge is correctly installed and works. (Or, at the very least, gives us an idea of what went wrong.)

The Real First Test

Not only is the previous “game” boring, it’s not even good from a programmer’s point of view. So we’ll make a new one (I’m using “tutorial2.py” for this one):

from pyrge import *
 
class TutorialWorld(World):
    def __init__(self):
        super(TutorialWorld, self).__init__()
        self.add(text.Text('Hello, Pyrge!'))
 
theGame = TutorialWorld()
theGame.loop()

Again, save and run. This time, we still get a black screen that doesn’t respond to any input, but this time that screen has text! Hey, everybody starts somewhere!

Let’s go through our little program line by line, since it shows most of the things that every game using Pyrge will have. First, line 1 imports the everything in the game engine. We can do this in two ways: import pyrge or from pyrge import *. With the first way, every time you use something from the engine, you’ll have to prefix it with “pyrge.“. In this game, that means that World would have to be written pyrge.World and text.Text would become pyrge.text.Text. The second method saves typing, but it means that you’ll have to be careful with the names of your own objects. If, for example, you called a variable “text”, it would hide the text module. For these simple tutorials, we’ll use the from pyrge import * form, saving typing at the expense of namespace pollution.

Continuing on, line 3 is the start of our “world definition”. Pretty much every Pyrge game will be defined as a subclass of World. In this case, we’re calling it by the unimaginative name of TutorialWorld.

Line 4 starts the definition of our game world’s constructor. This is the function that will create the world, so anything that needs be done before the game is started will go here.

Line 5 uses the super function to tell the parent World to do its own initialization. Almost all of your objects will have a call to the parent method to let the engine do its work: setting up variables and properties, initializing data, and so on.

On line 6, we are adding something to the world. (You can tell because the method is called self.add.) Specifically, we’re adding the text “Hello, Pyrge!” to the world. Since we don’t say otherwise, the text goes at position (0,0), the top-left corner of the screen, using the default font, size, and color.

Lines 8 and 9 actually run the game. We create an object and then call its loop method, which runs through the business of rendering a game each frame.