Movement Moving left and right is similar to the previous tutorial and simply mean changing the (x,y) position of the player. For jumping, we use a formula from classical mechanics:

F = 1/2 * m * v^2

Where F is the force up/down, m is the mass of your object and v is the velocity. The velocity goes down over time because when the player jumps the velocity will not increase more in this simulation. If the player reaches the ground, the jump ends. In Python, we set a variable isjump to indicate if the player is jumping or not. If the player is, its position will be updated according to the above formula.

Full Code:

from pygame.localsimport * import pygame import math from time import sleep classPlayer: x = 10 y = 500 speed = 10

# Stores if player is jumping or not. isjump = 0 # Force (v) up and mass m. v = 8 m = 2 defmoveRight(self): self.x = self.x + self.speed defmoveLeft(self): self.x = self.x - self.speed defjump(self): self.isjump = 1

defupdate(self): if self.isjump: # Calculate force (F). F = 0.5 * mass * velocity^2. if self.v > 0: F = ( 0.5 * self.m * (self.v*self.v) ) else: F = -( 0.5 * self.m * (self.v*self.v) ) # Change position self.y = self.y - F

# Change velocity self.v = self.v - 1

# If ground is reached, reset variables. if self.y >= 500: self.y = 500 self.isjump = 0 self.v = 8

Thanks, this is a rock-solid foundation to start with :)

John•Sun, 09 Aug 2015

Very helpful tutorial for understanding pygame programming.

Your physics is a little off. Force, F = ma. The expression 1/2 * m * v^2 is kinetic energy not force. But what you really want to do is calculate the change in the vertical y-coordinate for constant gravity acceleration while jumping. The change in y-coordinate is given by dy = v * dt, where dt is the constant time step. The vertical velocity, v, does decrease linearly due to constant acceleration (because v = at) as your example program does. But the F term should be proportional to v not v^2. If you change (self.v*self.v) to (self.v) you don't need the if/else because the expression proportional to self.v changes sign with self.v. Mass doesn't play into vertical velocity (ignoring air drag) as all objects are accelerated by gravity at the same rate.

To keep changes to a minimum, I replaced the "if self.v>0" expression for F with one line: F = ( self.m * self.v) Then I changed self.m = 8 to make jump height about same as original.

Changing the code that handles the jump physics results in a nice parabolic jump as in real world physics. The velocity squared physics rises quickly initially and hovers at the top of the arc longer. That may be desirable for some games, but it isn't how jumping works in the real world.

Staff•Mon, 10 Aug 2015

Thanks John! I will update the code when I finished the to-do list

Leave a Reply:Thanks, this is a rock-solid foundation to start with :)

Very helpful tutorial for understanding pygame programming.

Your physics is a little off. Force, F = ma. The expression 1/2 * m * v^2 is kinetic energy not force. But what you really want to do is calculate the change in the vertical y-coordinate for constant gravity acceleration while jumping. The change in y-coordinate is given by dy = v * dt, where dt is the constant time step. The vertical velocity, v, does decrease linearly due to constant acceleration (because v = at) as your example program does. But the F term should be proportional to v not v^2. If you change (self.v*self.v) to (self.v) you don't need the if/else because the expression proportional to self.v changes sign with self.v. Mass doesn't play into vertical velocity (ignoring air drag) as all objects are accelerated by gravity at the same rate.

To keep changes to a minimum, I replaced the "if self.v>0" expression for F with one line:

F = ( self.m * self.v)

Then I changed self.m = 8 to make jump height about same as original.

Changing the code that handles the jump physics results in a nice parabolic jump as in real world physics. The velocity squared physics rises quickly initially and hovers at the top of the arc longer. That may be desirable for some games, but it isn't how jumping works in the real world.

Thanks John! I will update the code when I finished the to-do list