Class GameApp¶
This is the primary class for creating a game. To implement a game, you subclass this class and override three methods. The three methods are as follows:
start: This method initializes the game state, defining all of the game attributes. This method is like __init__ except that you should not override that method. Overriding __init__ will break your game. Hence we have provided build as an alternative.
update: This method updates the game state at the start of every animation frame. Any code that moves objects or processes user input (keyboard or mouse) goes in this method.
draw: This method draws all of the objects to the screen. The only thing you should have in this method are calls to self.view.draw().
Constructor¶
-
GameApp
(**keywords)¶ Constructor: Creates, but does not start, a new game.
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, the constructor
Game(width=400,height=400)The game window will not show until you start the game. To start the game, use the method run().
You will never call the constructor or run yourself. That is handled for you in the provided code.
Parameter: keywords – dictionary of keyword arguments Precondition: See above.
Immutable Attributes¶
These attributes may be read (e.g. used in an expression), but not altered.
-
view
¶ The Game view.
Use the draw method in this attribute to display any GObject instance on the screen. See the class GView for more information.
Invariant: Must be instance of GView.
-
width
¶ The window width
Invariant: Must be an int or float > 0.
-
height
¶ The window height
Invariant: Must be an int or float > 0.
-
fps
¶ The number of frames-per-second to animate
By default this value is 60 FPS. However, we cannot guarantee that the FPS is achievable. If you are having performance stuttering, you might want to drop this value to 30 FPS instead.
Invariant: Must be an int or float > 0.
Methods¶
Methods to Override¶
You will need to replace all of these methods in your subclass.
-
start
()¶ Initializes the game state, creating a new game.
This method is distinct from the built-in initializer __init__. This method is called once the game is running. You should use it to initialize any game specific attributes. Never overridden the built-in method __init__
-
update
(dt)¶ Updates the state of the game one animation frame.
This method is called 60x a second (depending on the fps) to provide on-screen animation. Any code that moves objects or processes user input (keyboard or mouse) goes in this method.
Think of this method as the body of the loop. You will need to add attributes that represent the current animation state, so that they can persist across animation frames. These attributes should be initialized in start.
Parameter: dt – time in seconds since last update Precondition: a number (int or float)
-
draw
()¶ Draws the game objects on the screen.
Every single object that you draw will need to be an attribute of the GameApp class. This method should largely be a sequence of calls to self.view.draw().
Methods to Inherit¶
You should never override these methods.
-
run
()¶ Displays the game window and start the game.
This is a Kivy reserved method. It is part of the Kivy application process. It should never be overridden.
-
stop
()¶ Closes the game window and exit Python.
This is a Kivy reserved method. It is part of the Kivy application process. It should never be overridden.