I've recently started writing a 2d platformer in Python, using pyGame. It is based heavily on this tutorial, which means it is built by the MVC design. Now I managed to load spritesheets and make my character move around, but I have no idea of how to make it jump. I've been trying all day with no result. I coded it the way I did because I want it to be easily turned into an online game. I would really appreciate some pointers. Below, there is the code for movement, just so you can see how it works.
This is dummy code, but this should get you a good idea of jumping ... remember to apply gravity affects also
If (key_pressed == up and sprite_not_in_air() ):
sprite_Y = getY() + 10
if(key_pressed == right ):
sprite_X = getX() + 5
elseif(key_pressed == left ):
sprite_X = getX() - 5
Edit: changed dummy code for clarity
I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.
I would suggest something different from what Shippou suggests. You should test the keys independently, not test if key Up and Key Right. Also, you should be applying a velocity for movement, just just moving the sprite based on keys (which it seems you are doing from looking at the python code). I must admit, it's not the easiest to follow, but, of course, I don't know Python.
In another post, i gave some sample code (for C++) which shows, in general how you handle jumping. You should be able to get the gist of it form this code. The main points are:
#1, you can jump if you're on the ground and hit the jump key
#2, you apply an initial Negative Y velocity(which is up on the screen) once when jumping, and then subtract from the velocity every frame (to simulate gravity).
#3, if you hit anything in the Y direction (your head or your feet), reset the location, and set Y velocity to 0.
Good Luck!
(#define values are just guessed examples, you'll have to change them to fit your game)
// Speed player moves left or right
#define MOVEMENT_SPEED 10.0f
// initial velocity given to player when he jumps
#define JUMP_VELOCITY 20.0f
void Player::HandleInput()
{
if (LeftIsPressed()) {
this.xVelocity = -MOVEMENT_SPEED;
}
else if (RightIsPressed()) {
this.xVelocity = MOVEMENT_SPEED;
else {
this.xVelocity = 0.0f;
}
// Only jump if we're not already jumping or falling
if (JumpIsPressed() && this.OnGround) {
this.yVelocity = -JUMP_VELOCITY;
}
}
// defines amount to increase downward velocity every frame
#define GRAVITY_FORCE 4.0f
void Player::Update()
{
// Apply downward force to player
this.yVelocity += GRAVITY_FORCE;
// Move the Player
this.xLocation += this.xVelocity;
this.yLocation += this.yVelocity;
// Check we've collide with something above or below us
bool CollideBelow;
if (CheckCollisionY(CollideBelow)) {
// move us back to previous location and Stop Y Velocity
this.yLocation -= this.yVelocity;
this.yVelocity = 0.0f;
if (CollideBelow) {
this.OnGround = true;
}
}
else {
this.OnGround = false;
}
// Check if we've collided with anything on our left or right
if (CheckCollisionX()) {
// move us back to previous location and Stop X Velocity
this.xLocation -= this.xVelocity;
this.xVelocity = 0.0f;
}
}
I would suggest something different from what Shippou suggests. You should test the keys independently, not test if key Up and Key Right. Also, you should be applying a velocity for movement, just just moving the sprite based on keys (which it seems you are doing from looking at the python code). I must admit, it's not the easiest to follow, but, of course, I don't know Python.
In another post, i gave some sample code (for C++) which shows, in general how you handle jumping. You should be able to get the gist of it form this code. The main points are:
#1, you can jump if you're on the ground and hit the jump key
#2, you apply an initial Negative Y velocity(which is up on the screen) once when jumping, and then subtract from the velocity every frame (to simulate gravity).
#3, if you hit anything in the Y direction (your head or your feet), reset the location, and set Y velocity to 0.
Good Luck!
(#define values are just guessed examples, you'll have to change them to fit your game)
// Speed player moves left or right
#define MOVEMENT_SPEED 10.0f
// initial velocity given to player when he jumps
#define JUMP_VELOCITY 20.0f
void Player::HandleInput()
{
if (LeftIsPressed()) {
this.xVelocity = -MOVEMENT_SPEED;
}
else if (RightIsPressed()) {
this.xVelocity = MOVEMENT_SPEED;
else {
this.xVelocity = 0.0f;
}
// Only jump if we're not already jumping or falling
if (JumpIsPressed() && this.OnGround) {
this.yVelocity = -JUMP_VELOCITY;
}
}
// defines amount to increase downward velocity every frame
#define GRAVITY_FORCE 4.0f
void Player::Update()
{
// Apply downward force to player
this.yVelocity += GRAVITY_FORCE;
// Move the Player
this.xLocation += this.xVelocity;
this.yLocation += this.yVelocity;
// Check we've collide with something above or below us
bool CollideBelow;
if (CheckCollisionY(CollideBelow)) {
// move us back to previous location and Stop Y Velocity
this.yLocation -= this.yVelocity;
this.yVelocity = 0.0f;
if (CollideBelow) {
this.OnGround = true;
}
}
else {
this.OnGround = false;
}
// Check if we've collided with anything on our left or right
if (CheckCollisionX()) {
// move us back to previous location and Stop X Velocity
this.xLocation -= this.xVelocity;
this.xVelocity = 0.0f;
}
}
Although it was hardly what I was asking for, your post made me feel hopeless so I started rewriting some code that disable me from getting the jumping done. ~1.5 hours later, here I am, having a perfectly functioning jumping and moving system, so thank you!