简体   繁体   English

Pygame我只能移动一次图像,但不知道如何两次移动图像

[英]Pygame I can move an image once but don't know how to move it twice

I have a box that i want to move in increments, i know how to move it by an increment but only once. 我有一个要增量移动的框,我知道如何增量移动,但只能移动一次。 I don't how i'd do this multiple times. 我不怎么会多次这样做。 anyone got any ideas? 任何人有任何想法吗? The box: 包装盒:

highlight = pygame.draw.rect(window, (darkYellow),(30, 300, 130, 40),0)

Moving the box (this is to move it once) 移动盒子(一次移动一次)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()
        if event.type == pygame.KEYDOWN and pygame.K_RIGHT:
            highlight = pygame.draw.rect(window, (darkYellow),(260, 300, 130, 40),0)
            difficultyText = myFont.render("Difficulty", 1, red)
            window.blit(difficultyText, (260, 300))
            pygame.display.update()

Takes any key input when i only want it to activate when the key "ARROW DOWN" is pushed 当我只希望在按下键“ ARROW DOWN”时激活它时,可以接受任何键输入

        if event.type == pygame.KEYDOWN and pygame.K_DOWN:
            window.blit(arrowImg, (5,400))
            pygame.display.update()

There are few issues with your code. 您的代码几乎没有问题。

You are drawing, rendering text and bliting in your event loop. 您正在事件循环中绘制,渲染文本和变白。 You should move them out of the loop body. 您应该将它们移出循环体。

It seems that you want to have a cursor that will change its position with the right arrow key. 看来您想要一个可以用右箭头键更改其位置的光标。

To do this have a variable that will tell you where your cursor is. 为此,请使用一个变量,该变量将告诉您光标所在的位置。

Then your draw statement will look like this: 然后,您的draw语句将如下所示:

pygame.draw.rect(window, (darkYellow),(30+cursorPos*230, 300, 130, 40),0)

EDIT: 编辑:

As for why event.type == pygame.KEYDOWN and pygame.K_DOWN is true for all KEY_DOWN events, is because it can be translated to (event.type==pygame.KEYDOWN) and pygame.K_DOWN . 至于为什么event.type == pygame.KEYDOWN and pygame.K_DOWN对于所有KEY_DOWN事件都是true,是因为它可以转换为(event.type==pygame.KEYDOWN) and pygame.K_DOWN

Since pygame.K_DOWN is a non zero value, it is true when evaluated as a boolean value. 由于pygame.K_DOWN是非零值,因此当作为布尔值评估时为true。 To fix this you want to do something like this: 要解决此问题,您需要执行以下操作:

event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN

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

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