[pygame] sprite - jump method

Started by
3 comments, last by djtschke 14 years, 1 month ago
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
Advertisement
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 themfloat groundY,gravity;//define the player classclass 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 valuesgravity = 0.5;groundY = 150;colour black = rgb(0,0,0);int main(){  //game loop  while(true){    //object step code        player.step();    //drawing routines    //clear screen to background etc    player.draw();    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.
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)
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.getInput()        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
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,

djtschke

This topic is closed to new replies.

Advertisement