简体   繁体   English

我的动画不会在PyGame中移动

[英]My animation wont move in PyGame

My main code in the loop works fine by itself but for some reason this wont move and stays on the first image. 我在循环中的主要代码本身可以正常工作,但是由于某种原因,它不会移动并停留在第一张图像上。 Please help. 请帮忙。 Im new to this. 我是新来的。 The main loop executes fine by itself in another program but when i add the thing at the start to create the base it doesnt work. 主循环本身可以在另一个程序中很好地执行,但是当我在开始添加东西以创建基础时,它就无法正常工作。 My only theory is that the base thing has to be in the main loop? 我唯一的理论是基础事物必须在主循环中?

import pygame
pygame.init()

Window = pygame.display.set_mode((480,48))
pygame.display.set_caption("Mario Animation")

black = (0,0,0)

#Takes in the image for the base
Base1 = pygame.image.load("images/Base1.png")
Base2 = pygame.image.load("images/Base2.png")
Base3 = pygame.image.load("images/Base3.png")

#Takes the sprites for mario
MarioRunning1 = pygame.image.load("images/Mario1.png")
MarioRunning2 = pygame.image.load("images/Mario1.png")
MarioRunning3 = pygame.image.load("images/Mario1.png")

BaseX = 0

clock = pygame.time.Clock()

for i in range (10):
     Window.blit(Base1, ((BaseX,32)))
     BaseX=BaseX+16
     Window.blit(Base2, ((BaseX,32)))
     BaseX=BaseX+16
     Window.blit(Base3, ((BaseX,32)))
     BaseX=BaseX+16

CurrentImage = 1

MainLoop = True
while MainLoop:

    for event in pygame.event.get():

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

    if (CurrentImage == 1): 
        Window.blit(MarioRunning3, (0,0))

    if (CurrentImage == 2):
        Window.blit(MarioRunning2, (0,0))

    if (CurrentImage == 3):
        Window.blit(MarioRunning1, (0,0))

    if (CurrentImage == 3):
        CurrentImage = 1

    else:
        CurrentImage+=1

    pygame.display.flip()

    clock.tick(5)

pygame.quit()

As @ChristianRapp said, the MarioRunning variables all refer to the said image, Mario1.png . 正如@ChristianRapp所说, MarioRunning变量都引用了所说的图像Mario1.png Try changing that chunk to: 尝试将该块更改为:

MarioRunning1 = pygame.image.load("images/Mario1.png")
MarioRunning2 = pygame.image.load("images/Mario2.png")
MarioRunning3 = pygame.image.load("images/Mario3.png")

That's assuming you have the 3 Mario sprites necessary. 假设您有3个必要的Mario精灵。 It also seems that you're never changing CurrentImage when you swap to an image, so it will always be 1 , and will never change. 看起来,当您交换图像时,您永远不会更改CurrentImage ,因此它始终为1 ,并且永远不会更改。 Try: 尝试:

    if (CurrentImage == 1): 
        Window.blit(MarioRunning2, (0,0))
        CurrentImage = 2

    elif (CurrentImage == 2):
        Window.blit(MarioRunning3, (0,0))
        CurrentImage == 3

    else:
        Window.blit(MarioRunning1, (0,0))
        CurrentImage = 1

This way, it always goes to the next one in the cycle. 这样,它总是转到循环中的下一个。 Oh, and the reason the image won't move, is you're not telling it to. 哦,图片不会移动的原因是您没有告诉它。 For example, the (0, 0) part of the line Window.blit(MarioRunning1, (0,0)) tells pygame where to put the image, in pixels. 例如, (0, 0)的线路的一部分Window.blit(MarioRunning1, (0,0))告诉pygame的哪里放置图像,以像素为单位。 Changing these numbers will change where the image is put. 更改这些数字将更改放置图像的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM