| |
- __builtin__.object
-
- Camera
class Camera(__builtin__.object) |
|
A Camera provides a few services:
* Track a moving target in world coordinates.
* Convert coordinates between world and screen space.
* Interpolated view scrolling to take advantage of higher frame rates.
Dependencies:
* A target with a Vec2d attribute target.position.
* A surface from which to derive its viewing dimensions.
* State.clock is needed by interpolate().
To use the camera do the following:
1. In the game's update routine set camera.position to move the target.
2. Also in the game's update routine call Camera.update() to update the
camera's state.
3. In the game's draw routine call Camera.interpolate() to micro-update
the scrolling between the position prior to step 1 and final
position specified in step 1. This may sound complicated, but it is
all handled behind the scenes.
Screen-to-world and world-to-screen conversions are used to convert between
world coordinates and screen coordinates.
Note that using mouse position can be tricky if the camera is using a
subsurface of the screen, or an alternate surface. pygame always reports
mouse position relative to the top-level surface. Keep this in mind when
positioning graphics based on the mouse position under these circumstances.
Sometimes it may just be simplest, for example, to blit directly to the
top-level surface.
If creating multiple cameras to save and restore in State contexts, by
default the state_restored() method updates the new camera from the old.
This solves a split-brain condition that occurs when the cameras' internals
fall out of sync. In some scenarios you may want to have two independent
cameras. To prevent them from syncing during a restore, set the cameras'
update_when_restored=False. |
|
Methods defined here:
- __init__(self, target, view=None)
- Construct an instance of Camera.
The target argument is the object that camera should track. target must
have a position attribute which is its location in world coordinates.
The view argument is the screen.View object upon which to base
conversions between world and screen space. The view.surface attribute
is exposed view the Camera.surface property.
- anti_interp(self, obj)
- Return x,y to reverse interpolation jitter on an object
obj must have a pygame rect attribute.
Example:
screen.blit(thing.image, thing.rect.move(camera.anti_interp))
- init_position(self, pos)
- Hard set position to pos.
This circumvents interpolation, which may be desirable if for example
setting the initial position of the camera, or moving the camera a
great distance when you don't want it to pan.
- interpolate(self, *args)
- Interpolate camera position towards target for smoother scrolling
After updating the target position in the main program's update(), call
this every frame in the main program's draw() before any drawing
commands. It works best when frame speed is much higher than update
speed.
- screen_to_world(self, xy)
- Convert coordinates from screen space to world space.
- state_restored(self, prev)
- Sync a stale camera after swapping it in.
If switching states either manually, you may want to call this to
avoid video flashing or whizzing by. This typically happens when using
Camera.interpolate() and swapping in the old camera, which has stale
values in the _move_to and _move_from attributes. When swapping a camera
in via State.restore(), this method is called automatically.
- update(self, *args)
- Update Camera internals to prepare for efficient interpolation.
Call in the game's update routine after changing Camera.position.
- world_to_screen(self, xy)
- Convert coordinates from world space to screen space.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
- abs_offset
- Offset position camera.subsurface inside its top level parent surface.
This is equivalent to camera.surface.get_abs_offset(). The value is
cached whenever the camera.surface attribute is set.
- abs_screen_center
- The absolute coordinates of the camera surface's center.
In general, this is typically useful in mouse-map calculations.
This is equivalent to camera.surface.get_rect().center +
camera.surface.abs_offset(). The value is cached whenever the
camera.surface attribute is set.
- interp
- The clock's interpolation value after the last call to
Camera.interpolate.
- position
- Move Camera.target in world coordinates.
- screen_center
- The coordinates of the camera surface's center.
In general, this is typically useful in screen-map calculations.
This is equivalent to camera.surface.get_rect().center. The value is
cached whenever the camera.surface attribute is set.
- steady_target_position
- Return x,y to reverse interpolation jitter on the camera target
Example:
screen.blit(thing.image, thing.rect.move(camera.steady_target_position))
- surface
- The surface from which to derive the viewing dimensions.
- target
- The target that camera is tracking.
- view
- The view from which to derive the surface and viewing dimensions and,
for subsurfaces, the rect for the subsurface in the parent surface.
| |