为什么此代码用于检查pygame中是否按下两个键?
下面的代码是我使用pygame编写的代码的一部分,只是为了检查左键和/或右键是否被按下并相应地移动播放器IMG。除最后一个线以外,所有线路都在运行和更新游戏的时循环内部。按下左图应使左图true,并将playerx_change设置为-0.1(用于更新播放器的位置)。按正确的键应相反。
所有功能都可以与左键,IMG移动和控制台中的消息打印。
但是,正确的密钥不起作用。使用打印来测试某些东西,表明按正确的键确实设置为true(至少暂时),但是当稍后检查右_presse时,现在是错误的,并且不会移动IMG或打印消息。
(此外,最终的Elif语句是这样,如果左右按下左派和右手,则播放器IMG不会移动。)
关于为什么正确的键在这种情况下与左派有所不同的想法,或者为什么Right_pressed可能会切换到false太快会很棒。
任何帮助都将受到赞赏!谢谢! :) '''
keys_pressed = pygame.key.get_pressed()
right_pressed = False
left_pressed = False
if keys_pressed[pygame.K_RIGHT]:
right_pressed = True
elif keys_pressed[pygame.K_LEFT]:
left_pressed = True
if pygame.K_RIGHT not in keys_pressed:
right_pressed = False
elif pygame.K_LEFT not in keys_pressed:
left_pressed = False
if right_pressed:
playerX_change = 0.1
print("Right pressed")
elif left_pressed:
playerX_change = -0.1
print("Left pressed")
elif right_pressed and left_pressed:
playerX_change = 0
print("Right and left pressed")
else:
playerX_change = 0
playerX += playerX_change
'''
This code below is a portion of code I wrote using pygame just to check if left key AND/OR right key are being pressed and to move the player img accordingly. All lines besides the final one is inside the while loop that runs and updates the game. Pressing left should make left_pressed true and set playerX_change to -0.1 (which is used to update the location of the player). Pressing the right key should do the opposite.
All functionality works with the left key, the img moves and the message prints in the console.
However, the right key does not work. Using print to test some things showed that pressing the right key does set right_pressed to True (at least for a moment) but when right_pressed is checked later on it is now False and will not move the img or print a message.
(Also that final elif statement is so that if left and right are pressed at the same time the player img will not move.)
Any ideas for why the right key is different from the left in this situation or why right_pressed might be switching to False too quickly would be amazing.
Any help is appreciated! Thanks! :)
'''
keys_pressed = pygame.key.get_pressed()
right_pressed = False
left_pressed = False
if keys_pressed[pygame.K_RIGHT]:
right_pressed = True
elif keys_pressed[pygame.K_LEFT]:
left_pressed = True
if pygame.K_RIGHT not in keys_pressed:
right_pressed = False
elif pygame.K_LEFT not in keys_pressed:
left_pressed = False
if right_pressed:
playerX_change = 0.1
print("Right pressed")
elif left_pressed:
playerX_change = -0.1
print("Left pressed")
elif right_pressed and left_pressed:
playerX_change = 0
print("Right and left pressed")
else:
playerX_change = 0
playerX += playerX_change
'''
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您使用的和
elif
使用,如果第一个如果为true,则代码将永远不会处理elif
。因此,在这种情况下,如果keys_pressed [pygame.k_right]
是正确的,则永远不会检查keys_pressed [pygame.k_left]
也是正确的。因此,进行此更改:
您在代码开始时设置了
right_pressed
和left_pressed
tofalse
。仅当按下键时,它们才会更改,因此无需将它们设置回false
。最后,您应该首先检查
right_pressed和Left_pressed
。否则,由于 elif 行为,我前我已经减少了代码,请尝试一下。它将检查右按下是否不左左,则如果按下左侧,则不右。否则,
plaryx_change
将设置为0 (因为这是按压或无按压的值)。Because your using
if
andelif
, if the firstif
is True the code will never process theelif
. So in this Case, ifkeys_pressed[pygame.K_RIGHT]
is True, it will never look to check ifkeys_pressed[pygame.K_LEFT]
is also True.So make this change:
You set
right_pressed
andleft_pressed
toFalse
at the start of the Code. They are only changed if the key is pressed, so there is no need to set them back toFalse
.Finally, you should check
right_pressed and left_pressed
first. Otherwise, due to theif
andelif
behavior I mentioned earlier, if one of those are True it will never make it to the Code that checks for both.I've reduced the code, have a try with this. It will check if Right is pressed and not Left, then if Left is pressed and not Right. Otherwise,
playerX_change
will be set to 0 (as that is the value for either both pressed or none pressed).