-1

i'm making a game for my alevel programming project and im having some trouble. im having trouble making my sprite jump. I found some code telling you how to do it as well as a video and i followed the steps but it just doesn't seems to work? i have stared (***) the section that the code is in for jumping

def level1():
    global running, jumping1
    global x1, y1 , x2, y2, xChange1, yChange1, xChange2, yChange2, jump1, jump2, jumping1, jumping2, isJump

    while running:
        clock.tick(FPS)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    xChange1 = -5
                if event.key == pygame.K_RIGHT:
                    xChange1 = 5
**************************************************************************************
                if not jumping1:
                    if event.key == pygame.K_UP:
                        jumping1 = True
                    else:
                        if jump1 >= -10:
                            y1 -= (jump1 **2) * 0.5
                            jump1 -= 1
                        else:
                            jump1 = 10
                            jumping1 = False
********************************************************************************
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    xChange1 = 0


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    xChange2 = -5
                elif event.key == pygame.K_d:
                    xChange2 = 5
                elif event.key == pygame.K_w:
                    yChange2 = -5
                elif event.key == pygame.K_s:
                    yChange2 = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                    xChange2 = 0
                elif event.key == pygame.K_s or event.key == pygame.K_w:
                    yChange2 = 0           

        x1 += xChange1
        x2 += xChange2
        y1 += yChange1
        y2 += yChange2

        y1 -= gravity1
        y2 -= gravity2
        
        screen.blit(game_image,(0,0))
        character1(x1,y1)
        character2(x2,y2)
        gameFloor(fx,fy)

        if x1 < 0:
            x1 = 0
        if x1 > WIDTH - Char1WIDTH:
            x1 = WIDTH - Char1WIDTH

        if y1 < 0:
            y1 = 0
        if y1 > HEIGHT - Char1HEIGHT - 50:
            y1 = HEIGHT - Char1HEIGHT - 50

        if x2 < 0:
            x2 = 0
        if x2 > WIDTH - Char2WIDTH:
            x2 = WIDTH - Char2WIDTH

        if y2 < 0:
            y2 = 0
        if y2 > HEIGHT - Char2HEIGHT-50:
            y2 = HEIGHT - Char2HEIGHT -50


            
        pygame.display.flip()

jumping 1 is set to false beforehand and jump 1 is set to 5

GT02
  • 41
  • 5

1 Answers1

0

Hard to tell without more details, but shouldn't the first else have the same indentation as if not jumping1:? Like this:

if not jumping1:
    if event.key == pygame.K_UP:
        jumping1 = True
else:
    if jump1 >= -10:
        y1 -= (jump1 **2) * 0.5
        jump1 -= 1
    else:
        jump1 = 10
        jumping1 = False