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

时间:2025-04-13 07:57:40

显着无聊收集了下用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

# 模拟重力

GRAVITY = 0.05

# 颜色选项(随机或者按顺序)

colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']

'''

particles 类

粒子在空中随机生成随机,变成一个圈、下坠、消失

属性:

- id: 粒子的id

- x, y: 粒子的坐标

- vx, vy: 在坐标的变化速度

- total: 总数

- age: 粒子存在的时长

- color: 颜色

- cv: 画布

- lifespan: 最高存在时长

'''

class Particle:

def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx=0., vy=0., size=2., color='red', lifespan=2,

**kwargs):

= idx

= x

= y

self.initial_speed = explosion_speed

= vx

= vy

= total

= 0

= color

= cv

= .create_oval(

x - size, y - size, x + size,

y + size, fill=)

= lifespan

def update(self, dt):

+= dt

# 粒子范围扩大

if () and ():

move_x = cos(radians( * 360 / )) * self.initial_speed

move_y = sin(radians( * 360 / )) * self.initial_speed

(, move_x, move_y)

= move_x / (float(dt) * 1000)

# 以*落体坠落

elif ():

move_x = cos(radians( * 360 / ))

# we technically don't need to update x, y because move will do the job

(, + move_x, + GRAVITY * dt)

+= GRAVITY * dt

# 移除超过最高时长的粒子

elif is not None:

()

= None

# 扩大的时间

def expand (self):

return <= 1.2

# 粒子是否在最高存在时长内

def alive(self):

return <=

'''

循环调用保持不停

'''

def simulate(cv):

t = time()

explode_points = []

wait_time = randint(10, 100)

numb_explode = randint(6, 10)

# 创建一个所有粒子同时扩大的二维列表

for point in range(numb_explode):

objects = []

x_cordi = randint(50, 550)

y_cordi = randint(50, 150)

speed = uniform(0.5, 1.5)

size = uniform(0.5, 3)

color = choice(colors)

explosion_speed = uniform(0.2, 1)

total_particles = randint(10, 50)

for i in range(1, total_particles):

r = Particle(cv, idx=i, total=total_particles, explosion_speed=explosion_speed, x=x_cordi, y=y_cordi,

vx=speed, vy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75))

(r)

explode_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 explode_points:

for item in point:

(dt)

()

total_time += dt

# 循环调用

(wait_time, simulate, cv)

def close(*ignore):

"""退出程序、关闭窗口"""

global root

()

if __name__ == '__main__':

root = ()

cv = (root, height=400, width=600)

# 选一个好看的背景会让效果更惊艳!

image = ("./")//这里自己选择好背景图片哦

photo = (image)

cv.create_image(0, 0, image=photo, anchor='nw')

()

("WM_DELETE_WINDOW", close)

(100, simulate, cv)

()

二、动态樱花

效果如下:

代码如下:

import turtle as T

import random

import time

# 画樱花的躯干(60,t)

def Tree(branch, t):

(0.0005)

if branch > 3:

if 8 <= branch <= 12:

if (0, 2) == 0:

('snow') # 白

else:

('lightcoral') # 淡珊瑚色

(branch / 3)

elif branch < 8:

if (0, 1) == 0:

('snow')

else:

('lightcoral') # 淡珊瑚色

(branch / 2)

else:

('sienna') # 赭(zhě)色

(branch / 10) # 6

(branch)

a = 1.5 * ()

(20 * a)

b = 1.5 * ()

Tree(branch - 10 * b, t)

(40 * a)

Tree(branch - 10 * b, t)

(20 * a)

()

(branch)

()

# 掉落的花瓣

def Petal(m, t):

for i in range(m):

a = 200 - 400 * ()

b = 10 - 20 * ()

()

(b)

(90)

(a)

()

('lightcoral') # 淡珊瑚色

(1)

()

(a)

(90)

(b)

# 绘图区域

t = ()

# 画布大小

w = ()

() # 隐藏画笔

().tracer(5, 0)

(bg='wheat') # wheat小麦

(90)

()

(150)

()

('sienna')

# 画樱花的躯干

Tree(60, t)

# 掉落的花瓣

Petal(100, t)

()

三、烟花配合樱花齐放

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

# 模拟重力

GRAVITY = 0.05

# 颜色选项(随机或者按顺序)

colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']

# 画樱花的躯干(60,t)

def Tree(branch, t):

(0.0005)

if branch > 3:

if 8 <= branch <= 12:

if (0, 2) == 0:

('snow') # 白

else:

('lightcoral') # 淡珊瑚色

(branch / 3)

elif branch < 8:

if (0, 1) == 0:

('snow')

else:

('lightcoral') # 淡珊瑚色

(branch / 2)

else:

('sienna') # 赭(zhě)色

(branch / 10) # 6

(branch)

a = 1.5 * ()

(20 * a)

b = 1.5 * ()

Tree(branch - 10 * b, t)

(40 * a)

Tree(branch - 10 * b, t)

(20 * a)

()

(branch)

()

# 掉落的花瓣

def Petal(m, t):

for i in range(m):

a = 200 - 400 * ()

b = 10 - 20 * ()

()

(b)

(90)

(a)

()

('lightcoral') # 淡珊瑚色

(1)

()

(a)

(90)

(b)

class Particle:

def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx=0., vy=0., size=2., color='red', lifespan=2,

**kwargs):

= idx

= x

= y

self.initial_speed = explosion_speed

= vx

= vy

= total

= 0

= color

= cv

= .create_oval(

x - size, y - size, x + size,

y + size, fill=)

= lifespan

def update(self, dt):

+= dt

# 粒子范围扩大

if () and ():

move_x = cos(radians( * 360 / )) * self.initial_speed

move_y = sin(radians( * 360 / )) * self.initial_speed

(, move_x, move_y)

= move_x / (float(dt) * 1000)

# 以*落体坠落

elif ():

move_x = cos(radians( * 360 / ))

# we technically don't need to update x, y because move will do the job

(, + move_x, + GRAVITY * dt)

+= GRAVITY * dt

# 移除超过最高时长的粒子

elif is not None:

()

= None

# 扩大的时间

def expand (self):

return <= 1.2

# 粒子是否在最高存在时长内

def alive(self):

return <=

'''

循环调用保持不停

'''

def simulate(cv):

# (0.0005)

t1 = ()

explode_points = []

wait_time = randint(10, 100)

numb_explode = randint(6, 10)

# 创建一个所有粒子同时扩大的二维列表

for point in range(numb_explode):

objects = []

x_cordi = randint(50, 550)

y_cordi = randint(50, 150)

speed = uniform(0.5, 1.5)

size = uniform(0.5, 3)

color = choice(colors)

explosion_speed = uniform(0.2, 1)

total_particles = randint(10, 50)

for i in range(1, total_particles):

r = Particle(cv, idx=i, total=total_particles, explosion_speed=explosion_speed, x=x_cordi, y=y_cordi,

vx=speed, vy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75))

(r)

explode_points.append(objects)

total_time = .0

# 1.8s内一直扩大

while total_time < 1.8:

sleep(0.01)

tnew = ()

t1, dt = tnew, tnew - t1

for point in explode_points:

for item in point:

(dt)

()

total_time += dt

# 循环调用

(wait_time, simulate, cv)

def close(*ignore):

"""退出程序、关闭窗口"""

global root

()

if __name__ == '__main__':

root = ()

cv = (root, height=520, width=750)

()

image = ("./")

photo = (image)

w = (cv)

t = (w)

# 选一个好看的背景会让效果更惊艳!

cv.create_image(0, 0, image=photo)

# ()

() # 隐藏画笔

().tracer(5, 0)

(bg='black') # wheat小麦

(90)

()

(150)

()

('sienna')

# 画樱花的躯干

Tree(60, t)

# 掉落的花瓣

Petal(200, t)

("WM_DELETE_WINDOW", close)

(100, simulate, cv)

()

四、飘过的雪花

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)

(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)

exitonclick()

六、HTML+JS 网页樱花

canvas{

position: absolute;

left: 0;

top: 0;

}

//两个canvas

var tree = ("tree");

= ;

= ;

var tCxt = ("2d");

var flower = ("flower");

= ;

= ;

var cxt = ("2d");

var flowerList = [];//樱花列表

var rootTop = 450 ;//树起点

var flowerColor = "rgba(255,192,203,.3)" ;//花色

var flowerColorDeep = "rgba(241,158,194,.5)" ;//花色深

var treeColor2 = "rgba(255,192,203,.5)" ;//树枝颜色

var treeColor = "#FFF" ;//树干颜色

var fallList = [];//飘落樱花列表

var g = 0.01 ;//重力加速度

var gWind = 0.005;//风力加速度

var limitSpeedY = 1;//速度上限

var limitSpeedX = 1 ;//速度上限

= "#FFF" ;

= 10 ;

function drawTree(x,y,deg,step,type){

var deg1 = step%2 == 0 ? 0.1 : -0.1 ;

var x1 = x + (deg+deg1) * (step+4) * 0.8 ;//以步长来判断枝干长度 x轴偏移大一些

var y1 = y + (deg+deg1) * (step-1) * 0.8 ;//以步长来判断枝干长度 Y轴压缩一些

();

= step/3;

(x,y);

(x1,y1);

= (step > 5 ) ? treeColor : treeColor2 ;//细纸条都换成花的颜色

();

if(step > 20){//树干相交的位置有间隙,以一个圆填充

= treeColor ;

(x,y,step/6,0,*2);

();

}

if(step < 3 || (step < 23 && () > 0.1)){

//末梢位置 画花瓣

var color = [flowerColorDeep,flowerColor,flowerColor][(()+0.2)] ;

var r = 2+()*2

= color ;

(x1+()*3,y1+()*3,r,0,)

();

({x:x,y:y,sx:(()-0.5),sy:0,color:color,r:r,deg:deg});//保存下画樱花的位置

}

step -- ;

if(step > 0){

drawTree(x1,y1,deg,step,type);

if(step%3 == 1 && step > 0 && step < 30)

drawTree(x1,y1,deg+0.2 + 0.3 * (),(step/1.13));//右分叉

if(step%3 == 0 && step > 0 && step < 30)

drawTree(x1,y1,deg-0.2 - 0.3 * (),(step/1.13));//左分叉

}

}

drawTree(/2,rootTop,-/2,30,1);//执行

var len = ;

function step(){

if(() > 0.3) (flowerList[(()*len)]);//随机取出一个,花瓣复制到飘落花瓣的列表中

(0,0,,);

for(var i = 0 ;i < ; i ++){

if(fallList[i].sy < limitSpeedY) fallList[i].sy += g ;

fallList[i].sx += gWind ;

fallList[i].x += fallList[i].sx ;

fallList[i].y += fallList[i].sy ;

if(fallList[i].y > rootTop+30){//飘到树根+30的花瓣移除

(i,1);

i -- ;

continue ;

}

();

= fallList[i].color ;

fallList[i].deg += fallList[i].sx*0.05 ;//跟速度相关的旋转花瓣

(fallList[i].x,fallList[i].y,fallList[i].r,fallList[i].deg,fallList[i].deg+*1.3);

();

}

requestAnimationFrame(step);

}

requestAnimationFrame(step);