Sprites#
Sprites are game objects.
To use a sprite you use Scene.add
to add it
to a scene. When contained in an active scene, the engine will call the various
event
handlers on the sprite.
When defining your own custom sprites, we suggest you start with
Sprite
. By subclassing Sprite
, you get a number of
features automatically. You then define your event handlers as methods on your
new class to produce behaviors.
All sprites in ppb are built from composition via mixins or subclassing via traditional Python inheritance.
If you don’t need the built in features of Sprite
see
BaseSprite
.
Concrete Sprites#
Concrete sprites are a combination of BaseSprite
and various mixins.
They implement a number of useful features for game development and should be
the primary classes you subclass when building game objects.
- class ppb.Sprite(**props)[source]#
The default Sprite class.
Sprite defines no additional methods or attributes, but is made up of
BaseSprite
with the mixinsRotatableMixin
,RenderableMixin
, andSquareShapeMixin
.For most use cases, this is probably the class you want to subclass to make your game objects.
If you need rectangular sprites instead of squares, see
RectangleSprite
.BaseSprite
does not accept any positional arguments, and uses keyword arguments to set arbitrary state to theBaseSprite
instance. This allows rapid prototyping.Example:
sprite = BaseSprite(speed=6) print(sprite.speed)
This sample will print the numeral 6.
You may add any arbitrary data values in this fashion. Alternatively, it is considered best practice to subclass
BaseSprite
and set the default values of any required attributes as class attributes.Example:
class Rocket(ppb.sprites.BaseSprite): velocity = Vector(0, 1) def on_update(self, update_event, signal): self.position += self.velocity * update_event.time_delta
- add(child: GameObject, tags: Iterable = ()) None #
Shorthand for
Children.add()
- basis = Vector(0.0, -1.0)#
The baseline vector, representing the “front” of the sprite
- property bottom_left: Vector#
The coordinates of the bottom left corner of the object.
Can be set to a
ppb_vector.Vector
.
- property bottom_middle: Vector#
The coordinates of the midpoint of the bottom of the object.
Can be set to a
ppb_vector.Vector
.
- property center: Vector#
The coordinates of the center point of the object. Equivalent to the
position
.Can be set to a
ppb_vector.Vector
.
- property facing#
The direction the “front” is facing.
Can be set to an arbitrary facing by providing a facing vector.
- property height#
The height of the sprite.
Setting the height of the sprite changes the
size
andwidth()
.
- property left_middle: Vector#
The coordinates of the midpoint of the left side of the object.
Can be set to a
ppb_vector.Vector
.
- position: Vector = Vector(0.0, 0.0)#
(
ppb.Vector
): Location of the sprite
- remove(child: GameObject) GameObject #
Shorthand for
Children.remove()
- property right_middle: Vector#
The coordinates of the midpoint of the right side of the object.
Can be set to a
ppb_vector.Vector
.
- rotate(degrees)#
Rotate the sprite by a given angle (in degrees).
- size = 1#
The width and height of the object. Setting size changes the
height()
andwidth()
of the sprite.
- property top_left: Vector#
The coordinates of the top left corner of the object.
Can be set to a
ppb_vector.Vector
.
- property top_middle: Vector#
The coordinates of the midpoint of the top of the object.
Can be set to a
ppb_vector.Vector
.
- property top_right: Vector#
The coordinates of the top right corner of the object.
Can be set to a
ppb_vector.Vector
.
- class ppb.RectangleSprite(**props)[source]#
A rectangle sprite.
Similarly to
Sprite
,RectangleSprite
does not introduce any new methods or attributes. It’s made up ofBaseSprite
with the mixinsRotatableMixin
,RenderableMixin
, andRectangleShapeMixin
.BaseSprite
does not accept any positional arguments, and uses keyword arguments to set arbitrary state to theBaseSprite
instance. This allows rapid prototyping.Example:
sprite = BaseSprite(speed=6) print(sprite.speed)
This sample will print the numeral 6.
You may add any arbitrary data values in this fashion. Alternatively, it is considered best practice to subclass
BaseSprite
and set the default values of any required attributes as class attributes.Example:
class Rocket(ppb.sprites.BaseSprite): velocity = Vector(0, 1) def on_update(self, update_event, signal): self.position += self.velocity * update_event.time_delta
- add(child: GameObject, tags: Iterable = ()) None #
Shorthand for
Children.add()
- basis = Vector(0.0, -1.0)#
The baseline vector, representing the “front” of the sprite
- property bottom_left: Vector#
The coordinates of the bottom left corner of the object.
Can be set to a
ppb_vector.Vector
.
- property bottom_middle: Vector#
The coordinates of the midpoint of the bottom of the object.
Can be set to a
ppb_vector.Vector
.
- property center: Vector#
The coordinates of the center point of the object. Equivalent to the
position
.Can be set to a
ppb_vector.Vector
.
- property facing#
The direction the “front” is facing.
Can be set to an arbitrary facing by providing a facing vector.
- property left_middle: Vector#
The coordinates of the midpoint of the left side of the object.
Can be set to a
ppb_vector.Vector
.
- position: Vector = Vector(0.0, 0.0)#
(
ppb.Vector
): Location of the sprite
- remove(child: GameObject) GameObject #
Shorthand for
Children.remove()
- property right_middle: Vector#
The coordinates of the midpoint of the right side of the object.
Can be set to a
ppb_vector.Vector
.
- rotate(degrees)#
Rotate the sprite by a given angle (in degrees).
- property top_left: Vector#
The coordinates of the top left corner of the object.
Can be set to a
ppb_vector.Vector
.
- property top_middle: Vector#
The coordinates of the midpoint of the top of the object.
Can be set to a
ppb_vector.Vector
.
- property top_right: Vector#
The coordinates of the top right corner of the object.
Can be set to a
ppb_vector.Vector
.
Feature Mixins#
These mixins are the various features already available in Sprite. Here for complete documentation.
- class ppb.sprites.RenderableMixin[source]#
A class implementing the API expected by ppb.systems.renderer.Renderer.
The render expects a width and height (see
RectangleMixin
) and will skip rendering if a sprite has no shape. You can useRectangleMixin
,SquareMixin
, or set the values yourself.Additionally, if
image
isNone
, the sprite will not be rendered. If you just want a basic shape to be rendered, seeppb.assets
.
- class ppb.sprites.RotatableMixin[source]#
A rotation mixin. Can be included with sprites.
Warning
rotation does not affect underlying shape (the corners are still in the same place), it only rotates the sprites image and provides a facing.
- basis = Vector(0.0, -1.0)#
The baseline vector, representing the “front” of the sprite
- property facing#
The direction the “front” is facing.
Can be set to an arbitrary facing by providing a facing vector.
- class ppb.sprites.RectangleShapeMixin[source]#
A Mixin that provides a rectangular area to sprites.
Classes derived from RectangleShapeMixin default to the same size and shape as all ppb Sprites: A 1 game unit by 1 game unit square. Just set the width and height in your constructor (Or as
class attributes
) to change this default.Note
The concrete class using
RectangleShapeMixin
must have aposition
attribute.- property bottom_left: Vector#
The coordinates of the bottom left corner of the object.
Can be set to a
ppb_vector.Vector
.
- property bottom_middle: Vector#
The coordinates of the midpoint of the bottom of the object.
Can be set to a
ppb_vector.Vector
.
- property center: Vector#
The coordinates of the center point of the object. Equivalent to the
position
.Can be set to a
ppb_vector.Vector
.
- property left_middle: Vector#
The coordinates of the midpoint of the left side of the object.
Can be set to a
ppb_vector.Vector
.
- property right_middle: Vector#
The coordinates of the midpoint of the right side of the object.
Can be set to a
ppb_vector.Vector
.
- property top_left: Vector#
The coordinates of the top left corner of the object.
Can be set to a
ppb_vector.Vector
.
- property top_middle: Vector#
The coordinates of the midpoint of the top of the object.
Can be set to a
ppb_vector.Vector
.
- property top_right: Vector#
The coordinates of the top right corner of the object.
Can be set to a
ppb_vector.Vector
.
- class ppb.sprites.SquareShapeMixin[source]#
A Mixin that provides a square area to sprites.
Extends the interface of
RectangleShapeMixin
by using thesize
attribute to determinewidth()
andheight()
. Setting eitherwidth()
orheight()
sets thesize
and maintains the square shape at the new size.The default size of
SquareShapeMixin
is 1 game unit.Please see
RectangleShapeMixin
for additional details.- property height#
The height of the sprite.
Setting the height of the sprite changes the
size
andwidth()
.
Base Classes#
The base class of Sprite, use this if you need to change the low level expectations.
- class ppb.sprites.BaseSprite(**props)[source]#
The base Sprite class. All sprites should inherit from this (directly or indirectly).
The things that define a BaseSprite:
A position vector
A layer
BaseSprite provides an
__init__()
method that sets attributes based on kwargs to make rapid prototyping easier.BaseSprite
does not accept any positional arguments, and uses keyword arguments to set arbitrary state to theBaseSprite
instance. This allows rapid prototyping.Example:
sprite = BaseSprite(speed=6) print(sprite.speed)
This sample will print the numeral 6.
You may add any arbitrary data values in this fashion. Alternatively, it is considered best practice to subclass
BaseSprite
and set the default values of any required attributes as class attributes.Example:
class Rocket(ppb.sprites.BaseSprite): velocity = Vector(0, 1) def on_update(self, update_event, signal): self.position += self.velocity * update_event.time_delta
- add(child: GameObject, tags: Iterable = ()) None #
Shorthand for
Children.add()
- position: Vector = Vector(0.0, 0.0)#
(
ppb.Vector
): Location of the sprite
- remove(child: GameObject) GameObject #
Shorthand for
Children.remove()