Jump to content
  • Advertisement
Sign in to follow this  

[pygame] sprite - jump method

This topic is 3047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody, I made a sprite object for the player character in a side scrolling jump and run. Now I want to add a method that is called when the space button is pressed and then moves the character sprite up and down again one time. I just have no clue how I do this and would be very glad if someone could give me an impulse into the right direction. best regards, Djtschke

Share this post

Link to post
Share on other sites
We might need more information about what you are using to make this (win api c++ etc?) to help.

But in general, when you are first learning how to make platformer style jumping and movement you should work with a floor height.

Put it this way:
You have a character at location 100,100, and the floor is at y co-ordinate 100. So the character is on the floor.

You need to have some kind of "step" method in the character that ensures it cannot go below 100 height. And then include some form of gravity.

Take a look at this:

//define these now so the player class is "aware" of them
float groundY,gravity;

//define the player class
class Cplayer{
float x,y;
float vSpeed, hSpeed;
float maxVSpeed;
float jumpForce;

Cplayer(float setX,float setY){
x = setX;
y = setY;
jumpForce = 8;

maxVSpeed = 3; //don't move vertically faster than 3 pixels per frame

void step(){

vSpeed += gravity; //get "pulled" down by gravity (or pushed by space and time... if thats how it really works)
if(vSpeed > maxVSpeed) vSpeed = maxVSpeed; //don't fall too fast!
y += vSpeed; //move to new y co-ordinate

if(y >= groundY){ //if on/below the ground
vSpeed = 0; //don't keep moving down
y = groundY; //don't be inside the ground
if(key(SPACEBAR)){ //example check for spacebar key press
vSpeed = -jumpForce; //negative because +y direction is down, so we want to be moving up

void draw(); //draw the sprite at the current x/y co-ordinate

player Cplayer(100,50); //make the player

//assign global values
gravity = 0.5;
groundY = 150;
colour black = rgb(0,0,0);

int main(){

//game loop
//object step code

//drawing routines
//clear screen to background etc
drawLine(0,groundY,game.width,groundY,black); //example function

//flip buffers/etc


Now, I am not sure if you are using c++ or anything. But the principle should be the same. Also, I havn't run this code so there may be silly bugs. The animation will probably look unnatural too (would tweak gravity and other "forces" after initial testing)

Of course this code won't run because it would rely on some kind of example frame work, but take its teachings into account.

So, what are you developing this in? Do you have any code currently to share?

If you are very new to game development you should try game maker (google it; first result I think) it is extremely useful to get to grips with the concepts involved.

Share this post

Link to post
Share on other sites
hes using pygame, a library for Python.
i played around with pygame and was frustrated by its limitations.. pyglet is far more capable for what you want IMO. if you switch to that library let me know i have some code i can share with you that does exactly what you need. (But its in pyglet syntax)

Share this post

Link to post
Share on other sites

class Player():
global gravity
#class stuff
self.jumpForce = 8
self.maxVSpeed = 3
self.isJumping = False
def getInput(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if not self.isJumping:
self.isJumping = True

def update(self):
self.vSpeed += gravity
if self.vSpeed > self.maxVSpeed:
self.vSpeed = self.maxVSpeed
self.y += self.vSpeed
if self.y >= ground.y
self.vSpeed = 0
self.y = ground.y
self.isJumping = False
if self.isJumping:
self.vSpeed += -self.jumpForce

Share this post

Link to post
Share on other sites
Hello everybody,

yes, as Ironhorse already said I am working with python and pygame. Thank you for all the input !! I will try to get it working and then let you know again.

best regards,


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!