Trail:烟花轨迹,本质上是一个点。创建 Trail 类,定义 show 方法绘制轨迹 、 实时获取轨迹坐标
class Trail:def __init__(self, n, size, dynamic):self.pos_in_line = nself.pos = vector(-10, -10)self.dynamic = dynamicif self.dynamic:self.colour = trail_colours[n]self.size = int(size - n / 2)else:self.colour = (255, 255, 200)self.size = size - 2if self.size < 0:self.size = 0def get_pos(self, x, y):self.pos = vector(x, y)def show(self, win):pygame.draw.circle(win, self.colour, (int(self.pos.x), int(self.pos.y)), self.size)def update(win, fireworks):for fw in fireworks:fw.update(win)if fw.remove():fireworks.remove(fw)pygame.display.update()
主函数部分:
def main():pygame.init()pygame.font.init()pygame.display.set_caption("祝您新年快乐")# 标题background = pygame.image.load("./5.png")# 背景sound_wav = pygame.mixer.music.load("2.mp3")pygame.mixer.music.play()pygame.init()# 加载背景音乐'''pygame.mixer.music.load("./res/音乐文件名")# 循环播放背景音乐pygame.mixer.music.play(-1)# 停止背景音乐pygame.mixer.music.stop()# 加载音效boom_sound = pygame.mixer.Sound("./res/音效名")# 播放音效boom_sound.play()boom_sound.stop()myfont = pygame.font.Font("simkai.TTF", 80)myfont1 = pygame.font.Font("simkai.ttf", 30)testsurface = myfont.render("虎虎生威", False, (0, 0, 0), (220, 20, 60))testsurface1 = myfont1.render("", False, (251, 59, 85))'''# pygame.image.load("")win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))# win.blit(background)clock = pygame.time.Clock()fireworks = [Firework() for i in range(2)]# create the first fireworksrunning = Truewhile running:clock.tick(60)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:# Change game speed with number keysif event.key == pygame.K_1:# 按下 1fireworks.append(Firework())if event.key == pygame.K_2:# 按下 2 加入10个烟花for i in range(10):fireworks.append(Firework())if event.key == pygame.K_3:# 按下 3 加入100个烟花for i in range(100):fireworks.append(Firework())win.fill((20, 20, 30))# draw background#win.blit(background, (0, 0))#win.blit(testsurface, (200, 30))#win.blit(testsurface1, (520, 80))if randint(0, 20) == 1:# 创建新的烟花fireworks.append(Firework())update(win, fireworks)pygame.quit()quit()
运行游戏:
if __name__ == 'main':main()
效果展示
- 推荐一个会非常好用的记事本工具ediray
- 古代娼女怎么避孕的?竟用鱼膘用作避孕套
- 八 音频——C语言生成正弦波并用 I2S 输出
- 50g流量能用多久
- 教你用树莓派Python打造一个音乐播放器
- 含锌高的食物和水果 含锌高的食物和水果蔬菜
- 医用耗材的定义和类型-医疗院内SPD系统
- matlab实现 特征值分解用于数据压缩
- 淘宝客优惠券领取微信小程序前台和后台C#源码
- 推荐几个好用的在线代码编译器