[Tutor] Making a character jump with pygame

Lee Harr missive at hotmail.com
Fri Oct 7 13:57:01 CEST 2005

>      I've edited the aliens.py example to make my character just move
>back and forth. However I can't make him jump!

It is not really clear to me from your code where you expect the
character to jump. I do not see the word "jump" anywhere. I do
see the word "bounce" and one reference to the top of the image
so I will figure that is where you are working.

You have a .move method for your Player sprite:

    def move(self, direction):
        if direction: self.facing = direction
        self.rect.move_ip(direction*self.speed, 0)
        self.rect = self.rect.clamp(SCREENRECT)
        if direction < 0:
            self.image = self.images[0]
        elif direction > 0:
            self.image = self.images[1]
        self.rect.top = self.origtop - (self.rect.left/self.bounce%2)

origtop is set to the original value of top when the sprite is
created, and bounce = 1 and never changes.

What are you hoping this code will do?

Just as an experiment (to test that changes to self.rect.top
will actually make the sprite move up) I changed

self.rect.top = self.origtop - (self.rect.left/self.bounce%2)
self.rect.top -= 1

You might want to look at that.

Help us to help you. Name something "jump" or put in some
comments about what you are expecting, and what is not

>Here's my code (attached). I'm trying to make a platformer Mario style 

The other thing I notice is that in the controls section...

        #handle player input
        direction = keystate[K_RIGHT] - keystate[K_LEFT]

You handle K_LEFT and K_RIGHT but there is nothing to handle any
"jump" event. How does the user cause the Player to jump?

Express yourself instantly with MSN Messenger! Download today it's FREE! 

More information about the Tutor mailing list