简体   繁体   中英

Why won't my Pygame sprite jump code work?

I am trying to make my Pygame sprite jump so I followed a tutorial and this is what it gave me. But it doesn't work for some reason. What's wrong with it

keys = pygame.key.get_pressed()
if isjump == False:
    if keys[pygame.K_UP]:
        if isjump:
            F =(1 / 2)*m*(v**2)
            player.rect.y -= F
            v = v-1
        if v<0:
            m =-1
        if v ==-6:
                isjump = False
                v = 5
                m = 1

You need to set somewher isjump = True . Additionally the if isjump == False: needs an else case:

keys = pygame.key.get_pressed()
if isjump == False:
    if keys[pygame.K_UP]:
        isjump = true    
        v = 5
else:
    m = 1 if v >= 0 else -1
    F = m * (v**2)
    player.rect.y -= F
    
    v -= 1
    if v == -6:
        isjump = False

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