1

我目前正在制作横向滚动游戏。我画了一个简笔画,画了一个动画,当'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)

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

4

1 回答 1

1

好吧,一个小建议,在你的课堂上,定义一个状态:向左、向右、停止等。在你的移动领域,根据你角色的当前状态绘制东西。所以你唯一需要做的是:

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

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

于 2012-10-02T18:30:11.973 回答