The Scene

class wasabi2d.Scene(width: int = 800, height: int = 600, title: int = 'wasabi2d', rootdir: Optional[str] = None, scaler: Union[str, bool, None] = False)

Top-level interface for renderable objects.

Parameters
  • width – The width of the window to create, in pixels.

  • height – The height of the window to create, in pixels.

  • rootdir – The root directory to load for asset loading. Images will be loaded from the images/ directory inside rootdir, for example, and corresponding directories for sounds, fonts, and music.

  • title – The initial window title.

  • scaler – If True or a string, activate scene scaling using the named scaler, or ‘nearest’ if True is given.

The Scene object represents the whole graphics subsystem in wasabi2d. Scenes manage a collection of graphical primitives within a number of layers.

Create a scene with:

from wasabi2d import Scene

scene = Scene(width, height)
Scene.background

Get/set the background color of the entire scene as an RGB triple. (1, 1, 1) is white and (0, 0, 0) is black.

Scene.layers

The collection of layers that will be drawn. Layers are created on access and do not need to be explicitly declared.

Layers are drawn from back to front - lowest layer number to highest.

Scene.title

Get/set the caption for the window.

Scene.width

(read-only) The width of the scene in pixels.

Scene.height

(read-only) The height of the scene in pixels.

Scene.camera

(read-only) The Camera object used to render the scene. See Camera.

There’s also an off-screen version, which will mainly be useful for testing and screenshotting:

class wasabi2d.scene.HeadlessScene(width: int = 800, height: int = 600, rootdir: Optional[str] = None)

A scene that doesn’t create a window.

This can be used in automated applications and for testing.

Scene scaling

Scene scaling allows the wasabi2d scene to be scaled independently of the final window size. Two uses of this are:

  • Supporting high dpi/retina displays with practically the same performance as standard displays (but reduced quality).

  • Retro games, in order to draw everything pixellated.

The scene will aways have the requested dimensions, but the window will usually be a bigger size; the biggest integer multiple that fits your screen. The scaler in use determines how the scene is upscaled to the window.

To use this, set scaler to one of:

  • True or 'nearest' - nearest pixel; creates a pixellated appearance. Ideal for retro games.

  • 'linear' - linearly interpolate between pixel values.

For example, to create a retro game with a 200x100 pixel screen (very blocky indeed), create a scene with a small viewport size, but set scaler to True:

scene = Scene(
    width=200,
    height=120,
    scaler=True
)

This will create a scene that is logically 200x120:

  • All rendering will take place on a 200x120 frame buffer.

  • Mouse coordinates will be correctly translated to scene coordinates, eg. the bottom right corner is (199, 119).

Coordinate system

Unusually for an OpenGL-based game engine, wasabi2d uses Pygame’s coordinate system where the top of the screen has coordinate 0 and coordinates increase downwards.

This allows easier porting of Pygame Zero games.

By default distances are measured in screen pixels.

Camera

The camera for the scene is scene.camera.

wasabi2d.scene.Camera.pos

Get/set the center position of the camera, as a 2D vector/pair of floats.

Initially, this is (scene.width / 2, scene.height / 2).

Camera.screen_shake(dist=25)

Trigger a screen shake effect.

The camera will be offset from .pos by dist in a random direction; then steady itself in a damped harmonic motion.

Trigger a screen shake effect.

The camera will be offset from .pos by dist in a random direction; then steady itself in a damped harmonic motion.

The effect is added to the value of .pos; getting/setting pos moves the camera independently of the shake effect.

Screenshot and Video Recording

Games automatically have access to screenshot and video recording capabilities.

This is hard coded to:

  • F12 - take a screenshot, named with an automatic filename.

  • Shift-F12 - start/stop recording a video, named with an automatic filename.

New in version 1.3.0: Hard-coded screenshot/video capture to F12.

Recording video requires ffmpeg to be installed and on the $PATH.

As well as this, you can use these features programmatically:

Scene.screenshot(filename: Optional[str] = None) → str

Take a screenshot.

If filename is not given, save to a file named screenshot_<time>.png in the current directory. Return the filename.

Scene.record_video(filename=None)

Start recording a video.

Video will be encoded in MPEG4 format.

This requires an ffmpeg binary to be located on $PATH.

If filename is not given, save to a file named video_<time>.mp4 in the current directory.

Scene.stop_recording()

Finish recording the current video.

Scene.toggle_recording() → bool

Start or stop recording video.

Return True if recording started.