简体   繁体   中英

Python PyGame jump code running when it shouldnt

I couldn't find anything about this but for some reason my jump code keeps running when I hold the button down, so it basically makes you fly even when it shouldn't, please help.

    while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                pygame.quit()
                quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
               x_change = -5
            elif event.key ==  pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and jumped == 0:
               y_change = -10
               jumped = 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y_change = 0
    if x < 32:
        x += 5
    if x > (display_width-32) - char_width:
        x += -5
    if y < 32:
        y += 5
    if y > display_height - char_height:
        y = display_height - char_height
    if y < (display_height-32) - char_height:
        y += 5
    if y > (display_height-32) - char_height:
        jumped = 0
    x2 = x
    y2 = y + char_height
    x3 = x2 + (26)
    if gameDisplay.get_at((x2,y2)) == (0,0,255) or gameDisplay.get_at((x,y)) == (0,0,255) or gameDisplay.get_at((x3,y2)) == (0,0,255):
        y+= -5
        jumped = 0

(the variable "jumped" gets reset when you collide with the floor btw)

We don't really have enough code to judge, so I'm just guessing right now. The code you show here modifies the y-position of your character, to make it "jump". If the character continuously stays at that elevation, perhaps you should modify the y-position somewhere else to bring the character back down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM