Cursgame Manual 0.2.0: Filling the game

Next: AIs, Previous: Starting a game, Up: Getting started [Contents][Index]

### 1.2 Filling the game

Once you started the game you will see that nothing’s going on. In this section i will describe what to do to make stuff happen. In this section we will expand the code from the last chapter. So i assume that you did read it. To follow this section to a working program you need to insert the code, showed in this section, after the instanciation of the game and before game.start() is called.

#### 1.2.1 A player

The first thing we might want to have is a player. A player is a subclass of an Obj. Everything that can be placed in the game and can act in the game is an Obj. That’s all you need to know about Objs for now. For creating our player we need to import `cursgame.character`. What we want from there is the class Human. Later you can make your own Obj subclasses, for more on that see Objs. But for now we will only use the Human. We create and place the player like this:

    player = cursgame.character.Human()
    mp.place(player, 0, 0)
    mp.lookon = player

Let’s go through this line by line:
The first line creates an instance of the Human class. This object instance represents our player.
The second line places the player on the map, at the position 0, 0. This is marked with the last two aguments. See Map.place, for more information on the place function.
The third line assigns our player to the lookon variable. This tells the game that the screen should be centered at our player. See Map.lookon, for more information on this variable.

#### 1.2.2 Control

Now we want to be able to control our player. To do that we need to give instructions to the InputHandler. See InputHandler, for more on InputHandlers. The InputHandler holds an dictionary called callbacks (so it is accessable through inp.callbacks). Every time an input event is encountered the InputHandler looks in the dictionary for the event and executes the result. So the result should be a callable. Otherwise an error will occur. `cursgame.inp` holds a bunch of constants related to keys. See key-constants, for the meaning of the constants. For letter-keys it’s enough to assign the the function to the unicode of the letter. So let’s set up basic control for our player:

    inp.callbacks[ord('q')] = game.set_end
    inp.callbacks[cursgame.inp.KEY_DOWN] = lambda _: player.move(0, 1)
    inp.callbacks[cursgame.inp.KEY_LEFT] = lambda _: player.move(-1, 0)
    inp.callbacks[cursgame.inp.KEY_UP] = lambda _: player.move(0, -1)
    inp.callbacks[cursgame.inp.KEY_RIGHT] = lambda _: player.move(1, 0)

The first line instructs the InputHandler to end the game if Q is pressed. See Ending the game, for more on `game.set_end`. The other lines are a bit more complicated, but they all follow the same pattern. Let’s first take a look at the part left of the equal sign. `inp.callbacks[cursgame.inp.KEY_DOWN]` means that we want to assign a function that is called when the key related to KEY_DOWN is pressed. As you might thought, KEY_DOWN refers to the down-key. The right side of the equal sign is the place where the real work is done. The values of the dictionary must be functions taking exactly one argument. When we want to move an Obj (remember our player is an Obj) we call it’s move method. But move takes two arguments, telling where to move. So we need to make another function that takes exactly one function. That’s what the lambda is for. The one argument we get isn’t in our interest since it contains the key event that was caught by the InputHandler. Most of the time we don’t need this but it becomes handy of we assign one function to multiple key-events. So basically the right part of the equal sign is a function moving the player by by (0, 1), which is one field down. The last three lines follow the same pattern. See Obj.move, for more information at moving player.move.

#### 1.2.3 Objs

Now we can move our player. But that’s a bit stupid and boring if there’s nothing to see when walking. In fact there’s nothing except our player, the border of the map and empty fields. So let’s place some walls. How do we do that? First we need to subclass the Obj class, which is located at `cursgame.character`. So we already imported it along with the Human. Allow me to first show you how we subclass it and then describe what we did:

    class Wall(cursgame.character.Obj):
        def __str__(self):
            return "#"

        def activate(self, activater, x, y):
            return False

And that’s all you need for a wall. It inherits from Obj. It’s __str__ says how it should be presented on the map. In this case a Wall has the # sign as a representation. Just like Humans have the @ sign. The activate function is called when another Objs wants to enter the field where this Obj is already. The return value is used to determine wether the step on this Obj succedes and the moving Obj should be moved to the new position. In this case we simply return False because it’s not allowed to step on walls. See Obj.activate, for more on this topic.

#### 1.2.3.1 Placing the wall

Now we have to place the wall so it has an effect in the game. This is simply done by first instanciating it and then placing it. Just like the player. Here’s the code:

    wall = Wall()
    mp.place(wall, 10, 10)

This places the wall at 10;10.

#### 1.2.3.2 Color

Now we can paint our wall. For that take a brush and a little bit paint of your favorite color. Then start the game and paint the wall on the screen using your brush... Sounds stupid? It is! I prefer the cursgame-way to paint Objs. Just like __str__ is used to determine the textual representation of an Obj the color method is used to determine the color of an Obj. For that purpose `cursgame.color` holds a set of color-constants you can return. Let’s take a green wall. So we add this method to our Wall class:

        def color(self):
            return cursgame.color.GREEN

Don’t forget to add `import cursgame.color` to your imports! Now your wall should be green, without making you screen permanently green! See colors, for more on colors.

Next: AIs, Previous: Starting a game, Up: Getting started [Contents][Index]