Python Pygame制作雪夜烟花景
作者:炒鸡辣鸡123 发布时间:2023-10-21 08:57:07
标签:Python,Pygame,雪夜,烟花
运行截图
运行效果:
什么?你说你看不清烟花?那我换一种颜色,请点开看。
实现过程
准备工作
使用语言和框架:python、pygame。
安装pygame:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn pygame
你需要知道的基础知识
首先,pygame渲染是同步的,所以同屏渲染的点过多之后,就会造成卡顿的情况。
其次,pygame的代码逻辑是,周期性渲染一系列的屏,从而产生连续的动画。
你需要掌握的框架基础知识:
初始化过程
import pygame
pygame.init()
pygame.mixer.init()
pygame.font.init()
获取字体
myfont = pygame.font.SysFont('simHei', 30)
textsurface = myfont.render(a[i], False, random_color(150, 255))
screen.blit(textsurface, (80, 30))
画圈
pygame.draw.circle(screen, (snow_list[i][4], snow_list[i][5], snow_list[i][6]), snow_list[i][:2],
snow_list[i][3] - 3)
加载背景音乐
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("新年快乐")
bg = pygame.image.load(bg_img)
pygame.mixer.music.load('D:\\CloudMusic\\小时姑娘 - 霞光-《精灵世纪》片尾曲.mp3')
核心代码
基础架子
首先,需要实现一个基础的事件循环的架子,如下:
def main():
global show_n
global fk_list
bg_size = (WIN_W, WIN_H)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("新年快乐")
pygame.mixer.music.load('D:\\CloudMusic\\小时姑娘 - 霞光-《精灵世纪》片尾曲.mp3')
font_values = ['新年快乐']
grand_has = set()
clock = pygame.time.Clock()
while True:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill((0, 0, 0))
... ...
pygame.display.update()
time_passed = clock.tick(50)
if __name__ == '__main__':
main()
下雪的过程
现在,需要实现下雪的过程,首先,考虑定义定义一堆初始化的下雪点
def init_xue(bg_size):
snow_list = []
for i in range(200):
x_site = random.randrange(0, bg_size[0]) # 雪花圆心位置
y_site = random.randrange(0, bg_size[1]) # 雪花圆心位置
X_shift = random.randint(-1, 1) # x 轴偏移量
radius = random.randint(4, 6) # 半径和 y 周下降量
xxxxx = random_color(150, 255)
snow_list.append([x_site, y_site, X_shift, radius, 255, 255, 255])
return snow_list
然后实现渲染雪的过程
def draw_xue(snow_list: [], screen, bg_size: [], grand_has: set, grand_list: []):
# 雪花列表循环
# todo 空中的雪
for i in range(len(snow_list)):
# 绘制雪花,颜色、位置、大小
pygame.draw.circle(screen, (snow_list[i][4], snow_list[i][5], snow_list[i][6]), snow_list[i][:2],
snow_list[i][3] - 3)
# 移动雪花位置(下一次循环起效)
snow_list[i][0] += snow_list[i][2]
snow_list[i][1] += snow_list[i][3]
# 如果雪花落出屏幕,重设位置
if snow_list[i][1] > bg_size[1]:
# tmp = []
snow_list[i][1] = random.randrange(-50, -10)
snow_list[i][0] = random.randrange(0, bg_size[0])
x = snow_list[i][0]
y = bg_size[1]
while (grand_has.__contains__(x * 10000 + y)):
y = y - snow_list[i][3]
grand_has.add(x * 10000 + y)
grand_list.append(
[x, y, snow_list[i][2], snow_list[i][3], snow_list[i][4], snow_list[i][5],
snow_list[i][6]])
集成到上面的架子中,效果如下:
不过目前的下雪没有质感,可以考虑在底部堆一些雪,只需要在雪落到地上做特判即可。
雪落到地上堆起来的过程
在前面的下雪过程的代码中,我们维护了一个Grand_list的数组,目的就是维护堆雪的效果
min_height = 100000
# todo 地上的积雪
for i in range(len(grand_list)):
if grand_list[i][0] < 375:
min_height = min(min_height, grand_list[i][1])
然后进入维护程序:
draw_xue(snow_list, screen, bg_size, grand_has, grand_list)
最后再将雪画出来
for i in range(len(grand_list)):
pygame.draw.circle(screen, (grand_list[i][4], grand_list[i][5], grand_list[i][6]), grand_list[i][:2],
grand_list[i][3] - 3)
效果图如上。
实现烟花的过程
首先定义出烟花类:
class Fireworks():
is_show = False
x, y = 0, 0
vy = 0
p_list = []
color = [0, 0, 0]
v = 0
def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10):
self.x = x
self.y = y
self.vy = vy
self.color = color
self.v = v
for i in range(n):
self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1 / 3)])
def run(self):
global show_n
for p in self.p_list:
p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2]
p[2] = p[2] * 0.97
if p[2] < 1.2:
self.color[0] *= 0.9999
self.color[1] *= 0.9999
self.color[2] *= 0.9999
if max(self.color) < 10 or self.y > WIN_H + p[1]:
show_n -= 1
self.is_show = False
break
self.vy += 10 * t1
self.y += self.vy * t1
然后,我们需要画出烟花释放前上升的过程点,这部分与下雪的初始化差不多。
def init_yanhua(bg_size):
yanhua_list = []
for i in range(5):
x_site = random.randrange(0, WIN_W) # 雪花圆心位置
y_site = WIN_H # 雪花圆心位置
X_shift = 0 # x 轴偏移量
radius = random.randint(6, 10) # 半径和 y 周上升降量
xxxxx = random_color(150, 255)
red = xxxxx[0]
green = xxxxx[1]
blue = xxxxx[2]
yanhua_list.append([x_site, y_site, X_shift, radius, red, green, blue])
return yanhua_list
然后是画上升过程
def draw_yanhua(yanhua_list: [], screen, bg_size: []):
global fk_list
for i in range(len(yanhua_list)):
# 绘制雪花,颜色、位置、大小
pygame.draw.circle(screen, (yanhua_list[i][4], yanhua_list[i][5], yanhua_list[i][6]), yanhua_list[i][:2],
yanhua_list[i][3] - 3)
yanhua_list[i][0] += yanhua_list[i][2]
yanhua_list[i][1] -= yanhua_list[i][3]
if yanhua_list[i][1] <= 0:
# tmp = []
yanhua_list[i][1] = WIN_H
yanhua_list[i][0] = random.randrange(0, bg_size[0])
if yanhua_list[i][1] <= random.randint(200, 400):
# todo 放烟花
fk = Fireworks(yanhua_list[i][0], yanhua_list[i][1], -20, n=300, color=red_random(1, 150), v=10)
fk_list.append(fk)
yanhua_list[i][1] = WIN_H
yanhua_list[i][0] = random.randrange(0, bg_size[0])
效果图如下:
圈出来的就是上升过程的烟花。
最后就是绽放部分,其实在上升过程的代码中有维护,如果超过某个随机高度,就会生成一个烟花,只是没有渲染,现在我们把渲染加上。
for fk in fk_list:
fk.run()
for p in fk.p_list:
x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
if random.random() < 0.055:
screen.set_at((int(x), int(y)), (255, 255, 255))
else:
screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))
tmp = []
for fk in fk_list:
for p in fk.p_list:
x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
if y < WIN_H - 1000:
tmp.append(fk)
break
fk_list = tmp
最终的运行效果就如最顶上的效果一样。
完整代码
将上述过程进行组合,结果如下,感兴趣的朋友可以按自己的需求进行优化。
import pygame
import random
import math
pygame.init()
pygame.mixer.init()
pygame.font.init()
WIN_W = 2200
WIN_H = 1300
t1 = 0.18 # 时间流速
show_n = 0
show_frequency = 0.0015 # 烟花绽放频率,数值越大频率越高
color_list = [
[255, 0, 0]
]
yanhua_map = {}
fk_list = []
class Fireworks():
is_show = False
x, y = 0, 0
vy = 0
p_list = []
color = [0, 0, 0]
v = 0
def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10):
self.x = x
self.y = y
self.vy = vy
self.color = color
self.v = v
for i in range(n):
self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1 / 3)])
def run(self):
global show_n
for p in self.p_list:
p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2]
p[2] = p[2] * 0.97
if p[2] < 1.2:
self.color[0] *= 0.9999
self.color[1] *= 0.9999
self.color[2] *= 0.9999
if max(self.color) < 10 or self.y > WIN_H + p[1]:
show_n -= 1
self.is_show = False
break
self.vy += 10 * t1
self.y += self.vy * t1
def random_color(l, r):
return [random.randint(l, r), random.randint(l, r), random.randint(l, r)]
def red_random(l, r):
return [255, random.randint(l, r), random.randint(l, r)]
def init_yanhua(bg_size):
yanhua_list = []
for i in range(5):
x_site = random.randrange(0, WIN_W) # 雪花圆心位置
y_site = WIN_H # 雪花圆心位置
X_shift = 0 # x 轴偏移量
radius = random.randint(6, 10) # 半径和 y 周上升降量
xxxxx = random_color(150, 255)
red = xxxxx[0]
green = xxxxx[1]
blue = xxxxx[2]
yanhua_list.append([x_site, y_site, X_shift, radius, red, green, blue])
return yanhua_list
def init_xue(bg_size):
snow_list = []
for i in range(200):
x_site = random.randrange(0, bg_size[0]) # 雪花圆心位置
y_site = random.randrange(0, bg_size[1]) # 雪花圆心位置
X_shift = random.randint(-1, 1) # x 轴偏移量
radius = random.randint(4, 6) # 半径和 y 周下降量
xxxxx = random_color(150, 255)
# red = xxxxx[0]
# green = xxxxx[1]
# blue = xxxxx[2]
snow_list.append([x_site, y_site, X_shift, radius, 255, 255, 255])
return snow_list
def draw_xue(snow_list: [], screen, bg_size: [], grand_has: set, grand_list: []):
# 雪花列表循环
# todo 空中的雪
for i in range(len(snow_list)):
# 绘制雪花,颜色、位置、大小
pygame.draw.circle(screen, (snow_list[i][4], snow_list[i][5], snow_list[i][6]), snow_list[i][:2],
snow_list[i][3] - 3)
# 移动雪花位置(下一次循环起效)
snow_list[i][0] += snow_list[i][2]
snow_list[i][1] += snow_list[i][3]
# 如果雪花落出屏幕,重设位置
if snow_list[i][1] > bg_size[1]:
# tmp = []
snow_list[i][1] = random.randrange(-50, -10)
snow_list[i][0] = random.randrange(0, bg_size[0])
x = snow_list[i][0]
y = bg_size[1]
while (grand_has.__contains__(x * 10000 + y)):
y = y - snow_list[i][3]
grand_has.add(x * 10000 + y)
grand_list.append(
[x, y, snow_list[i][2], snow_list[i][3], snow_list[i][4], snow_list[i][5],
snow_list[i][6]])
def draw_yanhua(yanhua_list: [], screen, bg_size: []):
global fk_list
for i in range(len(yanhua_list)):
# 绘制雪花,颜色、位置、大小
pygame.draw.circle(screen, (yanhua_list[i][4], yanhua_list[i][5], yanhua_list[i][6]), yanhua_list[i][:2],
yanhua_list[i][3] - 3)
# 移动雪花位置(下一次循环起效)
yanhua_list[i][0] += yanhua_list[i][2]
yanhua_list[i][1] -= yanhua_list[i][3]
# 如果雪花落出屏幕,重设位置
if yanhua_list[i][1] <= 0:
# tmp = []
yanhua_list[i][1] = WIN_H
yanhua_list[i][0] = random.randrange(0, bg_size[0])
if yanhua_list[i][1] <= random.randint(200, 400):
# todo 放烟花
fk = Fireworks(yanhua_list[i][0], yanhua_list[i][1], -20, n=300, color=red_random(1, 150), v=10)
fk_list.append(fk)
yanhua_list[i][1] = WIN_H
yanhua_list[i][0] = random.randrange(0, bg_size[0])
def show_shi(a: list, n, screen):
i = 2 * n - 1
j = 2 * n
if i >= len(a):
i = len(a) - 2
j = len(a) - 1
if i >= 0:
myfont = pygame.font.SysFont('simHei', 30)
textsurface = myfont.render(a[i], False, random_color(150, 255))
screen.blit(textsurface, (WIN_W / 2, 30))
if j >= 0:
myfont = pygame.font.SysFont('simHei', 100)
textsurface = myfont.render(a[j], False, red_random(1, 1))
screen.blit(textsurface, (WIN_W / 2 - 200, 50))
def main():
global show_n
global fk_list
bg_size = (WIN_W, WIN_H)
screen = pygame.display.set_mode(bg_size)
# bg_img = "./1.png"
pygame.display.set_caption("新年快乐")
# bg = pygame.image.load(bg_img)
pygame.mixer.music.load('D:\\CloudMusic\\小时姑娘 - 霞光-《精灵世纪》片尾曲.mp3')
grand_list = []
font_values = ['新年快乐']
grand_has = set()
clock = pygame.time.Clock()
yanhua_list = init_yanhua(bg_size)
snow_list = init_xue(bg_size)
# 游戏主循环
while True:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill((0, 0, 0))
draw_yanhua(yanhua_list, screen, bg_size)
if len(fk_list) != 0:
print(len(fk_list))
# # 放烟花
show_shi(font_values, 0, screen)
for fk in fk_list:
fk.run()
for p in fk.p_list:
x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
if random.random() < 0.055:
screen.set_at((int(x), int(y)), (255, 255, 255))
else:
screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))
tmp = []
for fk in fk_list:
for p in fk.p_list:
x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
if y < WIN_H - 1000:
tmp.append(fk)
break
fk_list = tmp
min_height = 100000
# todo 地上的积雪
for i in range(len(grand_list)):
if grand_list[i][0] < 375:
min_height = min(min_height, grand_list[i][1])
draw_xue(snow_list, screen, bg_size, grand_has, grand_list)
for i in range(len(grand_list)):
pygame.draw.circle(screen, (grand_list[i][4], grand_list[i][5], grand_list[i][6]), grand_list[i][:2],
grand_list[i][3] - 3)
pygame.display.update()
time_passed = clock.tick(50)
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/xielinrui123/article/details/122701390
0
投稿
猜你喜欢
- 网页开发人员常常希望能够了解并掌握多种语言,结果是,学习一门语言的所有内容是棘手的,但是却很容易发现你并没有完全利用那些比较特殊却很有用的标
- HTML5之中一个很酷的新特性就是WebSockets,它可以让我们无需AJAX请求即可与服务器端对话。今天彬Go将让大家通过Php环境的服
- 一、两层结构的ASP应用有何缺点 在Browser/Server 应用程序开发领域,微软公司的IIS/ASP以其强大的功能,良好的扩展能力,
- ASP开发中有用的function集合,挺有用处的!希望大家保留!<% '**
- IE独有属性AlphaImageLoader用于修正7.0以下版本中显示PNG图片的半透明效果。这个滤镜的问题在于浏览器加载图片时它会终止内
- 首先,了解下原理。1,提供文本框进行查询内容的输入2,将查询信息提交页面程序处理3,程序页主要作用:接受查询信息,根据此信息调用特定的SQL
- 什么是 Python 中的 Lambda 函数今天我们来学习 Python 中的 lambda 函数,并探讨使用它的优点和局限性Let
- Microsoft SQL Server 2000的会话上下文信息使应用程序得以设置二进制值,以便在同一会话或连接上运行的多个批处理、存储过
- 这篇论坛文章(赛迪网技术社区)详细的介绍了在MySQL中使用GRANT语句增添新用户的具体步骤,更多内容请参考下文…&
- Request.ServerVariables("Url") 返回服务器地址Request.ServerVariable
- MySQL Group By用法我们现在回到函数上。记得我们用 SUM 这个指令来算出所有的 Sales (营业额)吧!如果我们的需求变成是
- 是在CSDN论坛看到的一个问题,平常我也没有注意,或者说没有这样用吧。看代码 <body id="ww
- 前言当我们编写任何程序时,都会遇到一些错误,会让我们有挫败感,所以我有一个解决方案给你。 今天在这篇文章中,我们将讨论错误类型error:
- 编写思路:把本地文件在客户端通过base64编码以后发送目的地.测试过程中,上传文件过大,导致超时不成功,后来经过改善.把编码分
- JS获取网页中HTML元素的几种方法分析:getElementById getElementsByName getElementsByTag
- 数据库操作类的优点优点可以说是非常多了,常见的优点就是便于维护、复用、高效、安全、易扩展。例如PDO支持的数据库类型是非常多的,与mysql
- #mkdir /mysqldata2、创建/usr/sbin/bakmysql文件#nano /usr/sbin/bakmysql输入:#!
- 杭州最美的季节里,淘宝无障碍访问改善小组有幸邀请到盲人在线站长——争渡读屏团队成员——杨永全同学和我们一起面对面交流网站无障碍访问方面的问题
- 第一次写技术博客,有不尽如人意的地方,还请见谅和指正。为什么想整理这方面的类容,我觉得就像油画家要了解他的颜料和画布、雕塑家要了解他的石材一
- 对方用<script language="javascript"> var location="&