Pygame,只要发生按键事件就会发生动画

时间:2014-03-17 01:30:13

标签: python pygame

我试图在按下右箭头键后才动画,只有这样。我试过把循环" currentimage" if语句中的变量,但nothign似乎有效。我想在按下键时设置一个布尔值为true但是由于某种原因(稍后在同一个循环中)没有触发它?当我按下右箭头键时,如何让动画工作?这是我的代码。

import pygame

pygame.init()

window = pygame.display.set_mode((1200,720))

pygame.display.set_caption("Window")

x,y=0,0
movex, movey = 0, 0
animation_boy = 0


clock = pygame.time.Clock()

image=pygame.image.load("images/10.png").convert_alpha()
image2=pygame.image.load("images/10(3).png").convert_alpha()
image=pygame.transform.scale(image, (93, 262))
image2=pygame.transform.scale(image2, (93, 262))

currentimage = 1
animation_boy=False

gameLoop=True
while gameLoop:

    mousex,mousey = pygame.mouse.get_pos()
    for event in pygame.event.get():

        if (event.type==pygame.QUIT):

            gameLoop=False

        if (event.type==pygame.KEYDOWN):

            if (event.key==pygame.K_RIGHT):

                movex = 3
                animation_boy=True

            if (event.key==pygame.K_LEFT):

                movex = -3

            if (event.key==pygame.K_UP):

                movey = -3

            if (event.key==pygame.K_DOWN):

                movey = 3


        if (event.type==pygame.KEYUP):

            if (event.key==pygame.K_RIGHT):

                movex = 0

            if (event.key==pygame.K_LEFT):

                movex = 0

            if (event.key==pygame.K_UP):

                movey = 0

            if (event.key==pygame.K_DOWN):

                movey = 0

    window.fill((0,0,0))

    x+=movex
    y+=movey


    window.blit(image2, (x,y))

    if (animation_boy==True):

        if (currentimage <= 25):

            window.blit(image2, (x,y))


        elif (currentimage >= 25 ) and (currentimage <= 50):

            window.blit(image, (x,y))

        else:

            currentimage=1

    clock.tick(50)

    pygame.display.flip()

pygame.quit()

1 个答案:

答案 0 :(得分:1)

正确,因此变量currentimage永远不会在循环和块中更新:

if (animation_boy==True):
    if (currentimage <= 25):
        window.blit(image2, (x,y))
    elif (currentimage >= 25 ) and (currentimage <= 50):
        window.blit(image, (x,y))
    else:
        currentimage = 1

唯一一直执行的操作是window.blit(image2,(x,y)),这也是您的默认图片。

相关问题