为什么此代码用于检查pygame中是否按下两个键?

发布于 2025-02-04 01:43:20 字数 1179 浏览 2 评论 0原文

下面的代码是我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2025-02-11 01:43:20

因为您使用的和elif使用,如果第一个如果为true,则代码将永远不会处理elif。因此,在这种情况下,如果keys_pressed [pygame.k_right]是正确的,则永远不会检查keys_pressed [pygame.k_left]也是正确的。

因此,进行此更改:

if keys_pressed[pygame.K_RIGHT]:
    right_pressed = True
# elif keys_pressed[pygame.K_LEFT]:
if keys_pressed[pygame.K_LEFT]:
    left_pressed = True

您在代码开始时设置了right_pressedleft_pressed to false。仅当按下键时,它们才会更改,因此无需将它们设置回false

最后,您应该首先检查right_pressed和Left_pressed。否则,由于 elif 行为,我前

我已经减少了代码,请尝试一下。它将检查右按下是否不左左,则如果按下左侧,则不右。否则,plaryx_change将设置为0 (因为这是按压或无按压的值)

keys_pressed = pygame.key.get_pressed()

if keys_pressed[pygame.K_RIGHT] and not keys_pressed[pygame.K_LEFT]:
    playerX_change = 0.1
    print("Right pressed")
elif keys_pressed[pygame.K_LEFT] and not keys_pressed[pygame.K_RIGHT]:
    playerX_change = -0.1
    print("Left pressed")
else:
    playerX_change = 0

playerX += playerX_change

Because your using if and elif, if the first if is True the code will never process the elif. So in this Case, if keys_pressed[pygame.K_RIGHT] is True, it will never look to check if keys_pressed[pygame.K_LEFT] is also True.

So make this change:

if keys_pressed[pygame.K_RIGHT]:
    right_pressed = True
# elif keys_pressed[pygame.K_LEFT]:
if keys_pressed[pygame.K_LEFT]:
    left_pressed = True

You set right_pressed and left_pressed to False at the start of the Code. They are only changed if the key is pressed, so there is no need to set them back to False.

Finally, you should check right_pressed and left_pressed first. Otherwise, due to the if and elif 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).

keys_pressed = pygame.key.get_pressed()

if keys_pressed[pygame.K_RIGHT] and not keys_pressed[pygame.K_LEFT]:
    playerX_change = 0.1
    print("Right pressed")
elif keys_pressed[pygame.K_LEFT] and not keys_pressed[pygame.K_RIGHT]:
    playerX_change = -0.1
    print("Left pressed")
else:
    playerX_change = 0

playerX += playerX_change
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文