【发布时间】:2016-08-29 07:55:10
【问题描述】:
我在main_screen 上添加了一个对象/图像,该对象称为cancer_cell。
我在这里想要做的是我希望对象顺利移动。我必须反复按箭头键才能保持移动。
如何让它移动while 箭头键被按下?
代码如下:
exitgame = False
cellpos_x = 0
cellpos_y = cancer_cell.get_rect().height*2
while not exitgame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitgame = True
quitgame()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
cellpos_x -= 10
if event.key == pygame.K_RIGHT:
cellpos_x += 10
gameplay_bg = pygame.image.load("/Users/wolf/Desktop/python/img/gameplay_bg.png").convert()
main_screen.fill(white)
main_screen.blit(gameplay_bg, [0,0])
main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
pygame.display.flip()
clock.tick(20)
有人告诉我在How to use pygame.KEYDOWN 尝试解决方案: 但这也没有用。或者我做错了:
if event.type == pygame.KEYDOWN:
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT]:
cellpos_x -= 10
if key_pressed[pygame.K_RIGHT]:
cellpos_x += 10
【问题讨论】:
-
你可能想澄清你在问什么。
-
所以我在
main_screen上有这个对象/图像,称为cancer_cell,当我用箭头键移动时,我必须重复按,否则如果我按住键它不会移动下来。 -
请编辑问题而不是评论。
-
而且solution 并不难找到。
-
我之前试过那个解决方案,不行。