python动画精灵_python – 几张图片的动画精灵

您可以尝试修改您的精灵,以便在更新内部交换其图像以获得不同的精灵.这样,当渲染精灵时,它看起来就像动画一样.

编辑:

这是我制作的一个简单示例:

import pygame

import sys

def load_image(name):

image = pygame.image.load(name)

return image

class 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值