Game

class quest.game.QuestGame[source]

Implements a top-down video game with a character on a map.

QuestGame is the central class in the Quest Game Framework, which is built on top of The Python Arcade Library. QuestGame is a subclass of arcade.Window. To create your own game, create a subclass of QuestGame and then override whatever you need to change from the default behavior.

When QuestGame is 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

run()[source]

Starts the game.

setup_maps()[source]

Sets up the game maps.

self.maps should be assigned to a list of Map objects, which get initialized here. Each map represents a ‘level’ or ‘scene’ of the game. Once the list of maps is created, use set_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 a SpriteList (Arcade likes to work with sprites in SpriteLists).

setup_walls()[source]

Does any neccessary setup for NPCs.

setup_npcs()[source]

Does any neccessary setup for NPCs.

add_map(game_map)[source]

Adds a map to the list of maps.

Parameters

game_map – A quest.map.Map object.

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 index is valid, and then stores it as current_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 resolves interactions between sprites according to a set of rules. The The Python Arcade Library PhysicsEngineSimple just keeps the player sprite from bumping into walls. More complicated physics engines could implement collisions, gravity, or even realistic 3-dimensional interactions.

By default, QuestGame uses PhysicsEngineSimple to prevent the player sprite from colliding or passing through any sprites on a map layer with the wall role. If there are no MapLayer with the wall role, uses the NullPhysicsEngine instead. Don’t override this method unless you understand Arcade’s physics engines pretty well.

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.

open_modal(modal)[source]

Shows a modal window and pauses the game until the modal resolves.

close_modal()[source]

Resolves a modal window and resumes the game.

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.

center_view_on_player()[source]

Centers the viewport on the player.

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.

update_viewport()[source]

Updates the viewport.

Uses the view_left, view_bottom, and the screen size properties to update the viewport. Needs to be called after changing any of these properties.

message()[source]

Generates a message (or no message) to be shown on screen.

Returns

A string to be shown on screen, or None if no message is needed.