Next: , Previous: , Up: Top   [Contents][Index]


2 Module game

This chapter describes the contents of the game module in detail.
The game module is imported from: cursgame.game
The game module is located at: cursgame/game.py

2.1 The Game class

The Game class is at the top of the structures holding the data for the game. It holds the Map and the InputHandler. It also provides access to the log.

2.1.1 methods

Here are all methods of the Game class.

2.1.1.1 __init__/constructor

The game class takes at instanciation three arguments. The first is the map. (See Map.) The second is the InputHandler (See InputHandler.) Since they are both needed for creating the Game they need to be instanciated before the Game. The third argument is the size the log (see log) should have. You can subclass Game and overwrite methods like described below.

2.1.1.2 logwrite

This method takes one string and writes it in the log. You don’t need to worry about scrolling the log manually. logwrite does everything that is needed to print the text you gave on the log on the left side of the screen. The log has has a size defined by the logsize argument. Note that the logsize is substracted from the size of the screen used for printing the map. If the text is too long to fit in the log it is splitted in multiple lines.

2.1.1.3 refresh

This function is called once per frame. It refreshes the physical screen. You shouldn’t call it yourself except you’re overwriting the main loop of the game. It takes two arguments. The screen where the map is printed on and the full screen.

2.1.1.4 loop

This function is called once per frame and does everything that has to happen in this frame. You shouldn’t call it directly. You can override it in a subclass, but you should be carefull! Your new loop should at least do everything in this list:

Please note that it may not be necessary to override the loop method. By default loop calls the method subloop (see Game.subloop). It gets two arguments, the screen wehere the map is printed on and the full screen.

2.1.1.5 subloop

This method is called once per frame. You can override it in a subclass and do anything you want. The return value is (by default, (see Game.loop). used to determine wether the game should continue. If True is returned the game continues. If False is returned the game ends immediately. This method takes no arguments

2.1.1.6 user_setup

This method is called at the end of the __init__ function. You can use it to initialize the game further, without overriding __init__.

2.1.1.7 run

This method is (indirectly) called by the start method (see Game.start). It initializes the the game to an running state. You shouldn’t call or override it. It takes three arguments. These are automatically created by padwrap (see padwrap). The arguments are in this order: The screen for the map, the full screen, the screen for the log.

2.1.1.8 start

This method starts the game. It takes no arguments and starts the main loop. It runs until the game ends. You shouldn’t override, unless you know what you’re doing.

2.1.1.9 set_end/ending the game

If you call this method the attribute Game.end is set to True. This means that at the end of the running frame the game ends. This method takes on optional argument, which is ignored. This is handy for use with InputHandler (since the InputHandler gives one argument to the callbacks). Note that you can instead of calling this method you can just set the variable Game.end to True yourself. Also note that you can end the game in the subloop, see Game.subloop.

2.1.2 attributes

Here are all the important attributes of the Game class.

2.1.2.1 map

This is the Map associated to the game.

2.1.2.2 inp

This is the InputHandler associated with the game.

2.1.2.3 end

This variable marks wether the game should end at the end of the running frame. See Ending the game, for more on ending the game.

2.2 padwrap

This function is used by the start method of the game. It’s like curses.wrapper but it is customized for the use with Cursgame. Normally you don’t want to use this function, since the Game class handles this itself.


Next: , Previous: , Up: Top   [Contents][Index]