0
votes

So I'm playing around with a Pygame platformer. Right now I have a player character, a bumper, and background. The player can run around the floor of the level or the bumper and the screen can screen when the character runs against the edge.

What I'd like though is a way to have multiple bumpers without having to create each individually. Here is some relevant code.

This is creating the bumper before the game's main loop...

bumper = pygame.image.load("bumper.png") #load bumper pic
bumperbounds = bumper.get_clip() #create boundaries for bumper
bumperbounds = bumperbounds.move (200, 250) #move the bumper to starting location

This is the code that makes the bumper stop the player from falling through. It just sets the landed value to 1...

    if playerbounds.left <= bumperbounds.right and playerbounds.right >= bumperbounds.left and playerbounds.bottom <= bumperbounds.top + 30 and playerbounds.bottom >= bumperbounds.top and gravity >=0: #if player lands on top of the bumper
        landed = 1 #set player to landed

This is some code so that the player lands on the platform correctly. Basically it slows the player down at the last loop iteration from falling through the bumper so its exact falling speed lands the bottom of the player on the top of the bumper.

    if playerbounds.left <= bumperbounds.right and playerbounds.right >= bumperbounds.left and playerbounds.bottom <= bumperbounds.top and gravity > bumperbounds.top-playerbounds.bottom: #to land exactly on the top of the bumper
        gravity = bumperbounds.top-playerbounds.bottom

This is to move the bumper when the screen is scrolling. This is just the right direction, the left one works basically the same.

    if xacc > 0:#if player is moving right
        if playerx >= width * .8 and screenx < levelwidth - playerbounds.width:#if the screen is scrolling
            screenx = screenx + xacc
            bumperbounds = bumperbounds.move(-xacc, 0) #bumper moves on the screen
            skybounds = skybounds.move(-xacc / 10, 0)
            groundbounds = groundbounds.move(-xacc * 2, 0)
        else:
            playerbounds = playerbounds.move(xacc, 0)
            playerx += xacc
        xacc -= 1

This just shows the bump on the screen at the end of the loop.

    screen.blit(bumper,bumperbounds) #displays the bumper on the screen

Any help can be useful?!

1
You should look into making a bumper class, see the classes tutorialhttps://docs.python.org/2/tutorial/classes.html - shockburner
Alternatively, you can just have a "factory function" that creates bumpers. But a class is a great idea for organizing things. - abarnert
Also, whichever way you go, you're going to need to store a list of bumper objects, or a list of bumperbounds and a list of bumpers, instead of just a single one. - abarnert

1 Answers

4
votes

You need create a class for the object you are creating.

For example:

class Leaf:
    def __init__(self):
        self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
        self.leafrect = self.leafimage.get_rect()
        xpos = random.randint(0, 640)
        self.leafrect.midtop = (xpos, 0)
    def move(self):
        self.leafrect = self.leafrect.move([0, 1])

To create objects at the same time,

leaves = []
for i in range(5):
    leaves.append(Leaf())

With changing xpos value:

class Leaf:
    def __init__(self,xpos):
        self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
        self.leafrect = self.leafimage.get_rect()
        self.leafrect.midtop = (xpos, 0)
    def move(self):
        self.leafrect = self.leafrect.move([0, 1])
    To create objects at the same time,

leaves = []
xpos= [20 30 40 50 60]
for i in range(5):
    leaves.append(Leaf(xpos[i]))