Reputation:
I can't figure out why my player is not jumping. I have defined jump
and update
functions as follows:
def jump(self):
if self.is_jumping == False:
self.is_jumping = True
def update(self):
if self.is_jumping:
self.y -= self.force
Here's the initialization
class Myman:
def __init__(self, x, y, side):
self.x = x
self.y = y
self.side = side
self.speed = 2
self.is_jumping = False
I have implemented this in the main loop as follows:
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
mm.jump()
d.fill((98, 98, 98))
mm.draw()
mm.g()
mm.update()
if mm.y + mm.side > 550:
mm.y = 550 - mm.side
mm.falling = False
However, when I try to jump, it doesn't work. All the examples I have seen on jumping so far have done the same thing, but mine isn't working and I can't figure out why. Thanks for helping.
Here's the code for anyone who would like to check for reference.
import pygame, os
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
win = pygame.display
d = win.set_mode((1200, 600))
win.set_caption("Jump")
gravity = 0.1
class Myman:
def __init__(self, x, y, side):
self.x = x
self.y = y
self.side = side
self.speed = 2
self.is_jumping = False
self.force = 2
def draw(self):
pygame.draw.rect(d, (0 ,255, 0), (self.x, self.y, self.side, self.side))
def g(self):
global gravity
self.y += gravity
gravity += 0.01
def move_left(self):
self.x -= self.speed
def move_right(self):
self.x += self.speed
def jump(self):
if self.is_jumping == False:
self.is_jumping = True
def update(self):
if self.is_jumping:
self.y -= self.force
mm = Myman(600, 400, 20)
def platform():
pygame.draw.rect(d, (0 ,0, 0), (0, 550, 1200, 50))
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
mm.jump()
d.fill((98, 98, 98))
mm.draw()
mm.g()
mm.update()
platform()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
mm.move_right()
if keys[pygame.K_LEFT]:
mm.move_left()
if mm.y + mm.side >= 550:
mm.y = 550 - mm.side
mm.falling = False
win.flip()
Upvotes: 1
Views: 137
Reputation: 14906
The problem is that your gravity is ever-increasing:
def g(self):
global gravity
self.y += gravity
gravity += 0.01 # <-- HERE
After just one second, gravity is 60
pixels per frame. This immediately more-than reverses any minor velocity change introduced by jumping.
So unless you want to do a real-time "physics" gravity model (which would be good), maybe just change the player's gravity for the millisecond duration of the jump. Give it negative gravity for the first half of the jump, then at the highest-point of the jump, the gravity reverts to normal. This is easily done based on a jump time.
Something like:
class Myman:
JUMP_TIME = 1500 # milliseconds to jump apogee
def __init__(self, x, y, side):
self.x = x
self.y = y
self.jump_at = 0 # time of jump start
# ... rest of code omitted
def isJumping( self ):
jumping = False
time_now = pygame.time.get_ticks()
if ( time_now - self.jump_at < 2 * Myman.JUMP_TIME ): # up and down time
jumping = True
return jumping
def jump( self ):
if ( not self.isJumping() ): # is the player ready to jump again
self.jump_at = pygame.time.get_ticks() # time now
def update( self ):
if ( self.isJumping() ):
time_now = pygame.time.get_ticks()
# is the player going up, or coming down?
if ( time_now - self.jump_at < Myman.JUMP_TIME ):
self.gravity = -0.1 # still jumping up
else:
self.gravity = 0.1 # now going down
# ... rest of code omitted
Style Note: I purposefully put brackets around all if
expressions because I truly believe it makes the code more readable. This is in opposition to recommendations in PEP8.
Upvotes: 1
Reputation: 359
Hopefully this resolves your issues but your code is incorrectly indented.
from d.fill((98, 98, 98))
till win.flip()
your code might need another 4 spaces / 1 tab to match the for loop
Upvotes: 0