只要我按住一个键,我就想显示一个图像。如果没有按下键(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))
提前致谢!!
答案 0 :(得分:3)
一旦你将图像blittet到屏幕上,它就会一直停留在那里,直到你在它上面画了一些东西。它不会自行消失。
一个简单的解决方案是在主循环的每次迭代中清除屏幕,例如类似的东西:
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()