Show:
Module: p5.game
Parent Module: p5.game

Methods

_isKeyInState
(
  • key
  • state
)
Boolean
private

Defined in lib/p5.game.js:507

Detects if a key is in the given state during the last cycle. Helper method encapsulating common key state logic; it may be preferable to call keyDown or other methods directly.

Parameters:

  • key Number | String

    Key code or character

  • state Number

    Key state to check against

Returns:

Boolean:

True if the key is in the given state

_isMouseButtonInState
(
  • buttonCode
  • state
)
Boolean
private

Defined in lib/p5.game.js:592

Detects if a mouse button is in the given state during the last cycle. Helper method encapsulating common mouse button state logic; it may be preferable to call mouseWentUp, etc, directly.

Parameters:

  • [buttonCode] Number optional

    Mouse button constant LEFT, RIGHT or CENTER

  • state Number

Returns:

Boolean:

True if the button was in the given state

_keyCodeFromAlias
(
  • alias
)
Number | Undefined
private

Defined in lib/p5.game.js:738

Given a string key alias (as defined in the KEY property above), look up and return the numeric JavaScript key code for that key. If a deprecated alias is passed (as defined in the KEY_DEPRECATIONS property) it will be mapped to a valid key code, but will also generate a warning about use of the deprecated alias.

Parameters:

  • alias !string
    • a case-insensitive key alias

Returns:

Number | Undefined:

a numeric JavaScript key code, or undefined if no key code matching the given alias is found.

animation
(
  • anim
  • x
  • y
)

Defined in lib/p5.game.js:442

Displays an animation.

Parameters:

  • anim Animation

    Animation to be displayed

  • x Number

    X coordinate

  • y Number

    Y coordinate

createGroup ()

Defined in lib/p5.game.js:225

In p5.game groups are collections of sprites with similar behavior. For example a group may contain all the sprites in the background or all the sprites that "kill" the player.

Groups are "extended" arrays and inherit all their properties e.g. group.length

Since groups contain only references, a sprite can be in multiple groups and deleting a group doesn't affect the sprites themselves.

Sprite.remove() will also remove the sprite from all the groups it belongs to.

createSprite
(
  • image_or_animation
  • x
  • y
)
Object

Defined in lib/p5.game.js:162

A Sprite is the main building block of p5.game: an element able to store images or animations with a set of properties such as position and visibility.

A Sprite can have a collider that defines the active area to detect collisions or overlappings with other sprites and mouse interactions.

Sprites created using createSprite (the preferred way) are added to the allSprites group and given a depth value that puts it in front of all other sprites.

Once you create a sprite, you must then draw the sprite in the draw() function. See drawSprite() and drawSprites()

Parameters:

  • image_or_animation String | p5.Image | Animation

    A loaded image or animation

  • [x] Number optional

    Initial x coordinate

  • [y] Number optional

    Initial y coordinate

Returns:

Object:

The new sprite instance

Example:

# Using a preloaded image or animation (recommended)
let mySprite = createSprite(mySpriteImage, 150, 200)

# Specifying a string for the image path
let mySprite = createSprite('assets/character.png', 140, 200)
createSprite
(
  • width
  • height
  • x
  • y
)
Object

Defined in lib/p5.game.js:193

Alternatively, you can pass a width, height instead of an image as the first argument to create a placeholder sprite.

Parameters:

  • width Number

    Width of the placeholder rectangle and of the collider until an image or new collider are set

  • height Number

    Height of the placeholder rectangle and of the collider until an image or new collider are set

  • x Number

    Initial x coordinate

  • y Number

    Initial y coordinate

Returns:

Object:

The new sprite instance

Example:

# Create a placeholder sprite
let mySprite = createSprite(30, 30, 150, 200)

# Add a pre-loaded image to the sprite
mySprite.addImage('normal', mySpriteImage)
drawSprite
(
  • sprite
)

Defined in lib/p5.game.js:333

Displays a Sprite. To be typically used in the main draw function.

Parameters:

  • sprite Sprite

    Sprite to be displayed

drawSprites
(
  • group
)

Defined in lib/p5.game.js:312

Displays a Group of sprites. If no parameter is specified, draws all sprites in the sketch. The drawing order is determined by the Sprite property "depth"

Parameters:

  • [group] Group optional

    Group of Sprites to be displayed

getSprites () Array

Defined in lib/p5.game.js:281

Returns all the sprites in the sketch as an array

Returns:

Array:

Array of Sprites

keyDown
(
  • key
)
Boolean

Defined in lib/p5.game.js:495

Detects if a key is currently pressed Like p5 keyIsDown but accepts strings and codes

Parameters:

  • key Number | String

    Key code or character

Returns:

Boolean:

True if the key is down

keyWentDown
(
  • key
)
Boolean

Defined in lib/p5.game.js:468

Detects if a key was pressed during the last cycle. It can be used to trigger events once, when a key is pressed or released. Example: Super Mario jumping.

Parameters:

  • key Number | String

    Key code or character

Returns:

Boolean:

True if the key was pressed

keyWentUp
(
  • key
)
Boolean

Defined in lib/p5.game.js:482

Detects if a key was released during the last cycle. It can be used to trigger events once, when a key is pressed or released. Example: Spaceship shooting.

Parameters:

  • key Number | String

    Key code or character

Returns:

Boolean:

True if the key was released

loadAnimation
(
  • fileName1
  • fileName2
  • fileNameN
)

Defined in lib/p5.game.js:345

Loads an animation. To be typically used in the preload() function of the sketch.

An Animation object contains a series of images (p5.Image) that can be displayed sequentially.

All files must be png images. You must include the directory from the sketch root, and the extension .png

A sprite can have multiple labeled animations, see Sprite.addAnimation and Sprite.changeAnimation, however an animation can be used independently.

An animation can be created either by passing a series of file names, no matter how many or by passing the first and the last file name of a numbered sequence. p5.game will try to detect the sequence pattern.

For example if the given filenames are "data/file0001.png" and "data/file0005.png" the images "data/file0003.png" and "data/file0004.png" will be loaded as well.

Parameters:

  • fileName1 String

    First file in a sequence OR first image file

  • fileName2 String

    Last file in a sequence OR second image file

  • [fileNameN] String optional multiple

    Any number of image files after the first two

Example:

let sequenceAnimation;
let glitch;

function preload() {
  sequenceAnimation = loadAnimation("data/walking0001.png", "data/walking0005.png");
  glitch = loadAnimation("data/dog.png", "data/horse.png", "data/cat.png", "data/snake.png");
}

function setup() {
  createCanvas(800, 600);
}

function draw() {
  background(0);
  animation(sequenceAnimation, 100, 100);
  animation(glitch, 200, 100);
}
loadSpriteSheet
(
  • image
  • frameWidth
  • frameHeight
)

Defined in lib/p5.game.js:395

Loads a Sprite Sheet. To be typically used in the preload() function of the sketch.

Returns a SpriteSheet istance which represents a sprite sheet and all it's frames. To be used with Animation, or static drawing single frames.

This is the first way to load a sprite sheet, given a width and height that will be used for every frame and the number of frames to cycle through. The sprite sheet must have a uniform grid with consistent rows and columns.

Parameters:

  • image String

    path or p5.Image object

  • frameWidth Number

    width of each frame

  • frameHeight Number

    height of each frame

Example:

// Method 1 - Using width, height for each frame and number of frames
explode_sprite_sheet = loadSpriteSheet('assets/explode_sprite_sheet.png', 171, 158, 11);
loadSpriteSheet
(
  • image
  • frames
)

Defined in lib/p5.game.js:416

Alternative method of loading a spritesheet given an array of frame objects that define the position and dimensions of each frame. This is Flexible because you can use sprite sheets that don't have uniform rows and columns.

A JSON file containing the frame definitions might look like this:

[
  {"name":"player_walk01", "frame":{"x":0, "y": 0, "width": 70, "height": 94}},
  {"name":"player_walk02", "frame":{"x":71, "y": 0, "width": 72, "height": 94}},
  {"name":"player_walk03", "frame":{"x":142, "y": 0, "width": 71, "height": 94}}
]

Parameters:

  • image String

    image path or p5.Image object

  • frames Array

    array of frame objects

Example:

// Method 2 - Using an array of objects that define each frame
var player_frames = loadJSON('assets/tiles.json');
player_sprite_sheet = loadSpriteSheet('assets/player_spritesheet.png', player_frames);
mouseDown
(
  • buttonCode
)
Boolean

Defined in lib/p5.game.js:543

Detects if a mouse button is currently down Combines mouseIsPressed and mouseButton of p5

Parameters:

  • [buttonCode] Number optional

    Mouse button constant LEFT, RIGHT or CENTER

Returns:

Boolean:

True if the button is down

mouseUp
(
  • buttonCode
)
Boolean

Defined in lib/p5.game.js:555

Detects if a mouse button is currently up Combines mouseIsPressed and mouseButton of p5

Parameters:

  • [buttonCode] Number optional

    Mouse button constant LEFT, RIGHT or CENTER

Returns:

Boolean:

True if the button is up

mouseWentDown
(
  • buttonCode
)
Boolean

Defined in lib/p5.game.js:580

Detects if a mouse button was pressed during the last cycle. It can be used to trigger events once, to be checked in the draw cycle

Parameters:

  • [buttonCode] Number optional

    Mouse button constant LEFT, RIGHT or CENTER

Returns:

Boolean:

True if the button was just pressed

mouseWentUp
(
  • buttonCode
)
Boolean

Defined in lib/p5.game.js:567

Detects if a mouse button was released during the last cycle. It can be used to trigger events once, to be checked in the draw cycle

Parameters:

  • [buttonCode] Number optional

    Mouse button constant LEFT, RIGHT or CENTER

Returns:

Boolean:

True if the button was just released

removeSprite
(
  • sprite
)

Defined in lib/p5.game.js:246

Removes a Sprite from the sketch. The removed Sprite won't be drawn or updated anymore. Equivalent to Sprite.remove()

Parameters:

  • sprite Object

    Sprite to be removed

updateSprites
(
  • updating
)

Defined in lib/p5.game.js:258

Updates all the sprites in the sketch (position, animation...) it's called automatically at every draw(). It can be paused by passing a parameter true or false; Note: it does not render the sprites.

Parameters:

  • updating Boolean

    false to pause the update, true to resume

useQuadTree
(
  • use
)

Defined in lib/p5.game.js:805

Turns the quadTree on or off. A quadtree is a data structure used to optimize collision detection. It can improve performance when there is a large number of Sprites to be checked continuously for overlapping.

p5.game will create and update a quadtree automatically.

Parameters:

  • use Boolean

    Pass true to enable, false to disable

Properties

allSprites

Group

Defined in lib/p5.game.js:149

A Group containing all the sprites in the sketch.

camera

Camera

Defined in lib/p5.game.js:46

The sketch camera automatically created at the beginning of a sketch. A camera facilitates scrolling and zooming for scenes extending beyond the canvas. A camera has a position, a zoom factor, and the mouse coordinates relative to the view.

In p5.js terms the camera wraps the whole drawing cycle in a transformation matrix but it can be disabled anytime during the draw cycle, for example to draw interface elements in an absolute position.

KEY

Object private

Defined in lib/p5.game.js:622

An object storing all useful keys for easy access Key.tab = 9

KEY_DEPRECATIONS

Object private

Defined in lib/p5.game.js:725

An object storing deprecated key aliases, which we still support but should be mapped to valid aliases and generate warnings.