简体   繁体   中英

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

        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:

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 .

Since pygame.K_DOWN is a non zero value, it is true when evaluated as a boolean value. To fix this you want to do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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