0

I am trying to implement jumping into a 2-D fighting game by using velocity. The player can jump when "w" is pressed however each time when the object lands, it lands a bit higher each time. I want the object to land back in the same position as where it initially jumped from. Any ideas?

velocity = list([-10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5 , 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10])


class Player():

   def __init__(self):
       velocity_index = 0

   def jump(self):
       self.image = self.jump_list[self.pos_jump]
       self.rect.y += velocity[self.velocity_index]
       if self.velocity_index ==0:
          self.pos_jump += 1
       elif self.velocity_index==40:
          self.pos_jump += 1
          self.image = self.jump_list[self.pos_jump]
       if self.pos_jump == self.ani_max_jump:
          self.pos_jump = 0
       if self.velocity_index!=40:
          self.velocity_index += 1
       elif self.velocity_index==40:
          self.velocity_index =0
          self.jumping = False 
e_robinson
  • 31
  • 7
  • Indentation is off in the provided code, could you please fix it? – Jonas Adler Feb 15 '18 at 16:25
  • Rather than using a hard-coded list of possible velocity values, it's probably a better idea to use real-world physics calculations, e.g. `GRAVITY = -9.8; self.pos_jump = (self.initial_velocity * self.time_since_jump) + (GRAVITY * self.time_since_jump * self.time_since_jump / 2.0)`, or maybe something like `self.acceleration = GRAVITY; self.velocity += self.acceleration; self.position += self.velocity`. (I haven't tested these, so they may be wrong.) This will allow you to be much more dynamic with the values used and should help solve logic errors like the one you are facing. – 0x5453 Feb 15 '18 at 16:30
  • okay thanks for the input. @0x5453 – e_robinson Feb 15 '18 at 16:58

0 Answers0