【问题标题】:How to get keyboard input in pygame?如何在pygame中获取键盘输入?
【发布时间】:2022-01-22 12:04:48
【问题描述】:

我正在使用 pygame 1.9.2 制作游戏。 这是一个失败的简单游戏,其中一艘船在五列坏人之间移动,他们通过缓慢向下移动来攻击。我正试图让船用左右箭头键左右移动。这是我的代码:

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    location-=1
    if location==-1:
        location=0
if keys[K_RIGHT]:
    location+=1
    if location==5:
        location=4

效果太好了。船移动得太快了。让它只向左或向右移动一个位置几乎是不可能的。我怎样才能让它每次按下键时船只移动一次?

【问题讨论】:

    标签: python pygame keyboard


    【解决方案1】:

    pygame.key.get_pressed() 返回一个包含每个键状态的列表。如果按住某个键,则该键的状态为True,否则为False。使用pygame.key.get_pressed() 评估按钮的当前状态并获得连续移动:

    while True:
    
        keys = pygame.key.get_pressed()
        x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
        y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed
    

    键盘事件(请参阅pygame.event 模块)仅在按键状态更改时发生一次。每次按下一个键时,KEYDOWN 事件就会发生一次。每次释放键时,KEYUP 发生一次。将键盘事件用于单个操作或移动:

    while True:
    
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x -= speed
                if event.key == pygame.K_RIGHT:
                    x += speed
                if event.key == pygame.K_UP:
                    y -= speed
                if event.key == pygame.K_DOWN:
                    y += speed
    

    另见Key and Keyboard event


    连续移动的最小例子: replit.com/@Rabbid76/PyGame-ContinuousMovement

    import pygame
    
    pygame.init()
    window = pygame.display.set_mode((300, 300))
    clock = pygame.time.Clock()
    
    rect = pygame.Rect(0, 0, 20, 20)
    rect.center = window.get_rect().center
    vel = 5
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                print(pygame.key.name(event.key))
    
        keys = pygame.key.get_pressed()
        
        rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
        rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
            
        rect.centerx = rect.centerx % window.get_width()
        rect.centery = rect.centery % window.get_height()
    
        window.fill(0)
        pygame.draw.rect(window, (255, 0, 0), rect)
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    单个操作的最小示例: replit.com/@Rabbid76/PyGame-ShootBullet

    import pygame
    pygame.init()
    
    window = pygame.display.set_mode((500, 200))
    clock = pygame.time.Clock()
    
    tank_surf = pygame.Surface((60, 40), pygame.SRCALPHA)
    pygame.draw.rect(tank_surf, (0, 96, 0), (0, 00, 50, 40))
    pygame.draw.rect(tank_surf, (0, 128, 0), (10, 10, 30, 20))
    pygame.draw.rect(tank_surf, (32, 32, 96), (20, 16, 40, 8))
    tank_rect = tank_surf.get_rect(midleft = (20, window.get_height() // 2))
    
    bullet_surf = pygame.Surface((10, 10), pygame.SRCALPHA)
    pygame.draw.circle(bullet_surf, (64, 64, 62), bullet_surf.get_rect().center, bullet_surf.get_width() // 2)
    bullet_list = []
    
    run = True
    while run:
        clock.tick(60)
        current_time = pygame.time.get_ticks()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
            if event.type == pygame.KEYDOWN:
                bullet_list.insert(0, tank_rect.midright)
    
        for i, bullet_pos in enumerate(bullet_list):
            bullet_list[i] = bullet_pos[0] + 5, bullet_pos[1]
            if bullet_surf.get_rect(center = bullet_pos).left > window.get_width():
                del bullet_list[i:]
                break
    
        window.fill((224, 192, 160))
        window.blit(tank_surf, tank_rect)
        for bullet_pos in bullet_list:
            window.blit(bullet_surf, bullet_surf.get_rect(center = bullet_pos))
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    【讨论】:

      【解决方案2】:

      您可以从 pygame 获取事件,然后注意 KEYDOWN 事件,而不是查看 get_pressed() 返回的键(它为您提供当前按下的键,而 KEYDOWN 事件显示你在那个框架上按下了哪些键)。

      您的代码现在发生的情况是,如果您的游戏以 30fps 的速度渲染,并且您按住左箭头键半秒,您将更新位置 15 次。

      events = pygame.event.get()
      for event in events:
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_LEFT:
                  location -= 1
              if event.key == pygame.K_RIGHT:
                  location += 1
      

      要在按住某个键时支持连续移动,您必须建立某种限制,或者基于游戏循环的强制最大帧速率,或者通过一个只允许您移动的计数器循环的滴答声。

      move_ticker = 0
      keys=pygame.key.get_pressed()
      if keys[K_LEFT]:
          if move_ticker == 0:
              move_ticker = 10
              location -= 1
              if location == -1:
                  location = 0
      if keys[K_RIGHT]:
          if move_ticker == 0:   
              move_ticker = 10     
              location+=1
              if location == 5:
                  location = 4
      

      然后在游戏循环中的某个地方你会做这样的事情:

      if move_ticker > 0:
          move_ticker -= 1
      

      这只会让你每 10 帧移动一次(所以如果你移动,ticker 会设置为 10,并且在 10 帧之后它会允许你再次移动)

      【讨论】:

      • 我尝试使用它,但问题是它只有在我在 pygame 正在寻找该事件的确切时刻按下按钮时才会移动。当我想要它时它不会移动。
      • 您必须在每次更新游戏循环时检查您的事件。你是这样做的吗?事件将保留在 Pygame 的事件队列中,直到您读取它们或抽取它们。
      • Dan,这个答案正是您所要求的,但请记住,如果您能够控制速度,则使用事件您将无法连续移动您可能想要的船。您可以保留原始代码,一旦检测到按键被按下,您可以轻松地忽略 N 循环按下的按钮。
      • Dan,我还更新了我的答案,向您展示了一种方法,您可以按住一个键并仍然管理玩家移动的速度。
      • 我对它可以移动的位置进行了限制,但是我的游戏窗口足够小(只有 5 个位置),我不需要重复。如果我打算让它更大,我会记住你。
      【解决方案3】:
      import pygame
      pygame.init()
      pygame.display.set_mode()
      while True:
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  pygame.quit(); #sys.exit() if sys is imported
              if event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_0:
                      print("Hey, you pressed the key, '0'!")
                  if event.key == pygame.K_1:
                      print("Doing whatever")
      

      请注意 K_0 和 K_1 不是唯一的键,要查看所有键,请参阅 pygame 文档,否则,请在输入后点击tab

      pygame.

      (注意 .pygame 之后的)进入一个空闲程序。请注意,K 必须大写。另请注意,如果您不为 pygame 提供显示大小(不传递参数),那么它将自动使用计算机屏幕/监视器的大小。编码愉快!

      【讨论】:

        【解决方案4】:

        这背后的原因是 pygame 窗口以 60 fps(每秒帧数)运行,当您按下键 1 秒时,它会根据事件块的循环更新 60 帧。

        clock = pygame.time.Clock()
        flag = true
        while flag :
            clock.tick(60)
        

        请注意,如果您的项目中有动画,那么图像的数量将定义tick() 中的值的数量。假设你有一个角色,它需要 20 组图像来行走和跳跃,那么你必须让 tick(20) 以正确的方式移动角色。

        【讨论】:

          【解决方案5】:

          我认为你可以使用:

          pygame.time.delay(delayTime)
          

          其中delayTime 以毫秒为单位。

          把它放在事件之前。

          【讨论】:

            【解决方案6】:

            要减慢您的游戏速度,请使用pygame.clock.tick(10)

            【讨论】:

              【解决方案7】:

              仅供参考,如果您想确保飞船不会离开屏幕

              location-=1
              if location==-1:
                  location=0
              

              你可能会更好地使用

              location -= 1
              location = max(0, location)
              

              这样,如果它跳过 -1,你的程序就不会中断

              【讨论】:

                【解决方案8】:

                以上所有答案都太复杂了,我只需将变量更改为 0.1 而不是 1 这使船慢了 10 倍 如果这仍然太快,将变量更改 0.01 这使船慢了 100 倍 试试这个

                keys=pygame.key.get_pressed()
                if keys[K_LEFT]:
                    location -= 0.1 #or 0.01
                    if location==-1:
                    location=0
                if keys[K_RIGHT]:
                    location += 0.1 #or 0.01
                    if location==5:
                        location=4
                

                【讨论】:

                • 嘿,除了所有动作都必须是整数值之外,这将起作用。进入四舍五入也是不必要的。使用 pygame.KEYDOWN 事件是我正在寻找的解决方案,每次按下按钮移动一次。当只有 5 个可能的位置时,它运行良好。
                【解决方案9】:

                试试这个:

                keys=pygame.key.get_pressed()
                if keys[K_LEFT]:
                    if count == 10:
                        location-=1
                        count=0
                    else:
                        count +=1
                    if location==-1:
                        location=0
                if keys[K_RIGHT]:
                    if count == 10:
                        location+=1
                        count=0
                    else:
                        count +=1
                    if location==5:
                        location=4
                

                这意味着您只移动 1/10 的时间。如果它仍然快速移动,您也可以尝试增加您设置的“count”值。

                【讨论】:

                  【解决方案10】:

                  您应该按照docs 中的说明使用clock.tick(10)

                  【讨论】:

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