I've basically just started developing with PyGame and I am having trouble with the whole Sprite concept. I have been looking every where for guides on how to use it, I just can't seem to find any. I would like to know the basic concept of how it all works. This is the code I have been working on:

import pygame, sys, os, time
from pygame.locals import *

windowWidth = 320
windowHeight = 640
halfWidth = int(windowWidth / 2)
halfHeight = int(windowHeight / 2)

#main blocks width and height (32x32)
blockSize = 32

red = [255,000,000]
green = [000,255,000]
blue = [000,000,255]
black = [000,000,000]
white = [255,255,255]

bgColor = black
playerColor = white
blockColor = green

fps = 60

assert windowWidth % blockSize == 0, "Too much space/not enough space for the ground"
floorAmt = windowWidth/blockSize
print floorAmt

floorY = windowHeight-blockSize
print floorY

floor = pygame.sprite.Group()

def main():

    global fpsClock, displayScreen

    inAir = False

    upVel = 0
    downVel = 0
    leftVel = 0
    rightVel = 0

    fpsClock = pygame.time.Clock()
    displayScreen = pygame.display.set_mode((windowWidth, windowHeight))
    pygame.display.set_caption("Jump Test")

    grass = pygame.image.load("grass.png")
    grass = grass.convert_alpha()
    grassRadius = blockSize

    floorDraw(blockSize, floorAmt, floorY, grass)

    while True:

        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                raise SystemExit, "Exit"

        floorDraw(blockSize, floorAmt, floorY)


def floorDraw(blockSize, floorAmt, floorY, grass):
    for i in range(floorAmt):
        floorX = i*blockSize
        grassRect = (floorX, floorY, blockSize, blockSize)
        displayScreen.blit(grass, grassRect)

if __name__ == "__main__":

basically I just want to know how to make these grass blocks into sprites in a group, so that when I add my player (also a sprite) I can determine whether or not he is in the air or not through the collision system. Can someone please explain to me how I would do this. All I am looking for is basically a kick starter because some of the documentation is quite bad in my opinion as it doesn't completely tell you how to use it. Any help is greatly appreciated :)


Although it may be a bad design choice, the Sprite object is meant to be inherited and be managed as a group.

You did right by creating the floor as Group and adding each grass to it.

But what I would suggest is to inherit the Sprite object to class you created yourself. Usually my Entities starts with the following:

class Entity(pygame.sprite.Sprite):
    def __init__(self, x, y, width=1, height=1):
        self.rect = pygame.Rect((x, y), (width, height))

It is an Sprite, but it contains the Rect object that you will want to use for a lot of reasons, like Bounding Box collisions and for moving your entity. Then, you can simple call

pygame.sprite.spritecollide(sprite, group, dokill, collided = None)

and have a list of Sprites that collide, so you can can manage them. More info on:


In fact, Pygame have lots of collision detection methods that do the same thing: check for a intersection between two Rect objects.

You can look thought this book, it may be useful: http://inventwithpython.com/ There are also some tutorials over here and there, and the mail-list.

Hope that helps!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.