【问题标题】:My sprite is dribbling instead of jumping [duplicate]我的精灵在运球而不是跳跃[重复]
【发布时间】:2021-08-05 04:33:27
【问题描述】:
fps = 90
fpsclock = pygame.time.Clock()  
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
            a = False
        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            #pygame.key.set_repeat(10,50)
            is_jump = True
            if True and icony <=340:
                icony -= jump_count**2*0.4*direction
                jump_count -= 1
                if jump_count == 0 :
                    direction = direction*-1
                if jump_count == -10:
                    is_jump = False 
                    jump_count = 10
                    direction = 1
                    icony = 340
            pygame.time.delay(10)
    fpsclock.tick(fps)
    pygame.display.update()

我希望它跳起来,然后停一秒钟左右,然后再跳,而不是连续跳。请帮忙

【问题讨论】:

    标签: python pygame game-physics


    【解决方案1】:

    这是Indentation 的问题。执行跳转的代码必须在应用循环而不是事件循环中执行。
    除此之外,您的代码中还有一些逻辑问题。见How can I do a double jump in pygame?python, pygame - jumping too fast?

    fps = 90
    fpsclock = pygame.time.Clock()  
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == QUIT:
                run = False
        
        #INDENTATION
        #<--|
        keys = pygame.key.get_pressed()
        if not is_jump and keys[pygame.K_SPACE]:
            is_jump = True
        if is_jump:
            icony -= jump_count**2*0.4*direction
            jump_count -= 1
            if jump_count == 0:
                direction = direction*-1
            if jump_count == -10:
                is_jump = False 
                jump_count = 10
                direction = 1
                icony = 340
    
        # [...]
    
        pygame.display.update()
        fpsclock.tick(fps)
    

    小例子:

    import pygame, sys
    from pygame.locals import * 
    
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    
    is_jump = False 
    jump_count = 10
    direction = 1
    icony = 340
    
    fps = 90
    fpsclock = pygame.time.Clock()  
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == QUIT:
                run = False
        
        keys = pygame.key.get_pressed()
        if not is_jump and keys[pygame.K_SPACE]:
            is_jump = True
        if is_jump:
            icony -= jump_count**2*0.4*direction
            jump_count -= 1
            if jump_count == 0:
                direction = direction*-1
            if jump_count == -10:
                is_jump = False 
                jump_count = 10
                direction = 1
                icony = 340
    
        screen.fill(0)
        pygame.draw.circle(screen, (255, 0, 0), (250, icony), 10)
        pygame.display.update()
        fpsclock.tick(fps)
    
    pygame.quit()
    sys.exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多