Game
- class quest.game.QuestGame[source]
Implements a top-down video game with a character on a map.
QuestGameis the central class in the Quest Game Framework, which is built on top of The Python Arcade Library.QuestGameis a subclass ofarcade.Window. To create your own game, create a subclass ofQuestGameand then override whatever you need to change from the default behavior.When
QuestGameis initialized, it sets up the player, the maps, the walls, the NPCs, the physics engine, and centers the viewport on the player. Rather than overriding__init__(), consider overriding just the setup functions you need to change.- screen_width
Width in pixels of the game window.
- screen_height
Height in pixels of the game window.
- left_viewport_margin
Minimum distance (in pixels) between the player sprite and the left edge of the viewport.
- right_viewport_margin
Minimum distance (in pixels) between the player sprite and the right edge of the viewport.
- bottom_viewport_margin
Minimum distance (in pixels) between the player sprite and the bottom edge of the viewport.
- top_viewport_margin
Minimum distance (in pixels) between the player sprite and the top edge of the viewport.
- screen_title
Title of the game window (displayed top center)
- player_scaling
Factor by which to scale the player sprite.
- player_sprite_image
Filepath for the player sprite.
- player_speed
In pixels per update. (By default, the game runs at 60 hertz, so update is called every 1/60 second.)
- player_initial_x
Initial x-coordinate for player center.
- player_initial_y
Initial y-coordinate for player center.
- view_bottom
y-coordinate of the bottom edge of the current viewport
- view_left
x-coordinate of the left edge of the current viewport
- setup_maps()[source]
Sets up the game maps.
self.maps should be assigned to a list of
Mapobjects, which get initialized here. Each map represents a ‘level’ or ‘scene’ of the game. Once the list of maps is created, useset_current_map()to set one of the maps as the initial current map. This method will need to be overridden by any game using a map. For more details, see Creating Maps
- setup_player()[source]
Creates the player sprite.
Initializes a sprite for the player, assigns its starting position, and appends the player sprite to an
arcade.SpriteList(Arcade likes to work with sprites in SpriteLists).
- get_current_map()[source]
Gets the current game map.
The current map is tracked using
current_map_index.- Returns
The current Map.
- set_current_map(index)[source]
Sets the current game map.
Checks to make sure
indexis valid, and then stores it ascurrent_map_index
- setup_physics_engine()[source]
Sets up the physics engine.
Initializes the physics engine that will be used in the game. A physics engine maintains a tick model of time (see Tick model of time) and, at each moment, resolves interactions between sprites according to a set of rules. By default,
QuestGameuses aContinuousPhysicsEngineallows sprites to occupy any (x, y) coordinates, while preventing the player sprite from passing through walls. Walls are any sprites on a map layer with thewallrole.If you prefer for sprites to occupy discrete coordinates on a grid instead (this can be helpful for implementing NPC behavior), use a
DiscretePhysicsEngineinstead.
- on_update(delta_time)[source]
Updates the game’s state.
At every tick, the game needs to be updated. The physics engine updates sprite positions, and then sprite callbacks are executed. Finally, the viewport is scrolled. Note that on_update changes the state of the game, but does not draw anything to the screen.
- Parameters
delta_time – How much time has passed since the last update.
- on_draw()[source]
Draws the screen.
At every tick, just after on_update, the whole screen needs to be redrawn. This involves drawing the background color, each map layer with the display role, the NPC’s, the player, and any message that needs to be displayed.
- on_key_press(key, modifiers)[source]
Handles key presses.
- Parameters
key – The key that was pressed.
modifiers – A list of currently-active modifier keys (e.g. shift).
While a key is pressed, the sprite’s x- and y- change values are set to the player’s movement speed. Think of this as an intention to move; it’s up to the physics engine to decide whether this actually results in movement. For example, the physics engine will prevent players from moving into walls. This method is automatically called at the appropriate time.
- on_key_release(key, modifiers)[source]
Handles key releases.
- Parameters
key – The key that was released.
modifiers – A list of currently-active modifier keys (e.g. shift).
Whenever a key is released, the player’s change_x or change_y is set to 0, indicating that the player no longer intends to keep moving. This method is automatically called at the appropriate time.
- scroll_viewport()[source]
Updates the viewport to keep the player within margins.
If the player sprite is too close to any edge of the viewport (or has somehow gone beyond the viewport), scrolls the viewport to the player. The {left, right, bottom, top}_viewport_margin properties specify how close the player is allowed to be to the edge before scrolling.