【问题标题】:pygame continuous and simultaneous key inputspygame 连续和同时键输入
【发布时间】:2016-08-21 18:21:44
【问题描述】:

我再次陷入困境,无法在线找到任何有效的解决方案。我正在尝试使用 pygame 及其关键输入来控制各种事物。现在我需要同时使用几个键。我的代码如下:

pygame.key.set_reapeat(50,50)
bProgramLoop = True
while (bProgramLoop == True):

    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            bProgramLoop = False
        if (pygame.key.get_pressed()[pygame.K_LEFT]):
            EXECUTE_FUNCTION1()
            print "left"
        if (pygame.key.get_pressed()[pygame.K_RIGHT]):
            EXECUTE_FUNCTION2()
            print "right"

现在我遇到的问题是: 当我按住“左向右”时,它正确并连续记录我按下左/右。但是当我按住“LEFT”并点击“RIGHT”时,它会记录左右被按下,但它会停止记录“LEFT”仍然被按下。

有什么想法吗? 任何帮助将不胜感激。 米莎

【问题讨论】:

    标签: python python-2.7 keyboard pygame


    【解决方案1】:

    在我的代码中,“重复”的拼写正确。

    我找到了解决我的问题的方法。以上代码需要修改。

    pygame.key.set_repeat(50,50)
    bProgramLoop = True
    while (bProgramLoop == True):
    
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                bProgramLoop = False
            if (event.type == pyame.KEYDOWN):
                if (event.key == pygame.K_a)   # if A is pressed
                    bKeyA = True               # set the Boolean True
                if (event.key == pygame.K_s)   
                    bKeyS = True
            if (event.type == pyame.KEYDOWN):
                if (event.key == pygame.K_a)   # if A is released
                    bKeyA = False# set the Boolean False
                if (event.key == pygame.K_s)   
                    bKeyS = False
    
        if (bKeyA == True):
            Execute_function1()
        if (bKeyB == True):
            Execute_function2()
    

    我仔细检查了一遍,重复的拼写正确,一旦点击另一个键盘输入,它就不会继续键盘输入。问题是,据我所知,甚至在按下键时发生一次。当同时按下另一个键时,该事件将丢失。

    因此解决方案是将变量设置为 true 直到键被抬起,因此将变量设置为 false。

    【讨论】:

      【解决方案2】:

      你在 pygame.key.repeat() 中拼错了 repeat。我纠正了这一点,它对我有用。

      def main():
          while Running:
              check_events()
              update()
              clock.tick(FPS) 
      
      def check_events():
          events = pygame.event.get()
          for event in events:
              if event.type == pygame.QUIT:
                  sys.exit()
      
                  if key == pygame.K_q:
                      Running = False
                      return
      
              if (pygame.key.get_pressed()[pygame.K_LEFT]):
                  #EXECUTE_FUNCTION1()
                  print "left"
              if (pygame.key.get_pressed()[pygame.K_RIGHT]):
                  #EXECUTE_FUNCTION2()
                  print "right"
      

      【讨论】:

      • 谢谢,这也有效。非常感谢您的快速回复!
      【解决方案3】:

      如果你想使用连续输入而不是试试这个,这是我的代码。

      import pygame as py
      import time
      sc = py.display.set_mode((800, 600))
      x = 350
      y = 300
      blue = (0, 0, 255)
      last = 0
      while True:
        key = py.key.get_pressed()
        for event in py.event.get():
          if event.type == py.KEYDOWN:
            last = event.key
          else:
            last = 0
        if last == py.K_UP:
          y -= 0.1
        if last == py.K_DOWN:
          y += 0.1
        if last == py.K_LEFT:
          x -= 0.1
        if last == py.K_RIGHT:
          x += 0.1
        sc.fill((0,255,0))
        py.draw.rect(sc, blue, (x,y,50,50))
        py.display.flip()
      

      如果你想使用同时输入,那么这里:

      import pygame as py
      import time
      sc = py.display.set_mode((800, 600))
      x = 350
      y = 300
      blue = (0, 0, 255)
      last = 0
      def move(times, yspeed, xspeed):
        for i in range(times):
          global x, y
          x += (xspeed / times)
          y += (yspeed / times)
          time.sleep((xspeed / times / 10) + (yspeed / times / 10))
      while True:
        key = py.key.get_pressed()
        for event in py.event.get():
          if event.type == py.KEYDOWN:
            last = event.key
          else:
            last = 0
          if event.key == py.K_UP and event.key == py.K_l:
            y -= 0.1
        sc.fill((0,255,0))
        py.draw.rect(sc, blue, (x,y,50,50))
        py.display.flip()
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2021-07-24
      • 1970-01-01
      • 2019-05-27
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多