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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file ucc-sprite-0.0.5.tar.gz.

File metadata

  • Download URL: ucc-sprite-0.0.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for ucc-sprite-0.0.5.tar.gz
Algorithm Hash digest
SHA256 22d8168c9387b0736a607c3e79bcebbea1325b3fd913a3a20c8193795cff8701
MD5 a0c7c50525ef84af656c45004c10c48e
BLAKE2b-256 60d6ba0d47e2db032aeb09cdd2a314faddaca4d16d0d7157f2678d5d20bd1472

See more details on using hashes here.

File details

Details for the file ucc_sprite-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: ucc_sprite-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for ucc_sprite-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d13b000f6380d475006fc7bafbcfaa5acd06bbbd2c825556075b0800ecba2208
MD5 544e40f0f0938479d431b91a6fe6112d
BLAKE2b-256 8f60b75655db66ca22f67a64a6c7185eba03b90563e74a8126de80f1a4187985

See more details on using hashes here.

Supported by

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