Can someone please help me debug my code because I don't get why I cannot make my character do a double jump with multiple spacebars. When I ran the script, I could move up,down,left,right but once I press spacebar one time, the object flies out of the window.
The problem comes with this if statement so I'm guessing that this if statement keeps running and incrementing my jumpCount, which I can't comprehend because after pressing space one time shouldn't the keys[pygame.K_SPACE]
evaluate to true and then back to false again so this if statement shouldn't run unless I press another spacebar?
else:
if keys[pygame.K_SPACE]:
jumpCount += 5
number_to_compensate += 1
Here is my script:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")
x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 5 #To calculate the height I have to rise by
number_to_compensate = 0 #So the more times I press space(higher I jump) the more I have to fall by
run = True
while run:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - vel - width:
x += vel
if not (isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
number_to_compensate += 1
else:
if keys[pygame.K_SPACE]:
jumpCount += 5
number_to_compensate += 1
if jumpCount >= -5 *number_to_compensate:
y -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 5
isJump = False
number_to_compensate = 0
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()