Class Game

Game(**keywords)

Primary controller class for a simple game application.

You should never use this class directly, as it does not do anything by itself. You should make a subclass and implement the three main methods: init, update, and draw. Read their method specifications for instructions about what these methods should do.

Note that init is different from the built-in initializer __init__. Because of the weird ways in which Kivy initializes graphics objects, you should never override __init__

Constructor: creates, but does not start, a new game.

param keywords:dictionary of keyword arguments

Precondition: See below.

To use the constructor for this class, you should provide it with a list of keyword arguments that initialize various attributes. The primary user defined attributes are the window width and height. For example, to create a game that fits inside of a 400x400 window at 30 frames per second, use the constructor

Game(width=400,height=400,fps=30.0)

The game window will not show until you start the game. To start the game, use the method run().

Immutable Attributes

These attributes may be read (e.g. used in an expression), but not altered.

view

The Game view.

You should pass this attribute to the draw method of a GObject instance to draw it.

Invariant: Immutable instance of GView.

width

The window width

Invariant: Immutable float.

height

The window height

Invariant: Immutable float.

fps

Target animation FPS

We cannot guarantee that the FPS is achievable. Python is not super fast. We do try for 60 FPS, however.

Invariant: Immutable float > 0.

Methods

Methods to Override

You will need to replace all of these methods in your subclass.

init()

Initialize the game state.

This method is distinct from the built-in initializer __init__. This method is called once the game has started, but before it enters the event loop. You should use it to initialize any game specific attributes. Do not override the method __init__

update(dt)

The body of the event loop.

param dt:time in seconds since last update

Precondition: a number (int or float)

This method is called repeatedly to provide on-screen animation. The speed at which this method is called is determined by the attribute fps. Sometimes it is impossible to guarantee that fps and the event loop will slow down. You can use the parameter dt to measure your animation speed; the higher the value, the slower your animation.

This method should handle all computation in the event loop. While the draw method is part of the even loop, it should never do anything but loop through the models and call draw.

draw()

The draw portion of the event loop.

This part of the event loop should be relatively simple. Unless directed otherwise, model objects are all instances of subclasses of GObject, which has the method draw. This method should just loop through all the models and call draw on each one. There is nothing else to do.

Methods to Inherit

You should never override these methods.

run()

Display the game window and start the game.

If you call this method in the interactive shell, it will cease to be responsive.

stop()

Close the game window and exit Python.

You should never need to call this method.

Table Of Contents

Previous topic

Class GView

Next topic

Class Sound

This Page