赞
踩
目录
首先非常感谢“我的python教程”分享的文章Python新年烟花代码-CSDN博客,这篇文章非常详细的介绍了Pygame 绘制烟花的基本原理。基于我对这篇文章的学习,我进一步对代码进行了改善,在烟花爆炸页面添加了祝福语和音效,希望对python初学者有所帮助。
这段代码的目的是渲染一段文本并将其存储在 text
变量中,以备后续在 Pygame 窗口中显示。
- # 渲染文本并居中
- font = pygame.font.SysFont(None, 100) # 使用系统默认字体
- text = font.render('Happy Spring Festival in 2024 !', True, (255, 0, 0)) # 将文本颜色改为红色
pygame.font.SysFont(None, 100)这个函数用于创建一个系统默认字体的字体对象。第一个参数是字体名称,传入 None
表示使用系统默认字体。第二个参数是字体大小,这里设置为 100。这样就创建了一个系统默认字体、大小为 100 的字体对象,存储在 font
变量中。
font.render('Happy Spring Festival in 2024 !', True, (255, 0, 0))是使用 font
对象的 render()
方法渲染文本。第一个参数是要渲染的文本内容,这里是 "Happy Spring Festival in 2024 !"。第二个参数指定是否启用抗锯齿,设置为 True
表示启用。第三个参数是文本的颜色,这里设置为红色 (255, 0, 0)
。渲染后的文本表面被存储在 text
变量中。
这段代码是用来初始化 Pygame 并加载音效文件的部分。
- # 初始化 Pygame
- pygame.init()
-
- # 加载音效
- explosion_sound = pygame.mixer.Sound("explosion.wav")
-
- # 加载背景音乐
- pygame.mixer.music.load("background_music.mp3")
- pygame.mixer.music.set_volume(0.9) # 设置音量
explosion_sound = pygame.mixer.Sound("explosion.wav")
这行代码加载了一个名为 "explosion.wav" 的音效文件,并将其存储在变量 explosion_sound
中。这表示可以在游戏中播放这个音效,比如在烟花爆炸时播放爆炸音效。
pygame.mixer.music.load("background_music.mp3")
: 这行代码加载了一个名为 "background_music.mp3" 的背景音乐文件,并使用 pygame.mixer.music.load()
函数将其加载到 Pygame 的音乐播放器中。这样就可以在程序运行时播放背景音乐。
pygame.mixer.music.set_volume(0.9)
: 这行代码设置了背景音乐的音量。音量是一个介于 0 和 1 之间的浮点数,其中 0 表示静音,1 表示最大音量。这里将音量设置为 0.9,以便音乐以较高的音量播放。
下列代码是一个Python程序的主函数(main
函数),它使用了Pygame库来创建一个简单的图形界面,并播放背景音乐,同时在屏幕上渲染文本“Happy Spring Festival in 2024!”。
- def main():
- pygame.display.set_caption('Happy Spring Festival in 2024 !')
- win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
- clock = pygame.time.Clock()
- fireworks = [Firework() for i in range(3)]
- running = True
- # 播放背景音乐
- pygame.mixer.music.play(-1) # -1 表示无限循环播放
- # 渲染文本并居中
- font = pygame.font.SysFont(None, 100) # 使用系统默认字体
- text = font.render('Happy Spring Festival in 2024 !', True, (255, 0, 0)) # 将文本颜色改为红色
-
- # 获取文本对象的宽度和高度
- text_width, text_height = text.get_size()
-
- # 计算文本对象的左上角坐标,使其位于页面正中
- text_x = (DISPLAY_WIDTH - text_width) // 2
- text_y = 20 # 调整文本的Y坐标使其位于页面顶部
-
- # 将文本对象渲染到窗口
- win.blit(text, (text_x, text_y))
- import math
- from random import randint, uniform, choice
- import pygame
-
- vector = pygame.math.Vector2
- gravity = vector(0, 0.3)
- DISPLAY_WIDTH = 1200
- DISPLAY_HEIGHT = 800
-
- trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75),
- (125, 125, 125), (150, 150, 150)]
- dynamic_offset = 1
- static_offset = 5
-
- # 初始化 Pygame
- pygame.init()
-
- # 加载音效
- explosion_sound = pygame.mixer.Sound("explosion.wav")
-
- # 加载背景音乐
- pygame.mixer.music.load("background_music.mp3")
- pygame.mixer.music.set_volume(0.9) # 设置音量
-
-
- class Firework:
-
- def __init__(self):
- self.colour = (randint(0, 255), randint(0, 255), randint(0, 255))
- self.colours = (
- (randint(0, 255), randint(0, 255), randint(0, 255)
- ), (randint(0, 255), randint(0, 255), randint(0, 255)),
- (randint(0, 255), randint(0, 255), randint(0, 255)))
- self.firework = Particle(randint(0, DISPLAY_WIDTH), DISPLAY_HEIGHT, True,
- self.colour)
- self.exploded = False
- self.particles = []
- self.min_max_particles = vector(100, 400)
-
- def update(self, win):
- if not self.exploded:
- self.firework.apply_force(gravity)
- self.firework.move()
- for tf in self.firework.trails:
- tf.show(win)
-
- self.show(win)
-
- if self.firework.vel.y >= 0:
- self.exploded = True
- self.explode()
- else:
- for particle in self.particles:
- particle.apply_force(
- vector(gravity.x + uniform(-1, 1) / 20, gravity.y / 2 + (randint(1, 8) / 100)))
- particle.move()
- for t in particle.trails:
- t.show(win)
- particle.show(win)
-
- def explode(self):
- amount = randint(self.min_max_particles.x, self.min_max_particles.y)
- for i in range(amount):
- self.particles.append(
- Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours))
- explosion_sound.play() # 播放爆炸音效
-
- def show(self, win):
- pygame.draw.circle(win, self.colour, (int(self.firework.pos.x), int(
- self.firework.pos.y)), self.firework.size)
-
-
- def remove(self):
- if self.exploded:
- for p in self.particles:
- if p.remove is True:
- self.particles.remove(p)
-
- if len(self.particles) == 0:
- return True
- else:
- return False
-
-
- class Particle:
-
- def __init__(self, x, y, firework, colour):
- self.firework = firework
- self.pos = vector(x, y)
- self.origin = vector(x, y)
- self.radius = 20
- self.remove = False
- self.explosion_radius = randint(5, 18)
- self.life = 0
- self.acc = vector(0, 0)
- self.trails = []
- self.prev_posx = [-10] * 10
- self.prev_posy = [-10] * 10
-
- if self.firework:
- self.vel = vector(0, -randint(17, 20))
- self.size = 5
- self.colour = colour
- for i in range(5):
- self.trails.append(Trail(i, self.size, True))
- else:
- self.vel = vector(uniform(-1, 1), uniform(-1, 1))
- self.vel.x *= randint(7, self.explosion_radius + 2)
- self.vel.y *= randint(7, self.explosion_radius + 2)
- self.size = randint(2, 4)
- self.colour = choice(colour)
- for i in range(5):
- self.trails.append(Trail(i, self.size, False))
-
- def apply_force(self, force):
- self.acc += force
-
- def move(self):
- if not self.firework:
- self.vel.x *= 0.8
- self.vel.y *= 0.8
-
- self.vel += self.acc
- self.pos += self.vel
- self.acc *= 0
-
- if self.life == 0 and not self.firework:
- distance = math.sqrt((self.pos.x - self.origin.x)
- ** 2 + (self.pos.y - self.origin.y) ** 2)
- if distance > self.explosion_radius:
- self.remove = True
-
- self.decay()
-
- self.trail_update()
-
- self.life += 1
-
- def show(self, win):
- pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)),
- self.size)
-
- def decay(self):
- if 50 > self.life > 10:
- ran = randint(0, 30)
- if ran == 0:
- self.remove = True
- elif self.life > 50:
- ran = randint(0, 5)
- if ran == 0:
- self.remove = True
-
- def trail_update(self):
- self.prev_posx.pop()
- self.prev_posx.insert(0, int(self.pos.x))
- self.prev_posy.pop()
- self.prev_posy.insert(0, int(self.pos.y))
-
- for n, t in enumerate(self.trails):
- if t.dynamic:
- t.get_pos(self.prev_posx[n + dynamic_offset],
- self.prev_posy[n + dynamic_offset])
- else:
- t.get_pos(self.prev_posx[n + static_offset],
- self.prev_posy[n + static_offset])
-
-
- class Trail:
-
- def __init__(self, n, size, dynamic):
- self.pos_in_line = n
- self.pos = vector(-10, -10)
- self.dynamic = dynamic
-
- if self.dynamic:
- self.colour = trail_colours[n]
- self.size = int(size - n / 2)
- else:
- self.colour = (255, 255, 200)
- self.size = size - 2
- if self.size < 0:
- self.size = 0
-
- def 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.display.set_caption('Happy Spring Festival in 2024 !')
- win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
- clock = pygame.time.Clock()
- fireworks = [Firework() for i in range(3)]
- running = True
- # 播放背景音乐
- pygame.mixer.music.play(-1) # -1 表示无限循环播放
- # 渲染文本并居中
- font = pygame.font.SysFont(None, 100) # 使用系统默认字体
- text = font.render('Happy Spring Festival in 2024 !', True, (255, 0, 0)) # 将文本颜色改为红色
-
- # 获取文本对象的宽度和高度
- text_width, text_height = text.get_size()
-
- # 计算文本对象的左上角坐标,使其位于页面正中
- text_x = (DISPLAY_WIDTH - text_width) // 2
- text_y = 20 # 调整文本的Y坐标使其位于页面顶部
-
- # 将文本对象渲染到窗口
- win.blit(text, (text_x, text_y))
-
- while running:
- clock.tick(60)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
-
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_1:
- fireworks.append(Firework())
- if event.key == pygame.K_2:
- for i in range(10):
- fireworks.append(Firework())
-
- win.fill((20, 20, 30))
-
- if randint(0, 20) == 1:
- fireworks.append(Firework())
-
- # 绘制文本
- win.blit(text, (text_x, text_y))
-
- update(win, fireworks)
-
- pygame.display.flip()
-
- pygame.quit()
- quit()
-
-
- if __name__ == "__main__":
- main()
贺新春—烟花效果展示
祝编友们2024新春快乐!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。