首页 培训资讯 教育动态 高考绽放 环球名师 文体艺术 职业技能 语言培训 学习帮助 推荐

python 实现漂亮的烟花,樱花,玫瑰花

2025-01-06

显着无聊聚集了下用python绘制烟花,绘制樱花,烟花+樱花,飘零雪花,玫瑰花的python绘制代码,可以来练手什么的哦。

同时python真现八音符,坦克大战,贪吃蛇,FlappyBird等都可以参考哦=> code

一、python真现烟花

成效图:

代码如下 :

import tkinter as tk from PIL import Image, ImageTk from time import time, sleep from random import choice, uniform, randint from math import sin, cos, radians # 模拟重力 GRAxITY = 0.05 # 颜涩选项(随机大概按顺序) colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue'] ''' particles 类 粒子正在地面随机生成随机,变为一个圈、下坠、消失 属性: - id: 粒子的id - V, y: 粒子的坐标 - ZZZV, ZZZy: 正在坐标的厘革速度 - total: 总数 - age: 粒子存正在的时长 - color: 颜涩 - cZZZ: 画布 - lifespan: 最高存正在时长 ''' class Particle: def __init__(self, cZZZ, idV, total, eVplosion_speed, V=0., y=0., ZZZV=0., ZZZy=0., size=2., color='red', lifespan=2, **kwargs): self.id = idV self.V = V self.y = y self.initial_speed = eVplosion_speed self.ZZZV = ZZZV self.ZZZy = ZZZy self.total = total self.age = 0 self.color = color self.cZZZ = cZZZ self.cid = self.cZZZ.create_oZZZal( V - size, y - size, V + size, y + size, fill=self.color) self.lifespan = lifespan def update(self, dt): self.age += dt # 粒子领域扩充 if self.aliZZZe() and self.eVpand(): moZZZe_V = cos(radians(self.id * 360 / self.total)) * self.initial_speed moZZZe_y = sin(radians(self.id * 360 / self.total)) * self.initial_speed self.cZZZ.moZZZe(self.cid, moZZZe_V, moZZZe_y) self.ZZZV = moZZZe_V / (float(dt) * 1000) # 以自由落体坠落 elif self.aliZZZe(): moZZZe_V = cos(radians(self.id * 360 / self.total)) # we technically don't need to update V, y because moZZZe will do the job self.cZZZ.moZZZe(self.cid, self.ZZZV + moZZZe_V, self.ZZZy + GRAxITY * dt) self.ZZZy += GRAxITY * dt # 移除赶过最高时长的粒子 elif self.cid is not None: cZZZ.delete(self.cid) self.cid = None # 扩充的光阳 def eVpand (self): return self.age <= 1.2 # 粒子能否正在最高存正在时长内 def aliZZZe(self): return self.age <= self.lifespan ''' 循环挪用保持不竭 ''' def simulate(cZZZ): t = time() eVplode_points = [] wait_time = randint(10, 100) numb_eVplode = randint(6, 10) # 创立一个所有粒子同时扩充的二维列表 for point in range(numb_eVplode): objects = [] V_cordi = randint(50, 550) y_cordi = randint(50, 150) speed = uniform(0.5, 1.5) size = uniform(0.5, 3) color = choice(colors) eVplosion_speed = uniform(0.2, 1) total_particles = randint(10, 50) for i in range(1, total_particles): r = Particle(cZZZ, idV=i, total=total_particles, eVplosion_speed=eVplosion_speed, V=V_cordi, y=y_cordi, ZZZV=speed, ZZZy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75)) objects.append(r) eVplode_points.append(objects) total_time = .0 # 1.8s内接续扩充 while total_time < 1.8: sleep(0.01) tnew = time() t, dt = tnew, tnew - t for point in eVplode_points: for item in point: item.update(dt) cZZZ.update() total_time += dt # 循环挪用 root.after(wait_time, simulate, cZZZ) def close(*ignore): """退出步调、封锁窗口""" global root root.quit() if __name__ == '__main__': root = tk.Tk() cZZZ = tk.CanZZZas(root, height=400, width=600) # 选一个都雅的布景会让成效更惊燕! image = Image.open("./image.jpg")//那里原人选择好布景图片哦 photo = ImageTk.PhotoImage(image) cZZZ.create_image(0, 0, image=photo, anchor='nw') cZZZ.pack() root.protocol("WM_DELETE_WINDOW", close) root.after(100, simulate, cZZZ) root.mainloop() 二、动态樱花

成效如下:

代码如下:

import turtle as T import random import time # 画樱花的躯干(60,t) def Tree(branch, t): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: t.color('snow') # 皂 else: t.color('lightcoral') # 淡珊瑚涩 t.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: t.color('snow') else: t.color('lightcoral') # 淡珊瑚涩 t.pensize(branch / 2) else: t.color('sienna') # 赭(zhě)涩 t.pensize(branch / 10) # 6 t.forward(branch) a = 1.5 * random.random() t.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b, t) t.left(40 * a) Tree(branch - 10 * b, t) t.right(20 * a) t.up() t.backward(branch) t.down() # 掉落的花瓣 def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() t.color('lightcoral') # 淡珊瑚涩 t.circle(1) t.up() t.backward(a) t.right(90) t.backward(b) # 绘图区域 t = T.Turtle() # 画布大小 w = T.Screen() t.hideturtle() # 隐藏画笔 t.getscreen().tracer(5, 0) w.screensize(bg='wheat') # wheat小麦 t.left(90) t.up() t.backward(150) t.down() t.color('sienna') # 画樱花的躯干 Tree(60, t) # 掉落的花瓣 Petal(100, t) w.eVitonclick() 三、烟花共同樱花齐放

import turtle import random import time from PIL import Image, ImageTk # from time import sleep import tkinter as tk from time import sleep from random import choice, uniform, randint from math import sin, cos, radians # 模拟重力 GRAxITY = 0.05 # 颜涩选项(随机大概按顺序) colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue'] # 画樱花的躯干(60,t) def Tree(branch, t): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: t.color('snow') # 皂 else: t.color('lightcoral') # 淡珊瑚涩 t.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: t.color('snow') else: t.color('lightcoral') # 淡珊瑚涩 t.pensize(branch / 2) else: t.color('sienna') # 赭(zhě)涩 t.pensize(branch / 10) # 6 t.forward(branch) a = 1.5 * random.random() t.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b, t) t.left(40 * a) Tree(branch - 10 * b, t) t.right(20 * a) t.up() t.backward(branch) t.down() # 掉落的花瓣 def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() t.color('lightcoral') # 淡珊瑚涩 t.circle(1) t.up() t.backward(a) t.right(90) t.backward(b) class Particle: def __init__(self, cZZZ, idV, total, eVplosion_speed, V=0., y=0., ZZZV=0., ZZZy=0., size=2., color='red', lifespan=2, **kwargs): self.id = idV self.V = V self.y = y self.initial_speed = eVplosion_speed self.ZZZV = ZZZV self.ZZZy = ZZZy self.total = total self.age = 0 self.color = color self.cZZZ = cZZZ self.cid = self.cZZZ.create_oZZZal( V - size, y - size, V + size, y + size, fill=self.color) self.lifespan = lifespan def update(self, dt): self.age += dt # 粒子领域扩充 if self.aliZZZe() and self.eVpand(): moZZZe_V = cos(radians(self.id * 360 / self.total)) * self.initial_speed moZZZe_y = sin(radians(self.id * 360 / self.total)) * self.initial_speed self.cZZZ.moZZZe(self.cid, moZZZe_V, moZZZe_y) self.ZZZV = moZZZe_V / (float(dt) * 1000) # 以自由落体坠落 elif self.aliZZZe(): moZZZe_V = cos(radians(self.id * 360 / self.total)) # we technically don't need to update V, y because moZZZe will do the job self.cZZZ.moZZZe(self.cid, self.ZZZV + moZZZe_V, self.ZZZy + GRAxITY * dt) self.ZZZy += GRAxITY * dt # 移除赶过最高时长的粒子 elif self.cid is not None: cZZZ.delete(self.cid) self.cid = None # 扩充的光阳 def eVpand (self): return self.age <= 1.2 # 粒子能否正在最高存正在时长内 def aliZZZe(self): return self.age <= self.lifespan ''' 循环挪用保持不竭 ''' def simulate(cZZZ): # time.sleep(0.0005) t1 = time.time() eVplode_points = [] wait_time = randint(10, 100) numb_eVplode = randint(6, 10) # 创立一个所有粒子同时扩充的二维列表 for point in range(numb_eVplode): objects = [] V_cordi = randint(50, 550) y_cordi = randint(50, 150) speed = uniform(0.5, 1.5) size = uniform(0.5, 3) color = choice(colors) eVplosion_speed = uniform(0.2, 1) total_particles = randint(10, 50) for i in range(1, total_particles): r = Particle(cZZZ, idV=i, total=total_particles, eVplosion_speed=eVplosion_speed, V=V_cordi, y=y_cordi, ZZZV=speed, ZZZy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75)) objects.append(r) eVplode_points.append(objects) total_time = .0 # 1.8s内接续扩充 while total_time < 1.8: sleep(0.01) tnew = time.time() t1, dt = tnew, tnew - t1 for point in eVplode_points: for item in point: item.update(dt) cZZZ.update() total_time += dt # 循环挪用 root.after(wait_time, simulate, cZZZ) def close(*ignore): """退出步调、封锁窗口""" global root root.quit() if __name__ == '__main__': root = tk.Tk() cZZZ = tk.CanZZZas(root, height=520, width=750) cZZZ.pack() image = Image.open("./image.jpg") photo = ImageTk.PhotoImage(image) w = turtle.TurtleScreen(cZZZ) t = turtle.RawTurtle(w) # 选一个都雅的布景会让成效更惊燕! cZZZ.create_image(0, 0, image=photo) # root.mainloop() t.hideturtle() # 隐藏画笔 t.getscreen().tracer(5, 0) w.screensize(bg='black') # wheat小麦 t.left(90) t.up() t.backward(150) t.down() t.color('sienna') # 画樱花的躯干 Tree(60, t) # 掉落的花瓣 Petal(200, t) root.protocol("WM_DELETE_WINDOW", close) root.after(100, simulate, cZZZ) root.mainloop() 四、飘过的雪花 

from turtle import * from random import * from math import * def tree(n,l): pd()#下笔 #阳映成效 t = cos(radians(heading()+45))/8+0.25 pencolor(t,t,t) pensize(n/3) forward(l)#画树枝 if n>0: b = random()*15+10 #左分收偏转角度 c = random()*15+10 #右分收偏转角度 d = l*(random()*0.25+0.7) #下一个分收的长度 #左转一定角度,画左分收 right(b) tree(n-1,d) #右转一定角度,画右分收 left(b+c) tree(n-1,d) #转回来离去 right(c) else: #画叶子 right(90) n=cos(radians(heading()-45))/4+0.5 pencolor(n,n*0.8,n*0.8) circle(3) left(90) #添加0.3倍的飘落叶子 if(random()>0.7): pu() #飘落 t = heading() an = -40 +random()*40 setheading(an) dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2) forward(dis) setheading(t) #画叶子 pd() right(90) n = cos(radians(heading()-45))/4+0.5 pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4) circle(2) left(90) pu() #返回 t=heading() setheading(an) backward(dis) setheading(t) pu() backward(l)#退回 bgcolor(0.5,0.5,0.5)#布景涩 ht()#隐藏turtle speed(0)#速度 1-10渐进,0 最快 tracer(0,0) pu()#抬笔 backward(100) left(90)#右转90度 pu()#抬笔 backward(300)#退却后退300 tree(12,100)#递归7层 done() 五、给你一朵玫瑰花

成效图:

代码:

from turtle import * import time setup(1000,800,0,0) speed(0) penup() seth(90) fd(340) seth(0) pendown() speed(5) begin_fill() fillcolor('red') circle(50,30) for i in range(10): fd(1) left(10) circle(40,40) for i in range(6): fd(1) left(3) circle(80,40) for i in range(20): fd(0.5) left(5) circle(80,45) for i in range(10): fd(2) left(1) circle(80,25) for i in range(20): fd(1) left(4) circle(50,50) time.sleep(0.1) circle(120,55) speed(0) seth(-90) fd(70) right(150) fd(20) left(140) circle(140,90) left(30) circle(160,100) left(130) fd(25) penup() right(150) circle(40,80) pendown() left(115) fd(60) penup() left(180) fd(60) pendown() end_fill() right(120) circle(-50,50) circle(-20,90) speed(1) fd(75) speed(0) circle(90,110) penup() left(162) fd(185) left(170) pendown() circle(200,10) circle(100,40) circle(-52,115) left(20) circle(100,20) circle(300,20) speed(1) fd(250) penup() speed(0) left(180) fd(250) circle(-300,7) right(80) circle(200,5) pendown() left(60) begin_fill() fillcolor('green') circle(-80,100) right(90) fd(10) left(20) circle(-63,127) end_fill() penup() left(50) fd(20) left(180) pendown() circle(200,25) penup() right(150) fd(180) right(40) pendown() begin_fill() fillcolor('green') circle(-100,80) right(150) fd(10) left(60) circle(-80,98) end_fill() penup() left(60) fd(13) left(180) pendown() speed(1) circle(-200,23) eVitonclick() 六、HTML+JS 网页樱花

<!doctype html> <html> <head> <meta ht-equiZZZ="Pragma" content="no-cache" /> <meta ht-equiZZZ="Cache-Control" content="no-cache" /> <meta ht-equiZZZ="EVpires" content="0" /> <meta ht-equiZZZ="Content-Type" content="teVt/html;charset=utf-8" /> <meta content="width=deZZZice-width,initial-scale=1.0,user-scalable=no" /> <style type="teVt/css"> canZZZas{ position: absolute; left: 0; top: 0; } </style> </head> <body bgcolor="#000000"> <canZZZas></canZZZas> <canZZZas></canZZZas> <script> //两个canZZZas ZZZar tree = document.getElementById("tree"); tree.width = window.innerWidth; tree.height = window.innerHeight ; ZZZar tCVt = tree.getConteVt("2d"); ZZZar flower = document.getElementById("flower"); flower.width = window.innerWidth; flower.height = window.innerHeight ; ZZZar cVt = flower.getConteVt("2d"); ZZZar flowerList = [];//樱花列表 ZZZar rootTop = 450 ;//树末点 ZZZar flowerColor = "rgba(255,192,203,.3)" ;//花涩 ZZZar flowerColorDeep = "rgba(241,158,194,.5)" ;//花涩深 ZZZar treeColor2 = "rgba(255,192,203,.5)" ;//树枝颜涩 ZZZar treeColor = "#FFF" ;//树干颜涩 ZZZar fallList = [];//飘落樱花列表 ZZZar g = 0.01 ;//重力加快度 ZZZar gWind = 0.005;//风力加快度 ZZZar limitSpeedY = 1;//速度上限 ZZZar limitSpeedX = 1 ;//速度上限 cVt.shadowColor= "#FFF" ; cVt.shadowBlur = 10 ; function drawTree(V,y,deg,step,type){ ZZZar deg1 = step%2 == 0 ? 0.1 : -0.1 ; ZZZar V1 = V + Math.cos(deg+deg1) * (step+4) * 0.8 ;//以步长来判断枝干长度 V轴偏移大一些 ZZZar y1 = y + Math.sin(deg+deg1) * (step-1) * 0.8 ;//以步长来判断枝干长度 Y轴压缩一些 tCVt.beginPath(); tCVt.lineWidth = step/3; tCVt.moZZZeTo(V,y); tCVt.lineTo(V1,y1); tCVt.strokeStyle = (step > 5 ) ? treeColor : treeColor2 ;//细纸条都换成花的颜涩 tCVt.stroke(); if(step > 20){//树干订交的位置有间隙,以一个圆填充 tCVt.fillStyle = treeColor ; tCVt.arc(V,y,step/6,0,Math.PI*2); tCVt.fill(); } if(step < 3 || (step < 23 && Math.random() > 0.1)){ //终梢位置 画花瓣 ZZZar color = [flowerColorDeep,flowerColor,flowerColor][Math.round(Math.random()+0.2)] ; ZZZar r = 2+Math.random()*2 tCVt.fillStyle = color ; tCVt.arc(V1+Math.random()*3,y1+Math.random()*3,r,0,Math.PI) tCVt.fill(); flowerList.push({V:V,y:y,sV:(Math.random()-0.5),sy:0,color:color,r:r,deg:deg});//保存下画樱花的位置 } step -- ; if(step > 0){ drawTree(V1,y1,deg,step,type); if(step%3 == 1 && step > 0 && step < 30) drawTree(V1,y1,deg+0.2 + 0.3 * Math.random(),Math.round(step/1.13));//左分叉 if(step%3 == 0 && step > 0 && step < 30) drawTree(V1,y1,deg-0.2 - 0.3 * Math.random(),Math.round(step/1.13));//右分叉 } } drawTree(tree.width/2,rootTop,-Math.PI/2,30,1);//执止 ZZZar len = flowerList.length ; function step(){ if(Math.random() > 0.3) fallList.push(flowerList[Math.floor(Math.random()*len)]);//随机与出一个,花瓣复制到飘落花瓣的列表中 cVt.clearRect(0,0,tree.width,tree.height); for(ZZZar i = 0 ;i < fallList.length ; i ++){ if(fallList[i].sy < limitSpeedY) fallList[i].sy += g ; fallList[i].sV += gWind ; fallList[i].V += fallList[i].sV ; fallList[i].y += fallList[i].sy ; if(fallList[i].y > rootTop+30){//飘到树根+30的花瓣移除 fallList.splice(i,1); i -- ; continue ; } cVt.beginPath(); cVt.fillStyle = fallList[i].color ; fallList[i].deg += fallList[i].sV*0.05 ;//跟速度相关的旋转花瓣 cVt.arc(fallList[i].V,fallList[i].y,fallList[i].r,fallList[i].deg,fallList[i].deg+Math.PI*1.3); cVt.fill(); } requestAnimationFrame(step); } requestAnimationFrame(step); </script> </body> </html>

 

热门文章

友情链接: 永康物流网 义乌物流网 本网站域名出售 手机靓号-号码网 抖音视频制作 AI工具 旅游大全 影视动漫 算命星座 宠物之家 两性关系 学习教育