2

只要按住一个键,我就想显示图像。如果不再按下键(KEYUP),图像应该消失。

在我的代码中,当我按住键时图像会出现,但我不会立即消失。任何人都知道为什么只要我按住我的键,图像就不会保持可见=?

button_pressed = False
for event in pygame.event.get():
    if event.type == KEYDOWN:               
        if event.key == K_UP:
            button_pressed = True
            print"True"

    if event.type == KEYUP:
        if event.key == K_UP:
            button_pressed = False

if button_pressed:
    DISPLAYSURF.blit(arrow_left, (20, SCREEN_HEIGHT/2 - 28))

提前致谢!!

4

1 回答 1

3

一旦你把你的图像粘贴到屏幕上,它就会一直呆在那里,直到你在它上面画一些东西。它不会自行消失。

一个简单的解决方案是在主循环的每次迭代中清除屏幕,例如:

while running:
    DISPLAYSURF.fill((0, 0, 0)) # fill screen black

    for event in pygame.event.get():
        # handle events
        pass

    # I'm using 'key.get_pressed' here because using the KEYDOWN/KEYUP event
    # and a variable to track if a key is pressed totally sucks and people 
    # should stop doing this :-)
    keys = pygame.key.get_pressed() 
    if keys[pygame.K_UP]:
        # only draw image if K_UP is pressed.
        DISPLAYSURF.blit(arrow_left, (20, SCREEN_HEIGHT/2 - 28))

    pygame.display.flip()
于 2013-09-13T14:04:43.650 回答