Skip to main content

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 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.

topleft

@property
def topleft()

The coordinates of the top left corner of the sprite.

bottomleft

@property
def bottomleft()

The coordinates of the bottom left corner of the sprite.

topright

@property
def topright()

The coordinates of the top right corner of the sprite.

bottomright

@property
def bottomright()

The coordinates of the bottom right corner of the sprite.

midleft

@property
def midleft()

The coordinates of the middle of the left edge of the sprite.

midright

@property
def midright()

The coordinates of the middle of the right edge of the sprite.

midtop

@property
def midtop()

The coordinates of the middle of the top edge of the sprite.

midbottom

@property
def midbottom()

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.

centerx

@property
def centerx()

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.

centery

@property
def centery()

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

ucc-sprite-0.0.5.tar.gz (8.0 kB view hashes)

Uploaded Source

Built Distribution

ucc_sprite-0.0.5-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page