Cursgame Manual 0.2.0: Starting a game

Next: Filling the game, Up: Getting started [Contents][Index]

### 1.1 Starting a game

So the first we might want to know is how to start the game at all.

#### 1.1.1 Importing

To start the game we have to use the game class, and to do that we (obviously) need to import it. The game class is located at cursgame.game.Game which leads to the following import statement:

    import cursgame.game

or we could have done:

    from cursgame.game import Game

To make access to Game shorter. In this book i will use the upper way to make clear where classes and functions come from. You however are free to use your preferred way to import stuff.

#### 1.1.2 Preparing

Before we can make an instance of Game we need to get some other stuff together. What we exactly need is a Map and an InputHandler.

#### 1.1.2.1 Map

So now you might ask: “What exactly is this Map good for?” and i will answer: “The Map holds all the objects in the game and is responsible for writing all the objects onto the screen.”. And instanciating a Map is easy as it sounds. First we need to import it from cursgame.map and then make an instance of it:

    import cursgame.map
    mp = cursgame.mp.Map((100, 100))

Seems easy, right? But if you read carefully you will have noticed that i gave some parameters to your new map. And you will ask me: “Hey! What did you tell my map what you didn’t tell me?!” But please bear with me. I just told your map how big it has to be. This parameter can be any iterable — in this case a tuple — with exactly two ints in it telling the dimensions of the map. So in this case the argument was (100, 100), so we will get a map with 100x100 fields on it. And that’s all about maps you have to know now.

#### 1.1.2.2 InputHandlers

Now you might ask me: “What is an InputHandler?” And this time i will answer without quotation marks. An InputHandler catches key inputs and calls a function you instructed it to call. More on that later. But how do you make an instance of an InputHandler? It’s basically the same as making a Map. But instead of the dimensions it needs a paremeter telling wether the InputHandler should work turn-based or in real-time. We will choose turn-based because i don’t like real-time very much. For more on real-time or turn-based, see time-mode. So now let’s make an InputHandler:

    import cursgame.inp  # Note that the module is called inp so it not
                         # collides with the builtin input
    inp = cursgame.inp.InputHandler(False) # False: turn-based
                                           # True: real-time

And that’s all about for now.

#### 1.1.3 Instanciation

Now we’re prepared for instanciating the Game. We already discussed how to import the Game. So we only need to make an instance:

    game = cursgame.game.Game(mp, inp, 20)

So now let me explain what the parameters mean. The first is the Map we created in the preparation. The second the InputHandler. You may wonder a bit more about the third. See the Game class by default sets up a log for you. In this log you can write messages to the end-user (or to you when debugging). The third parameter defines how wide the log should be. But be carefull! If you make the log wider the map get’s smaller, so if you make the log wider than 80 on a lot of terminals you can’t see the map at all! For more on the log, see log.

#### 1.1.4 Starting the game

So after all you still want to use cursgame and finally get this damn game started? if not please leave now. Anyone left? good. So all left to do is to call the start method on the Game. Like this:

    game.start()  #Note that we use the instance of the game not the class

Now you should see a bunch of dots and on the left border of the screen a bit free space. The free space is the log discussed earlier and the dots are the fields on the map. For now there’s nothing to do because we didn’t set up any controls, not even for ending the game. So to end it you need to press ctrl+c, to end it the hard way.

#### 1.1.5 The whole thing

If you now have seen some pieces of code and don’t know how to put them together. Here’s the whole code needed to start the game.

    import cursgame.game
    import cursgame.map
    import cursgame.inp

    mp = cursgame.map.Map((100, 100))
    inp = cusgame.inp.InputHandler(False)
    game = cursgame.game.Game(mp, inp, 20)

    game.start()

I hope you see, it’s not that much.

Next: Filling the game, Up: Getting started [Contents][Index]