【问题标题】:Pygame, do command exactly 1 time when pressed a key then wait for another clickPygame,按下一个键时恰好执行 1 次命令,然后等待另一次点击
【发布时间】:2017-11-16 03:57:02
【问题描述】:
if key("LEFT"):
    if select_x != 100:
        select_x-=64
    else:
        print("BORDER REACHED")

如果你运行程序并按下 LEFT 键,pygame 将永远执行下面写的命令,直到你停止按下该键,我希望 pygame 只执行命令 1 次,然后等待再次单击。

【问题讨论】:

    标签: python button pygame key


    【解决方案1】:

    在事件循环中做事件处理for event in pygame.event.get()::

    import pygame as pg
    
    
    pg.init()
    screen = pg.display.set_mode((320, 240))
    clock = pg.time.Clock()
    select_x = 164
    done = False
    
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_LEFT:
                    if select_x != 100:
                        select_x -= 64
                    else:
                        print("BORDER REACHED")
    
        screen.fill((30, 30, 30))
        pg.display.flip()
        clock.tick(30)
    
    pg.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      相关资源
      最近更新 更多