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¶
- class game2d.app.GameApp(**keywords)¶
A controller class for a simple game application.
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 toself.view.draw()
.
Immutable Attributes¶
These attributes may be read (e.g. used in an expression), but not altered.
- GameApp.view¶
The game view.
Use the draw method in this attribute to display any
GObject
instance on the screen. See the classGView
for more information.Invariant: Must be instance of
GView
.
- GameApp.width¶
The window width
Invariant: Must be an int or float > 0.
- GameApp.height¶
The window height
Invariant: Must be an int or float > 0.
- GameApp.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.
Object Methods¶
Methods to Override¶
You will need to replace all of these methods in your subclass.
- GameApp.start()¶
Initializes the game state, creating a new game.
This method is distinct from the built-in initializer
__init__
, which has been hidden from you. This method is called once the game is running. You should use it to initialize any game specific attributes.Never override the built-in method __init__
- GameApp.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.
- Parameters
dt (
int
orfloat
) – time in seconds since last update
- GameApp.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 toself.view.draw()
.
Methods to Inherit¶
You should never override these methods.
- GameApp.run()¶
Displays the game window and starts the game.
This is a Kivy reserved method. It is part of the Kivy application process. It should never be overridden.
- GameApp.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.
Class Methods¶
These class methods are used by the game application to process assets. For the most
part, all of this is handled for your automatically, and you should never need to use
these methods. The only method that you will actually need to use is load_json
.
- classmethod GameApp.is_font(name)¶
Checks if
name
refers to a font fileThe method searches the Fonts folder for the given file name.
- Parameters
name (
str
) – The file name- Returns
True if
name
refers to a font file; False otherwise- Return type
bool
- classmethod GameApp.is_json(name)¶
Checks if
name
refers to a JSON fileThe method searches the JSON folder for the given file name.
- Parameters
name (
str
) – The file name- Returns
True if
name
refers to a sound file; False otherwise- Return type
bool
- classmethod GameApp.is_image(name)¶
Checks if
name
refers to an image fileThe method searches the Images folder for the given file name.
- Parameters
name (
str
) – The file name- Returns
True if
name
refers to an image file; False otherwise- Return type
bool
- classmethod GameApp.is_sound(name)¶
Checks if
name
refers to a sound fileThe method searches the Sounds folder for the given file name.
- Parameters
name (
str
) – The file name- Returns
True if
name
refers to a sound file; False otherwise- Return type
bool
- classmethod GameApp.load_json(name)¶
Returns: The JSON for the given file name, or None if it cannot be loaded
The
name
must refer to the file in the JSON folder. If the file is not there, it will return None.- Parameters
name (
str
) – The file name
- classmethod GameApp.load_texture(name)¶
Returns: The texture for the given file name, or None if it cannot be loaded
The
name
must refer to the file in the Images folder. If the texture has already been loaded, it will return the cached texture. Otherwise, it will load the texture and cache it before returning it.This method will crash if name is not a valid file.
- Parameters
name (
str
) – The file name
- classmethod GameApp.unload_texture(name)¶
Returns: The texture for the given file name, or None if it does not exist
The
name
should refer to the file in in the texture cache. If the texture is in the cache, it will return the cached texture before removing it. Otherwise, it will returning None.- Parameters
name (
str
) – The file name