Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

#rgb
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:
        fpsClock.tick(fps)

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

        displayScreen.fill(black)
        floorDraw(blockSize, floorAmt, floorY)

        pygame.display.flip()
        pygame.display.update()
        fpsClock.tick(fps)

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

        v
if __name__ == "__main__":
    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 :)

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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):
        pygame.sprite.Sprite.__init__(self)
        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:

http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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