I have had this issue for a while now and I have looked for the solution and have tried many things but nothing I do seems to work. What is happening is when I press my jump key the sprite jumps up very high the first time and then every time after that it jumps properly but goes below the ground. I will try to attach a video somehow to show what I mean.
Here is the function that has the jumping within it:
def level1():
global running, jumping1, neg1
global x1, y1 , x2, y2, xChange1, yChange1, xChange2, yChange2, jump1, jump2, jumping1, jumping2, isJump
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xChange1 = -5
if event.key == pygame.K_RIGHT:
xChange1 = 5
if event.key == pygame.K_UP:
if not jumping1:
jumping1 = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
xChange1 = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
xChange2 = -5
elif event.key == pygame.K_d:
xChange2 = 5
elif event.key == pygame.K_w:
yChange2 = -5
elif event.key == pygame.K_s:
yChange2 = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
xChange2 = 0
elif event.key == pygame.K_s or event.key == pygame.K_w:
yChange2 = 0
if jumping1:
if jump1 >= -25:
neg1 = 1
if jump1 <0:
neg1 = -1
y1 -= (jump1 **2) * 0.5 * neg1
jump1 -= 1
else:
jump1 = 10
jumping1 = False
x1 += xChange1
x2 += xChange2
y1 += yChange1
y2 += yChange2
gravity1(y1)
screen.blit(game_image,(0,0))
character1(x1,y1)
character2(x2,y2)
gameFloor(fx,fy)
if x1 < 0:
x1 = 0
if x1 > WIDTH - Char1WIDTH:
x1 = WIDTH - Char1WIDTH
if y1 < 0:
y1 = 0
if y1 > HEIGHT - Char1HEIGHT - 50:
y1 = HEIGHT - Char1HEIGHT - 50
if x2 < 0:
x2 = 0
if x2 > WIDTH - Char2WIDTH:
x2 = WIDTH - Char2WIDTH
if y2 < 0:
y2 = 0
if y2 > HEIGHT - Char2HEIGHT-50:
y2 = HEIGHT - Char2HEIGHT -50
pygame.display.flip()
Here is a link including my whole code: https://trinket.io/python/8d14105b3d
Here is a link to a video showing my program: https://kapwi.ng/w/50KogQ7A