I would really like the purple_idle_rect to be able to jump in the air, then get pulled down by gravity. What really happens here is that once I jump, I am stuck in the air then once I press the jump button again I fall. Basically, I need to press the up arrow key twice to fully jump and land on the ground.
import pygame
# import n stuff
import pygame
pygame.init()
# Screen and setup
Screen = pygame.display.set_mode((900, 500))
# Velocity
Velocity = 8
# Purple who sleep
purple_sleeping = pygame.transform.scale(pygame.image.load("purple_guy_sleeping.png"), (100, 100))
purple_sleeping_rect = purple_sleeping.get_rect(center=(450, 302))
# Purple who stand
purple_idle = pygame.transform.scale(pygame.image.load("purple_man_idle.png"), (100, 100))
purple_idle_rect = purple_idle.get_rect(midbottom = (100,360))
# The ground
ground = pygame.transform.scale(pygame.image.load("a2889b85d5c7122.png"), (800, 100))
# Main loop
Run = True
FPS = 60
Clock = pygame.time.Clock()
# All of our keys
pressed_keys = {"left": False, "right": False}
# Gravity
player_gravity = 0
isJump = False
while Run:
pygame.display.update()
Screen.fill("black")
Screen.blit(ground, (45, 350))
Screen.blit(purple_sleeping, purple_sleeping_rect)
Screen.blit(purple_idle, purple_idle_rect)
Clock.tick(FPS)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP and purple_idle_rect.bottom == 360:
player_gravity -=20
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if purple_idle_rect.bottom == 360:
isJump = False
if not isJump:
purple_idle_rect.bottom = 360
if purple_idle_rect.bottom < 360:
isJump = True
if isJump:
player_gravity += 20
if pressed_keys["left"]: # == True is implied here
purple_idle_rect.x -= 10
if pressed_keys["right"]:
purple_idle_rect.x += 10
purple_idle_rect.bottom += player_gravity
if purple_idle_rect.bottom > 360:
purple_idle_rect.bottom = 360
pygame.display.update()
I changed your jump a bit, this is how it works for me:
player_gravity = 20
isJump = False
while Run:
# pygame.display.update()
Screen.fill("black")
Screen.blit(ground, (45, 350))
Screen.blit(purple_sleeping, purple_sleeping_rect)
Screen.blit(purple_idle, purple_idle_rect)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP and purple_idle_rect.bottom == 360:
isJump = True
player_gravity = -player_gravity
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if pressed_keys["left"]: # == True is implied here
purple_idle_rect.x -= 10
if pressed_keys["right"]:
purple_idle_rect.x += 10
# Set gravity back
if isJump:
player_gravity += 1
if player_gravity >= 20:
isJump = False
purple_idle_rect.bottom += player_gravity
if purple_idle_rect.bottom > 360:
purple_idle_rect.bottom = 360
pygame.display.update()
Clock.tick(FPS)
Perhaps you understand everything well, if you do not ask in the comments. I hope I helped you, Adam