Sprite class for use in Pygame programs at UCC
Project description
UCC Sprite Class
This module provides the Sprite
class. It is a convenience class that implements many desireable features for Pygame sprites.
Install the module
pip3 install ucc-sprite
Getting Started
from ucc_sprite import Sprite
...
image = pygame.image.load("image.png")
my_sprite = Sprite(image)
You can use the my_sprite
object as a Pygame sprite.
Table of Contents
- ucc_sprite
- Sprite
- __init__
- image
- rect
- size
- width
- height
- position
- top_left
- bottom_left
- top_right
- bottom_right
- mid_left
- mid_right
- mid_top
- mid_bottom
- center
- x
- y
- left
- right
- center_x
- top
- bottom
- center_y
- move_forward
- move_backward
- direction
- turn_left
- turn_right
- rotates
- speed
- layer
- contains_point
- mask_contains_point
- update
- draw
- Sprite
ucc_sprite
Sprite Objects
class Sprite(pygame.sprite.DirtySprite)
A Sprite represents an image that moves around the screen in a game.
Sprite objects store the following information necessary for drawing these images on the screen:
- The position of the sprite on the screen using coordinates
- The direction that the sprite is pointing using an angle measured counterclockwise from the positive x-axis.
Attributes and methods are provided for the following:
- Moving and turning the sprite
- Animating the sprite
__init__
def __init__(image, position=(0, 0), direction=0, speed=0, rotates=True)
Create a Sprite object with the provided file as its image
image
@property
def image()
The Surface that the sprite represents. This Surface is used when blitting the sprite.
rect
@property
def rect()
The Rect containing the size and position of the sprite. This Rect is used when blitting the sprite.
This property is readonly.
size
@property
def size()
The size (width, height) of the sprite's image.
This property is readonly.
width
@property
def width()
The width of the sprite's image.
This property is readonly.
height
@property
def height()
The height of the sprite's image.
This property is readonly.
position
@property
def position()
The current the position of the sprite on the screen.
top_left
@property
def top_left()
The coordinates of the top left corner of the sprite.
bottom_left
@property
def bottom_left()
The coordinates of the bottom left corner of the sprite.
top_right
@property
def top_right()
The coordinates of the top right corner of the sprite.
bottom_right
@property
def bottom_right()
The coordinates of the bottom right corner of the sprite.
mid_left
@property
def mid_left()
The coordinates of the middle of the left edge of the sprite.
mid_right
@property
def mid_right()
The coordinates of the middle of the right edge of the sprite.
mid_top
@property
def mid_top()
The coordinates of the middle of the top edge of the sprite.
mid_bottom
@property
def mid_bottom()
The coordinates of the middle of the bottom edge of the sprite.
center
@property
def center()
The coordinates of the center of the sprite.
x
@property
def x()
The x-coordinate of the sprite on the screen.
y
@property
def y()
The y-coordinate of the sprite on the screen.
left
@property
def left()
The x-coordinate of the left side of the sprite.
right
@property
def right()
The x-coordinate of the right side of the sprite.
center_x
@property
def center_x()
The x-coordinate of the center of the sprite.
top
@property
def top()
The y-coordinate of the top of the sprite.
bottom
@property
def bottom()
The y-coordinate of the bottom of the sprite.
center_y
@property
def center_y()
The y-coordinate of the center of the sprite.
move_forward
def move_forward(distance)
Move the sprite by the given distance
in the direction it is
currently pointing.
move_backward
def move_backward(distance)
Move the sprite by the given distance
in the opposite of the
direction it is currently pointing.
direction
@property
def direction()
The current direction that the sprite is pointing.
The direction is an angle (in degrees) counterclockwise from the positive x-axis. Here are some important directions:
- 0 degrees is directly to the right
- 90 degrees is directly up
- 180 degrees is directly to the left
- 270 degrees is directly down
turn_left
def turn_left(angle)
Turn the sprite left (counterclockwise) by the given angle
.
turn_right
def turn_right(angle)
Turn the sprite right (clockwise) by the given angle
.
rotates
@property
def rotates()
Whether or not the image rotates when the sprite changes direction.
speed
@property
def speed()
The distance that the sprite moves forward by on each update.
layer
@property
def layer()
The layer number to draw the sprite on. Higher layers are drawn on top.
contains_point
def contains_point(x, y=None)
Determine whether or not a point is contained within this sprite's rectangle.
mask_contains_point
def mask_contains_point(x, y=None)
Determine whether or not a point is contained within this sprite's mask.
update
def update()
Update the sprite in preparation to draw the next frame.
This method should generally not be called explicitly, but will be called by the event loop if the sprite is on the active screen.
draw
def draw(surface)
Draw the sprite on the given Surface object.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for ucc_sprite-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4eec3fb661abf349ec3049f8a5645d74f1b971912159a5f04281a742e14bb21 |
|
MD5 | e08fdbbf9deb613509e18835f26eacb7 |
|
BLAKE2b-256 | 0552f77782730602cd8bcece3b7c2df990f32dfcde6c4375d6f24bafd09e3d0a |