1

I'm trying to make a game with pygame and trying to make an object jump to the right or to the left, I use this code to move an object and also jump:

import pygame

pygame.init()


win = pygame.display.set_mode((500, 500))


playerX = 250
playerY = 250

vel_x = 10
vel_y = 10

jump = False


run = True

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_a] and playerX > 0:
        playerX -= vel_x
    elif keys[pygame.K_d] and playerX < 500:
        playerX += vel_x
    elif jump == False and keys[pygame.K_SPACE]:
        jump = True
    elif jump == True:
        playerY -= vel_y * 4
        vel_y -= 1
        if vel_y < -10:
            jump = False
            vel_y = 10
    
    
    win.fill((0, 0, 0))
    
    
    pygame.draw.circle(win, (255, 255, 255), (int(playerX), int(playerY)), 15)
    
    pygame.time.delay(30)
    
    pygame.display.update()

pygame.quit()

But the code doesn't work, I mean it works when the space key is pressed but it stops moving when another movement key is pressed, for example when I press the space key the ball jumps but when I press the d key (to move to the left) the ball stops in the air and can only move left and right, but when I release the space key the ball falls back and forth as it was at the beginning, or If you don't understand what I'm talking about you can copy the code and try the code yourself.

The movement of the ball should look like in this video (from minute 8:10):

https://www.youtube.com/watch?v=am2Tb_tj8zM

Can someone tell me what's wrong with the code?

Daffa Zaki
  • 39
  • 5

1 Answers1

2

You have to separate the movement along the x and y axis. Just turn the elif to if, in the condition that evaluates if SPACE is pressed:

if keys[pygame.K_a] and playerX > 0:
    playerX -= vel_x
elif keys[pygame.K_d] and playerX < 500:
    playerX += vel_x

# elif jump == False and keys[pygame.K_SPACE]:
if jump == False and keys[pygame.K_SPACE]:
    jump = True
elif jump == True:
    playerY -= vel_y * 4
    vel_y -= 1
    if vel_y < -10:
        jump = False
        vel_y = 10

Complete example:

import pygame

pygame.init()
win = pygame.display.set_mode((500, 500))

playerX = 250
playerY = 250
vel_x = 10
vel_y = 10
jump = False

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_a] and playerX > 0:
        playerX -= vel_x
    elif keys[pygame.K_d] and playerX < 500:
        playerX += vel_x
    
    if jump == False and keys[pygame.K_SPACE]:
        jump = True
    elif jump == True:
        playerY -= vel_y * 4
        vel_y -= 1
        if vel_y < -10:
            jump = False
            vel_y = 10
    
    win.fill((0, 0, 0))
    pygame.draw.circle(win, (255, 255, 255), (int(playerX), int(playerY)), 15)
    pygame.display.update()
    pygame.time.delay(30)

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174