我在循环中的主要代码本身工作正常,但由于某种原因,它不会移动并停留在第一个图像上。请帮忙。我是新手。主循环在另一个程序中自行执行正常但是当我在开始时添加东西来创建基础时它不起作用。我唯一的理论是,基本的东西必须在主循环中?
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()
答案 0 :(得分:0)
正如@ChristianRapp所说, MarioRunning 变量都是指所谓的图像 Mario1.png 。尝试将该块更改为:
MarioRunning1 = pygame.image.load("images/Mario1.png")
MarioRunning2 = pygame.image.load("images/Mario2.png")
MarioRunning3 = pygame.image.load("images/Mario3.png")
假设您需要3个马里奥精灵。当你交换图像时,你似乎永远不会改变 CurrentImage ,所以它总是 1 ,永远不会改变。尝试:
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
这样,它总是进入循环中的下一个。哦,以及图像不会移动的原因,你是不是在告诉它。例如,行(0, 0)
的{{1}}部分告诉pygame放置图像的位置(以像素为单位)。更改这些数字将改变图像的放置位置。