动画帮助,Python 3.2 与 Pygame



我目前正在制作一个横向卷轴游戏。我画了一个简笔画,并画了一个动画,当"W"键被帮助时,他会向右走。问题是我不知道如何在我按住 W 时使原始绘图(他保持静止的那个)消失,以便它们相互重叠。这是我的代码:

def pulse_ninja(screen,x,y):
    #Head
    pygame.draw.ellipse(screen,PULSEPURPLE,[14+x,-8+y,15,15],0)
     #Legs
    pygame.draw.line(screen,WHITE,[20+x,17+y],[25+x,27+y],4)
    pygame.draw.line(screen,WHITE,[20+x,17+y],[15+x,27+y],4)
     #Body
    pygame.draw.line(screen,PULSEPURPLE,[20+x,16+y],[20+x,-2+y],4)
     #Arms
    pygame.draw.line(screen,PULSEPURPLE,[20+x,3+y],[30+x,18+y],4)
    pygame.draw.line(screen,PULSEPURPLE,[20+x,3+y],[10+x,18+y],4)
    #Sword
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_w:
            pygame.draw.line(screen,GREEN,[30+x,18+y],[35+x,0+y],3)
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_w:
            pygame.draw.line(screen,GREEN,[30+x,18+y],[50+x,16+y],3)
def ninja_animate_right(screen,x,y):
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_d:
            # Head
            pygame.draw.ellipse(screen,PULSEPURPLE,[16+x,-6+y,15,15],0)
            # Legs
            pygame.draw.arc(screen,WHITE,[10+x,0+y,15,30], 3*pi/2, 2*pi, 2)
            #pygame.draw.arc(screen,WHITE,[20+x,17+y],[15+x,27+y],4)
            # Body
            pygame.draw.line(screen,PULSEPURPLE,[20+x,16+y],[25+x,-2+y],4)
            # Arms
            pygame.draw.line(screen,PULSEPURPLE,[20+x,3+y],[30+x,18+y],4)
            pygame.draw.line(screen,PULSEPURPLE,[20+x,3+y],[10+x,18+y],4) 
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_w:
            pygame.draw.line(screen,GREEN,[30+x,18+y],[35+x,0+y],3)
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_w:
            pygame.draw.line(screen,GREEN,[30+x,18+y],[50+x,16+y],3)

当我打电话给他们时,我只是让他们一行接一行

pulse_ninja(screen,x,y)
ninja_animate_right(screen,x,y)

我猜我需要一段时间循环?是否有停止模块?假设我想运行一个函数,然后在满足条件后停止它。这基本上就是我想做的。

好吧,在你的类中,一个小建议,定义一个状态:向左移动、向右移动、停止等。在你的移动字段中,根据角色的当前状态绘制东西。因此,您唯一需要做的就是:

if event.type == pygame.KEYUP:
    if event.key == pygame.K_w:
        character.state = WALKING
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_w:
        character.state = STANDING

在移动函数中,您将查看当前状态,并相应地绘制。我建议使用类,因为变量和函数会更有条理。

最新更新