I have been trying to simulate the jump in the Pygame code but haven't been able to successfully implement it. There is a rectangle of dimension 10 by 10 and I want that rectangle to jump when SPACE is press. I am keeping this code independent of gravity for now.
import pygame
pygame.init()
ScreenLenX = 1000
ScreenLenY = 500
win = pygame.display.set_mode((ScreenLenX, ScreenLenY))
pygame.display.set_caption("aman")
run = True
Xcord = 100
Ycord = 100
length = 10
height = 10
vel = 2
xmove = 1
ymove = 1
while run:
#pygame.time.delay(1)
for event in pygame.event.get():
print(event)
if event.type ==pygame.QUIT:
run = False
if keys[pygame.K_RIGHT] and Xcord <= ScreenLenX-length:
Xcord += vel
if keys[pygame.K_LEFT] and Xcord >= 0:
Xcord -= vel
if keys[pygame.K_UP] and Ycord >= 0:
Ycord -= vel
if keys[pygame.K_DOWN] and Ycord <= ScreenLenY - height:
Ycord += vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (Xcord, Ycord, length, height))
keys = pygame.key.get_pressed()
pygame.display.update()
pygame.quit()