解释按下的键
Intepreting held down keys
我正在构建一个用 Python 编写的 Raspberry Pi
流动站,我希望通过 SSH
控制流动站。因此,我会 运行 脚本,然后希望漫游车按照我提供的实时方向移动,就像遥控车一样(想想向上键、向下键、向左键、向右键)。
我在网上看到,一种可能性是使用 pygame.key.get_pressed()
函数,但这在我的 shell 中不起作用。当我 运行 在我的 raspberry pi
python shell 中执行该命令时,我只收到一个零元组,该元组在几分之一秒后超时。
我的代码如下:
speed = input('How fast do you want the rover to go? Give a value lower than 1: ')
while True:
keys = pygame.key.get_pressed() #checking pressed keys
if keys == True:
if keys[pygame.K_UP]:
fwd(speed)
if keys[pygame.K_DOWN]:
bwd(speed)
if keys == False:
MS.motor1off
MS.motor2off
其中 fwd
和 bwd
是向前和向后激活电机的函数。
当我 运行 脚本时,它可以正常通过循环,但电机不会响应按住的键。当我添加打印语句时,我注意到它也没有打印到控制台上。
有人知道如何进行吗?
我不是 pygame 专家,也从未使用过 key.get_pressed()
方法。我使用 event.get()
效果很好。这是我使用的代码的最小示例:
import pygame
pygame.init()
WINDOW_WIDTH = 250
WINDOW_HEIGHT = 120
pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print('works!')
您必须确保初始化 pygame 并创建 window 并且 window 必须位于焦点中。然后它会起作用。如果这对您来说不方便,那么您必须挂钩键盘的按键事件,以便获得全局按键,无论 window 处于焦点。
希望对您有所帮助。
pygame.key.get_pressed()
returns 表示键状态的布尔值列表。列表永远不会等于 True
或 False
因此 if keys == True:
和 if keys == False:
子句中的代码将永远不会被执行。
所以只需删除这些 if
子句并在 if keys[pygame.K_UP]:
或两个语句之后使用 else
子句来关闭电机(不确定你到底想做什么) .
while True:
# You must make a call to the event queue every frame or the window will "freeze".
pygame.event.pump()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
fwd(speed)
elif keys[pygame.K_DOWN]:
bwd(speed)
else: # No key pressed.
MS.motor1off
MS.motor2off
顺便说一下,如果 motor1off
和 motor2off
是方法,您可能想调用它们:MS.motor1off()
.
根据@WurzelseppQX
的代码,我制作了一个新代码,只要按住按钮就会打印文本,当按钮按下时将停止打印被释放。这是代码:
import pygame #importing pygame
pygame.init() #initializing pygame
width = 100 # width of window
height = 100 #height of window
pygame.display.set_mode((width, height)) # setting dimensions
while True:
tbreak = False # to break two loops
for event in pygame.event.get(): #getting events
if event.type == pygame.KEYDOWN: #see if a button is pressed
if event.key == pygame.K_w: # if button is 'W' then
while True: # making loop
print("Key 'W' pressed")
ev = pygame.event.get() #getting events to see if the user just released the key
for e in ev: #looping the events
if e.type == pygame.KEYUP: #if any key is released
#You can also set specific keys
tbreak = True #to break the outerloop
break #breaking the inner loop
if tbreak == True:break #breaking
现在上面的代码会在按下按钮时阻止游戏的其余部分。如果您不希望您的代码执行此操作,请使用以下代码:
global topass
topass = 0
def on_press(): #what to do on button press
global topass
print("Key 'W' pressed")
topass = 1
while True:
events = pygame.event.get()
if len(events) == 0 and topass == 1:on_press()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w or topass == 1:
on_press()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
topass = 0
我正在构建一个用 Python 编写的 Raspberry Pi
流动站,我希望通过 SSH
控制流动站。因此,我会 运行 脚本,然后希望漫游车按照我提供的实时方向移动,就像遥控车一样(想想向上键、向下键、向左键、向右键)。
我在网上看到,一种可能性是使用 pygame.key.get_pressed()
函数,但这在我的 shell 中不起作用。当我 运行 在我的 raspberry pi
python shell 中执行该命令时,我只收到一个零元组,该元组在几分之一秒后超时。
我的代码如下:
speed = input('How fast do you want the rover to go? Give a value lower than 1: ')
while True:
keys = pygame.key.get_pressed() #checking pressed keys
if keys == True:
if keys[pygame.K_UP]:
fwd(speed)
if keys[pygame.K_DOWN]:
bwd(speed)
if keys == False:
MS.motor1off
MS.motor2off
其中 fwd
和 bwd
是向前和向后激活电机的函数。
当我 运行 脚本时,它可以正常通过循环,但电机不会响应按住的键。当我添加打印语句时,我注意到它也没有打印到控制台上。
有人知道如何进行吗?
我不是 pygame 专家,也从未使用过 key.get_pressed()
方法。我使用 event.get()
效果很好。这是我使用的代码的最小示例:
import pygame
pygame.init()
WINDOW_WIDTH = 250
WINDOW_HEIGHT = 120
pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print('works!')
您必须确保初始化 pygame 并创建 window 并且 window 必须位于焦点中。然后它会起作用。如果这对您来说不方便,那么您必须挂钩键盘的按键事件,以便获得全局按键,无论 window 处于焦点。
希望对您有所帮助。
pygame.key.get_pressed()
returns 表示键状态的布尔值列表。列表永远不会等于 True
或 False
因此 if keys == True:
和 if keys == False:
子句中的代码将永远不会被执行。
所以只需删除这些 if
子句并在 if keys[pygame.K_UP]:
或两个语句之后使用 else
子句来关闭电机(不确定你到底想做什么) .
while True:
# You must make a call to the event queue every frame or the window will "freeze".
pygame.event.pump()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
fwd(speed)
elif keys[pygame.K_DOWN]:
bwd(speed)
else: # No key pressed.
MS.motor1off
MS.motor2off
顺便说一下,如果 motor1off
和 motor2off
是方法,您可能想调用它们:MS.motor1off()
.
根据@WurzelseppQX
的代码,我制作了一个新代码,只要按住按钮就会打印文本,当按钮按下时将停止打印被释放。这是代码:
import pygame #importing pygame
pygame.init() #initializing pygame
width = 100 # width of window
height = 100 #height of window
pygame.display.set_mode((width, height)) # setting dimensions
while True:
tbreak = False # to break two loops
for event in pygame.event.get(): #getting events
if event.type == pygame.KEYDOWN: #see if a button is pressed
if event.key == pygame.K_w: # if button is 'W' then
while True: # making loop
print("Key 'W' pressed")
ev = pygame.event.get() #getting events to see if the user just released the key
for e in ev: #looping the events
if e.type == pygame.KEYUP: #if any key is released
#You can also set specific keys
tbreak = True #to break the outerloop
break #breaking the inner loop
if tbreak == True:break #breaking
现在上面的代码会在按下按钮时阻止游戏的其余部分。如果您不希望您的代码执行此操作,请使用以下代码:
global topass
topass = 0
def on_press(): #what to do on button press
global topass
print("Key 'W' pressed")
topass = 1
while True:
events = pygame.event.get()
if len(events) == 0 and topass == 1:on_press()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w or topass == 1:
on_press()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
topass = 0