简体   繁体   English

pygame 连续和同时按键输入

[英]pygame continuous and simultaneous key inputs

I am stuck again and cannot find any valid solutions online.我再次陷入困境,无法在线找到任何有效的解决方案。 I am trying to use pygame and its key inputs to control various things.我正在尝试使用 pygame 及其关键输入来控制各种事物。 Now I need to use several keys simultaneously.现在我需要同时使用几个键。 My code is as follows:我的代码如下:

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"

Now the problem that I have is: When I hold down "LEFT of RIGHT" it correctly and continuously registers that I pressed left/right.现在我遇到的问题是:当我按住“LEFT of RIGHT”时,它会正确地连续记录我向左/向右按下的信息。 BUT when I hold in "LEFT" and just tap "RIGHT", it registers that left and right were pressed but it then stops to register that "LEFT" is still being pressed.但是,当我按住“LEFT”并轻按“RIGHT”时,它会记录左和右已被按下,但随后停止以记录“LEFT”仍在被按下。

Any ideas anyone?任何人的想法? Any help would be greatly appreciated.任何帮助将不胜感激。 Misha米莎

You have misspelled repeat in pygame.key.repeat(). 您在pygame.key.repeat()中拼写了重复的拼写错误。 I corrected this and it worked for me. 我纠正了这个问题,对我有用。

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"

In my code the "repeat" is correctly spelt. 在我的代码中,“重复”是正确拼写的。

I found the work around for my problem. 我找到了解决我的问题的方法。 The above code needs to be modified. 上面的代码需要修改。

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()

I double checked, the repeat is correctly spelt and it would not continue a keyboard input once another one was tapped. 我仔细检查了一下,重复的拼写是否正确,一旦敲击另一个键盘,它就不会继续进行键盘输入。 The problem is, as far as I can figure it out, and even occurs once at the point when a key is pressed. 据我所知,问题是,甚至在按下某个键时发生一次。 When another key is simultaneously pressed the event is lost. 同时按下另一个键时,事件丢失。

Thus the solution is to set a variable true until the key is lifted up, and thus the variable is set false. 因此,解决方案是将变量设置为true,直到提起钥匙为止,从而将变量设置为false。

If you want to use continuous inputs than try this, it is my code.如果你想使用连续输入而不是尝试这个,这是我的代码。

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()

If you want to use simultaneous input, then here:如果你想使用同时输入,那么这里:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM