I am trying to make my character jump. The jumping animation is great, but there is one problem. The rectangle jumps without me pressing the keybind. Also, please make it so the character stops at Y = 260 because that is where I am going to put the ground. (dont come here just to edit my question, actually answer it!) Please help!
import pygame
pygame.init()
win = pygame.display.set_mode((500,300))
pygame.display.set_caption("Run")
y = 100
width = 32
height = 32
jumping = False
jumpVel = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
jumping = True
else:
if jumpVel >= -10:
y -= (jumpVel * abs(jumpVel)) * 0.5
jumpVel -= 1
else:
jumpVel = 10
jumping = False
win.fill((255,255,255))
pygame.draw.rect(win, (0,0,0), (128, y, width, height))
pygame.display.update()
pygame.quit()