Sprites¶
Sprites are game objects.
To use a sprite you use BaseScene.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
(**kwargs)[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
-
bottom
¶ The y-axis position of the bottom of the object.
Can be set to a number.
-
bottom_left
¶ The coordinates of the bottom left corner of the object.
Can be set to a
ppb_vector.Vector
.
-
bottom_middle
¶ The coordinates of the midpoint of the bottom of the object.
Can be set to a
ppb_vector.Vector
.
-
center
¶ The coordinates of the center point of the object. Equivalent to the
position
.Can be set to a
ppb_vector.Vector
.
-
facing
¶ The direction the “front” is facing.
Can be set to an arbitrary facing by providing a facing vector.
-
left
¶ The x-axis position of the left side of the object.
Can be set to a number.
-
left_middle
¶ The coordinates of the midpoint of the left side of the object.
Can be set to a
ppb_vector.Vector
.
-
right
¶ The x-axis position of the right side of the object.
Can be set to a number.
-
right_middle
¶ 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).
-
rotation
¶ The amount the sprite is rotated, in degrees
-
top
¶ The y-axis position of the top of the object.
Can be set to a number.
-
top_left
¶ The coordinates of the top left corner of the object.
Can be set to a
ppb_vector.Vector
.
-
top_middle
¶ The coordinates of the midpoint of the top of the object.
Can be set to a
ppb_vector.Vector
.
-
top_right
¶ The coordinates of the top right corner of the object.
Can be set to a
ppb_vector.Vector
.
-
-
class
ppb.
RectangleSprite
(**kwargs)[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
-
bottom
¶ The y-axis position of the bottom of the object.
Can be set to a number.
-
bottom_left
¶ The coordinates of the bottom left corner of the object.
Can be set to a
ppb_vector.Vector
.
-
bottom_middle
¶ The coordinates of the midpoint of the bottom of the object.
Can be set to a
ppb_vector.Vector
.
-
center
¶ The coordinates of the center point of the object. Equivalent to the
position
.Can be set to a
ppb_vector.Vector
.
-
facing
¶ The direction the “front” is facing.
Can be set to an arbitrary facing by providing a facing vector.
-
left
¶ The x-axis position of the left side of the object.
Can be set to a number.
-
left_middle
¶ The coordinates of the midpoint of the left side of the object.
Can be set to a
ppb_vector.Vector
.
-
right
¶ The x-axis position of the right side of the object.
Can be set to a number.
-
right_middle
¶ 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).
-
rotation
¶ The amount the sprite is rotated, in degrees
-
top
¶ The y-axis position of the top of the object.
Can be set to a number.
-
top_left
¶ The coordinates of the top left corner of the object.
Can be set to a
ppb_vector.Vector
.
-
top_middle
¶ The coordinates of the midpoint of the top of the object.
Can be set to a
ppb_vector.Vector
.
-
top_right
¶ 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
-
facing
¶ The direction the “front” is facing.
Can be set to an arbitrary facing by providing a facing vector.
-
rotation
¶ The amount the sprite is rotated, in degrees
-
-
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.-
bottom
¶ The y-axis position of the bottom of the object.
Can be set to a number.
-
bottom_left
¶ The coordinates of the bottom left corner of the object.
Can be set to a
ppb_vector.Vector
.
-
bottom_middle
¶ The coordinates of the midpoint of the bottom of the object.
Can be set to a
ppb_vector.Vector
.
-
center
¶ The coordinates of the center point of the object. Equivalent to the
position
.Can be set to a
ppb_vector.Vector
.
-
height
= 1¶ The height of the sprite.
-
left
¶ The x-axis position of the left side of the object.
Can be set to a number.
-
left_middle
¶ The coordinates of the midpoint of the left side of the object.
Can be set to a
ppb_vector.Vector
.
-
right
¶ The x-axis position of the right side of the object.
Can be set to a number.
-
right_middle
¶ The coordinates of the midpoint of the right side of the object.
Can be set to a
ppb_vector.Vector
.
-
top
¶ The y-axis position of the top of the object.
Can be set to a number.
-
top_left
¶ The coordinates of the top left corner of the object.
Can be set to a
ppb_vector.Vector
.
-
top_middle
¶ The coordinates of the midpoint of the top of the object.
Can be set to a
ppb_vector.Vector
.
-
top_right
¶ The coordinates of the top right corner of the object.
Can be set to a
ppb_vector.Vector
.
-
width
= 1¶ The width of the sprite.
-
-
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.
Base Classes¶
The base class of Sprite, use this if you need to change the low level expectations.
-
class
ppb.sprites.
BaseSprite
(**kwargs)[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
-
layer
= 0¶ The layer a sprite exists on.
-
position
= Vector(0.0, 0.0)¶ (
ppb.Vector
): Location of the sprite