Previous: , Up: Getting started   [Contents][Index]


1.3 AIs

This section describes how to use the AI-loop and what it is. This section only describes where Cursgame gives you space for AIs. This chapter does NOT describe how to write good AIs. Cursgame does not contain an AI.

1.3.1 The action-loop

Cursgame contains a so called “action-loop”. The action-loop is a list of objects that need to do stuff every frame. At every frame/round the game calls the loop method on every Obj in the action-loop. It’s most often used for giving AI-controlled Objs the possibility to do stuff.

1.3.1.1 action-loop example

For using the action-loop we first need an Obj that uses the ai-loop. What’s better for that purpose than an enemy? (Suggestions please to the maintainer!) So let’s make an enemy:

class Enemy(chatacter.Obj):
    def __str__(self):
        return "e"

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

    def aloop(self):
        # The following code references the global variable player.
        # Please note that i don't consider this good style.
        # If you write your own Cursgame-based programs i suggest
        # keeping your variables somewhere else.
        px, py = player.pos
        sx, sy = self.pos
        if px > sx:
            x = 1
        elif px < sx:
            x = -1
        else:
            x = 0
        if py > sy:
            y = 1
        elif py < sy:
            y = -1
        else:
            y = 0
        self.move(x, y)

So this is our whole AI, for this very simple Enemy. The important part of the Enemy class is the aloop method. It’s very simple, so i don’t describe it in detail. It just makes the enemy move one step towards the player. Now we need to place our enemy:

enemy = Enemy()
mp.place(enemy, 20, 20, True)

So at first this might doesn’t look different from placing a wall or something else, but it is different! Note the fourth argument to mp.place! It says that our enemy should be placed on the action-loop. When we used place before we omitted this argument. It defaults to False, so if we don’t give it Obj.aloop isn’t called! Please note that the player doesn’t need to be on the action-loop for controlling him. The InputHandler has other ways to do stuff.


Previous: , Up: Getting started   [Contents][Index]