0

Hello i have a problem with jumping. I've defined 3 functions: main loop with game process, drawwindow which displays the window and square_movement function which is responsible for handling the movement of a rect square. I want the square to jump but whenever i click space, nothing happens and i can't find a solution to fix it. Could someone help me?

import pygame
import os
import random
pygame.init()

#----------------------------------------------------------
#VARIABLES

 #SCREEN
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60

 #COLORS
BLACK = (0, 0,0)
RED = (255, 0, 0)

#OBJECT
square_y = 340
vel_y = 10
square = pygame.Rect((10, square_y), (150, 150))

 #JUMPING
jump = False
#----------------------------------------------------------

#DISPLAY
def draw_window(square):
    #SCREEN
    WIN.fill(BLACK)
    #OBJECTS
    pygame.draw.rect(WIN, RED, square)

    pygame.display.update()

#MOVEMENT
def square_movement(keys_pressed, jump, square, square_y, vel_y):
    if jump is False and keys_pressed[pygame.K_SPACE]:
        jump = True

    if jump is True:
        square_y -= vel_y
        square_y -= 1
        if vel_y < -10:
            jump = False
            vel_y = 10

#MAIN LOOP
def main():
    #VARIABLES

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys_pressed = pygame.key.get_pressed()

        #MOVEMENT
        square_movement(keys_pressed, jump, square, square_y, vel_y)

        draw_window(square)

    pygame.quit()

if __name__ == "__main__":
    main()
deja
  • 3
  • 2

0 Answers0