Camera#

Cameras are objects that straddle the line between game space and screen space. The renderer uses the position of the camera to translate Sprite's positions to the screen in order to make them visible.

The Renderer inserts a Camera into the current scene in response to the SceneStarted.

class ppb.camera.Camera(renderer, target_game_unit_width: Real, viewport_dimensions: Tuple[int, int])[source]#

A simple Camera.

Intentionally tightly coupled to the renderer to allow information flow back and forth.

There is a one-to-one relationship between cameras and scenes.

You can subclass Camera to add event handlers. If you do so, set the camera_class class attribute of your scene to your subclass. The renderer will instantiate the correct type.

You shouldn’t instantiate your own camera in general. If you want to override the Camera, see above.

Parameters:
  • renderer (Renderer) – The renderer associated with the camera.

  • target_game_unit_width (Real) – The number of game units wide you would like to display. The actual width may be less than this depending on the ratio to the viewport (as it can only be as wide as there are pixels.)

  • viewport_dimensions (Tuple[int, int]) – The pixel dimensions of the rendered viewport in (width, height) form.

property height: Real#

The game unit height of the viewport.

See ppb.sprites for details about game units.

When setting this property, the resulting height may be slightly different from the value provided based on the ratio between the height of the window in screen pixels and desired number of game units to represent.

When you set the height, the width will change as well.

point_is_visible(point: Vector) bool[source]#

Determine if a given point is in view of the camera.

Parameters:

point (Vector) – A vector representation of a point in game units.

Returns:

Whether the point is in view or not.

Return type:

bool

sprite_in_view(sprite: Sprite) bool[source]#

Determine if a given sprite is in view of the camera.

Does not guarantee that the sprite will be rendered, only that it exists in the visible space.

A sprite without area (size=0 or lacking width, height, or any of the sides accessors) behave as point_is_visible().

Parameters:

sprite – The sprite to check

Type:

Sprite

Returns:

Whether the sprite is in the space in view of the camera.

Return type:

bool

translate_point_to_game_space(point: Vector) Vector[source]#

Convert a vector from screen position to game position.

Parameters:

point (Vector) – A vector in pixels

Returns:

A vector in game units.

Return type:

Vector

translate_point_to_screen(point: Vector) Vector[source]#

Convert a vector from game position to screen position.

Parameters:

point (Vector) – A vector in game units

Returns:

A vector in pixels.

Return type:

Vector

property width: Real#

The game unit width of the viewport.

See ppb.sprites for details about game units.

When setting this property, the resulting width may be slightly different from the value provided based on the ratio between the width of the window in screen pixels and desired number of game units to represent.

When you set the width, the height will change as well.

Warning

Setting the game unit dimensions of a camera (whether via Camera.width, Camera.height, or the target_game_unit_width of the Camera constructor) will affect both Camera.width and Camera.height. Their ratio is determined by the defined window.