您可以尝试修改sprite,以便将其图像换成另一个内部的图像
update。这样,当精灵被渲染时,它将看起来很生气。
编辑 :
这是我草拟的一个简单示例:
import pygameimport sysdef load_image(name): image = pygame.image.load(name) return imageclass TestSprite(pygame.sprite.Sprite): def __init__(self): super(TestSprite, self).__init__() self.images = [] self.images.append(load_image('image1.png')) self.images.append(load_image('image2.png')) # assuming both images are 64x64 pixels self.index = 0 self.image = self.images[self.index] self.rect = pygame.Rect(5, 5, 64, 64) def update(self): '''This method iterates through the elements inside self.images and displays the next one each tick. For a slower animation, you may want to consider using a timer of some sort so it updates slower.''' self.index += 1 if self.index >= len(self.images): self.index = 0 self.image = self.images[self.index]def main(): pygame.init() screen = pygame.display.set_mode((250, 250)) my_sprite = TestSprite() my_group = pygame.sprite.Group(my_sprite) while True: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() sys.exit(0) # Calling the 'my_group.update' function calls the 'update' function of all # its member sprites. Calling the 'my_group.draw' function uses the 'image' # and 'rect' attributes of its member sprites to draw the sprite. my_group.update() my_group.draw(screen) pygame.display.flip()if __name__ == '__main__': main()
假定您有两个被调用的图像,
image1.png并且
image2.png在代码所在的同一文件夹中。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)