
用pygame实现的爆炸效果,游戏中经常要用到的。下面是部分代码预览:
"""
酷酷的爆炸效果类
"""
import time
import pygame
from random import randint
from pygame.locals import *
class Explosion(pygame.sprite.Sprite):
"""
爆炸效果类,继承自Sprite
"""
def __init__(self,pos,frames,interval,group):
"""
frames:帧图,interval:帧播放间隔时间
"""
pygame.sprite.Sprite.__init__(self)
self.frames = frames
def set_costume(self):
"""设置造型"""
self.image = self.frames[self._index]
self.rect = self.rects[self._index]
def update(self):
"""
如果超时了,就切换到下一个造型
"""
def main():
"""
主调用函数,本函数新建screen,
每隔一定时间产生一个爆炸效果
"""
width,height = size = 960,720
screen = pygame.display.set_mode(size)
pygame.display.set_caption("酷酷的爆炸效果类Pygame版 by lixingqiu")
images = [f"frames/{i}.gif" for i in range(6)]
images = [pygame.image.load(im) for im in images]
pygame.time.set_timer(USEREVENT,randint(10,100))
clock = pygame.time.Clock()
group = pygame.sprite.Group() # 爆炸组
running = True
while running:
e = pygame.event.poll()
if e.type == QUIT:running = False
group.update()
screen.fill((60,0,160))
group.draw(screen)
pygame.display.update()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
