World > Camera#

Besides scrolling and coordinate conversion, world.camera also offers the public helper methods add_right(...), add_bottom(...), remove_world(...), and switch_world(...).

API Reference#

class miniworlds.worlds.manager.camera_manager.CameraManager(view_x, view_y, world)[source]#

CameraManager defines a movable viewport into a 2D world and tracks visible actors. It is accessed via world.camera and is responsible for view positioning, actor visibility, and coordinate transformations.

For public API compatibility, docking and world-switching helpers are also exposed through world.camera and delegated internally to the layout manager.

Public Data Attributes:

width

Width of the camera viewport in pixels.

height

Height of the camera viewport in pixels.

world_size_x

Width of the world in pixels.

world_size_y

Height of the world in pixels.

world_size

Returns world size as (width, height).

x

Camera x-position (top-left corner in world coordinates).

y

Camera y-position (top-left corner in world coordinates).

topleft

Top-left corner of the camera in world coordinates.

rect

Returns camera rect in world coordinates.

screen_rect

Returns camera rect in screen coordinates.

world_rect

Returns the full world rect.

window_docking_position

Returns the docking position of this world in the application window.

center

Returns center of camera in world coordinates.

Inherited from Sprite

image

rect

layer

Dynamic, read only property for protected _layer attribute.

Public Methods:

__init__(view_x, view_y, world)

get_rect()

Returns the camera rectangle in world coordinates.

get_screen_rect()

Returns the camera rectangle in screen coordinates.

get_world_rect()

Returns the full world rectangle from the camera's current origin.

get_screen_position(pos)

Convert world coordinates to screen coordinates.

get_local_position(pos)

Convert world coordinates to camera-local coordinates.

get_global_coordinates_for_world(pos)

Convert local camera position to global world position.

get_actors_in_view()

is_actor_in_view(actor)

from_actor(actor)

Move camera to center on a given actor.

add_right(world[, size])

Dock a helper world to the right side of the current world.

add_bottom(world[, size])

Dock a helper world below the current world.

remove_world(world)

Remove a previously docked world.

switch_world(new_world[, reset])

Switch to another world through the public camera API.

is_in_screen(pixel)

Inherited from Sprite

__init__(*groups)

add(*groups)

add the sprite to groups

remove(*groups)

remove the sprite from groups

add_internal(group)

For adding this sprite to a group internally.

remove_internal(group)

For removing this sprite from a group internally.

update(*args, **kwargs)

method to control sprite behavior

kill()

remove the Sprite from all Groups

groups()

list of Groups that contain this Sprite

alive()

does the sprite belong to any groups

__repr__()

Return repr(self).

Private Methods:

_disable_resize()

Disable automatic resize handling.

_enable_resize()

Re-enable automatic resize handling.

_set_width(value)

_set_height(value)

_set_topleft(value)

_limit_x(value)

_limit_y(value)

_reload_actors_in_view()

Marks actors in view as dirty for redraw.

_should_repaint_actor(actor)

_reload_camera()

Internal: reloads geometry and triggers world resize.

_clear_camera_cache()

Clears view tracking for actor visibility.

_update()

Called each frame to refresh geometry if needed.

_cache_rects()

Caches world and screen rects.


add_bottom(world, size=100)[source]#

Dock a helper world below the current world.

Return type:

World

add_right(world, size=100)[source]#

Dock a helper world to the right side of the current world.

Return type:

World

property center: Tuple[float, float]#

Returns center of camera in world coordinates.

from_actor(actor)[source]#

Move camera to center on a given actor.

Return type:

None

Examples

>>> world.camera.from_actor(actor)
get_actors_in_view()[source]#
Return type:

Set[Actor]

get_global_coordinates_for_world(pos)[source]#

Convert local camera position to global world position.

Return type:

Tuple[int, int]

Parameters:

pos – Position relative to the camera.

Returns:

Global world position.

Examples

>>> world.camera.get_global_coordinates_for_world((100, 50))
get_local_position(pos)[source]#

Convert world coordinates to camera-local coordinates.

Return type:

Tuple[int, int]

Parameters:

pos – Global position in the world.

Returns:

Local position relative to the camera.

Examples

>>> world.camera.get_local_position((500, 400))
get_rect()[source]#

Returns the camera rectangle in world coordinates.

Return type:

Rect

Returns:

A pygame.Rect representing the current viewport’s world area.

Examples

>>> world.camera.get_rect()
get_screen_position(pos)[source]#

Convert world coordinates to screen coordinates.

Return type:

Tuple[int, int]

Parameters:

pos – Position in world coordinates.

Returns:

Position in screen coordinates.

Examples

>>> world.camera.get_screen_position((100, 200))
get_screen_rect()[source]#

Returns the camera rectangle in screen coordinates.

Return type:

Rect

Returns:

A pygame.Rect representing where the camera appears on screen.

Examples

>>> world.camera.get_screen_rect()
get_world_rect()[source]#

Returns the full world rectangle from the camera’s current origin.

Return type:

Rect

Returns:

A pygame.Rect representing the visible and scrolled world area.

Examples

>>> world.camera.get_world_rect()
property height: int#

Height of the camera viewport in pixels.

is_actor_in_view(actor)[source]#
Return type:

bool

is_in_screen(pixel)[source]#
Return type:

bool

property rect: Rect#

Returns camera rect in world coordinates.

remove_world(world)[source]#

Remove a previously docked world.

Return type:

None

property screen_rect: Rect#

Returns camera rect in screen coordinates.

switch_world(new_world, reset=False)[source]#

Switch to another world through the public camera API.

Return type:

None

property topleft: Tuple[int, int]#

Top-left corner of the camera in world coordinates.

property width: int#

Width of the camera viewport in pixels.

property window_docking_position: str | None#

Returns the docking position of this world in the application window.

property world_rect: Rect#

Returns the full world rect.

property world_size: Tuple[int, int]#

Returns world size as (width, height).

property world_size_x: int#

Width of the world in pixels.

property world_size_y: int#

Height of the world in pixels.

property x: int#

Camera x-position (top-left corner in world coordinates).

property y: int#

Camera y-position (top-left corner in world coordinates).