Class GameController

class graphics.GameController

Primary controller class for a simple game application.

To create a game application, subclass this class. Make sure to add a call to super to your constructor (should you choose to override the constructor at all). To provide game functionality you only need to override the following five methods:

initialize: Called at the start of the game to initialize your program. It is preferable to put start-up code here rather than in your constructor.

update: Called every animation frame (60x a second). This is where you add any game animation code.

on_touch_down: Called whenever the user presses the mouse or a finger (for a touch screen device).

on_touch_up: Called whenever the user releases the mouse or a finger (for a touch screen device). Every touch_down event has a corresponding touch_up event.

on_touch_move: Called whenever the user moves the mouse or a finger while it is still help down. touch_move events are optional for each press, while touch_down and touch_up are not.

These are the only methods that you must implement. In addition, you should add whatever fields and/or helper methods are necessary for your game.

Except in very few instances you should never need to add properties to a GameController. As the controller, it manages all of the objects in the game, and none of the other objects need to access its fields.

Constructor: Creates a game with this controller

Immutable Attributes

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

view

The Game view.

This attribute is immutable in that you cannot delete it or replace it by another view. However, the view itself is not immutable. You are free to use the add and remove methods in this attribute to add and remove graphics objects.

Methods

Methods to Override

initialize()

Called to initialize the game features.

This method is the preferred way to initialize the elements in your game, rather than overriding your constructor. Because of the way graphical applications work, you cannot guarantee that the view window is sized properly when you are still inside of the constructor. This method is called later, when we can guarantee that the window and other graphics elements have the right size.

Override this method to provide code specific to your game.

update(dt)

Called every animation frame.

param dt:time in seconds since last update

Precondition: a number (int or float)

This method is called 60x a second to provide on-screen animation. Think of it as the body of the loop. It is best to have fields that represent the current animation state so that you know where you are in the animation.

Override this method to provide code specific to your game.

on_touch_down(view, touch)

Called when the user presses the mouse or a finger (on touch screens)

param view:view receiving touch event

Precondition: the GameView of this GameController

param touch:touch event information

Precondition: a Kivy MotionEvent object

This method responds to a touch_down event. If you want the user to ‘click to continue’, or if you want to control a paddle on screen, this is the primary method for responding to those controls. Every touch_down event is followed by a touch_up event.

Override this method to provide code specific to your game.

on_touch_up(view, touch)

Called when the user releases the mouse or a finger (on touch screens)

param view:view receiving touch event

Precondition: the GameView of this GameController

param touch:touch event information

Precondition: a Kivy MotionEvent object

This method responds to a touch_up event. As every touch_down event has a corresponding touch_up event, this lets you know when the mouse or finger has been released. At this point you should reset whatever state fields you had following the touch, as appropriate.

Override this method to provide code specific to your game.

on_touch_move(view, touch)

Called when the user moves the mouse or a finger (on touch screens)

param view:view receiving touch event

Precondition: the GameView of this GameController

param touch:touch event information

Precondition: a Kivy MotionEvent object

This method responds to a touch_move event. Touch moves are optional events and do not always need to be processed. However, they are important if you want the touch to move a graphics object on screen, such as a paddle.

Override this method to provide code specific to your game.

Utility Methods

delay(callback, time)

Delay the execution of callback for time seconds.

param callback:function called after delay

Precondition: a function reference; must be a function with no additional arguments

param time:time to wait in seconds before calling the function.

Precondition: a positive number (int or float)

You may have multiple callbacks delayed at any given time. However, you be careful about called delay inside of callback functions already delayed. The result is similar to recursion in that it can run out of memory if you do it too much.

Table Of Contents

Previous topic

Class GameView

Next topic

Graphics Objects

This Page