Skip to main content
 首页 » 编程设计

python之在pygame中使用关键事件移动 Sprite

2025年05月04日9bhlsheji

<分区>

我刚刚使用“w,a,s,d”让一个 Sprite 在窗口周围移动,我想通过按空格键让这个 Sprite “射击”。

我遇到的问题是 Sprite 出现但在我释放空格键之前不会移动,我希望 Sprite 镜头向前移动直到窗口结束,只需按下空格键,而不是通过释放它。

这是我的主循环:

while pygame.event.poll().type != QUIT: 
 
 
    screen.blit(background, (0, 0)) 
 
    #"player" is the sprite moving around the window 
    player.move(width,height) 
    screen.blit(player.image, player.rect) 
    key = pygame.key.get_pressed() 
 
    if key[K_SPACE]: 
        xpos = player.rect.right 
        ypos = player.rect.top 
        shot_on_screen = True 
 
 
    if shot_on_screen: 
        xpos += 1 
        #"kame" is the sprite i want to move forward 
        screen.blit(shot.kame, (xpos, ypos)) 
    else: 
        shot.x = player.rect.right 
        shot.y = player.rect.top 
        shot_on_screen = False 
 
 
    pygame.display.update() 

我是这个 python-pygame 世界的新手,我在询问之前已经检查了很多手册和文档,希望您能提供帮助,谢谢。