开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第30天,点击查看活动详情
马上双旦了给大家带来一些python代码:
1.元旦节日倒计时代码的实现
2.使用python源码“绘制圣诞树”的实现
3.使用python绘制“跨年烟花”的实现
4.使用python 绘制“爱心”的实现
接下来我们一个一个实现吧!
1.元旦倒计时代码:
输入相应的节日时间,可以计算任何节日的倒计时。
# -*- coding='utf-8' -*- ''' 功能:基于python的元旦倒计时代码 作者:Pegasus 时间:2022/12/09 ''' # 导入所需要的功能模块 import datetime import sys import math import time # 定义新的一年日期 spring = datetime.datetime(2023, 1, 1, 0, 0, 0) # 新的一年的日期 def caldays(): while True: # 获取当前的日期 today = datetime.datetime.now() # 新年日期减去当前日期 day = (spring - today).days # 得到秒数 second = (spring - today).seconds # 计算秒 sec = second % 60 # 计算分 minute = second / 60 % 60 # 计算小时 hour = second / 60 / 60 # 计算天数 if hour > 24: hour = hour - 24 hour = math.floor(hour) # 去掉小数点,向下取整 minute = math.floor(minute) # 去掉小数点,向下取整 # 输出结果 sys.stdout.write("离2023年元旦还有" + str(day) + "天" + str(hour) + "小时" + str(minute) + "分钟" + str(sec) + "秒" + 'r') sys.stdout.flush() time.sleep(1) print("离2023年元旦还有" + str(day) + "天" + str(hour) + "小时" + str(minute) + "分钟" + str(sec) + "秒" + 'r') if __name__ == '__main__': caldays()
2.绘制圣诞树代码:
主要使用turtle库,绘制彩色圣诞树。
# -*- coding='utf-8' -*- ''' 功能:基于python的圣诞树绘制 作者:Pegasus 时间:2022/12/09 ''' #导入turtle库 import turtle #设置屏幕大小 screen = turtle.Screen() screen.setup(800,600) #获取画笔并设置一些属性:圆形、红色、快 circle = turtle.Turtle() circle.shape('circle') circle.color('red') circle.speed('fastest') #抬起画笔 circle.up() #重新获取画笔 square = turtle.Turtle() #重新设置画笔属性:四方形、绿色、快 square.shape('square') square.color('green') square.speed('fastest') #重新抬起画笔 square.up() #跳到指定坐标位置 circle.goto(0,280) #复制当前图形 circle.stamp() k = 0 for i in range(1, 17): y = 30*i for j in range(i-k): x = 30*j square.goto(x,-y+280) square.stamp() square.goto(-x,-y+280) square.stamp() if i % 4 == 0: x = 30*(j+1) circle.color('red') circle.goto(-x,-y+280) circle.stamp() circle.goto(x,-y+280) circle.stamp() k += 2 if i % 4 == 3: x = 30*(j+1) circle.color('yellow') circle.goto(-x,-y+280) circle.stamp() circle.goto(x,-y+280) circle.stamp() square.color('brown') for i in range(17,20): y = 30*i for j in range(3): x = 30*j square.goto(x,-y+280) square.stamp() square.goto(-x,-y+280) square.stamp() turtle.exitonclick()
3.跨年烟花代码:
主要使用tkinter绘制界面,使用多线程开启烟花画面,绘制结果为动图。
# -*- coding: utf-8 -*- import math, random,time import threading import tkinter as tk import re #import uuid Fireworks=[] maxFireworks=8 height,width=600,600 class firework(object): def __init__(self,color,speed,width,height): #uid=uuid.uuid1() self.radius=random.randint(2,4) #粒子半径为2~4像素 self.color=color #粒子颜色 self.speed=speed #speed是1.5-3.5秒 self.status=0 #在烟花未爆炸的情况下,status=0;爆炸后,status>=1;当status>100时,烟花的生命期终止 self.nParticle=random.randint(20,30) #粒子数量 self.center=[random.randint(0,width-1),random.randint(0,height-1)] #烟花随机中心坐标 self.oneParticle=[] #原始粒子坐标(100%状态时) self.rotTheta=random.uniform(0,2*math.pi) #椭圆平面旋转角 #椭圆参数方程:x=a*cos(theta),y=b*sin(theta) #ellipsePara=[a,b] self.ellipsePara=[random.randint(30,40),random.randint(20,30)] theta=2*math.pi/self.nParticle for i in range(self.nParticle): t=random.uniform(-1.0/16,1.0/16) #产生一个 [-1/16,1/16) 的随机数 x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t) #椭圆参数方程 xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta), y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta) #平面旋转方程 self.oneParticle.append([xx,yy]) self.curParticle=self.oneParticle[0:] #当前粒子坐标 self.thread=threading.Thread(target=self.extend) #建立线程对象 def extend(self): #粒子群状态变化函数线程 for i in range(100): self.status+=1 #更新状态标识 self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle] #更新粒子群坐标 time.sleep(self.speed/50) def explode(self): self.thread.setDaemon(True) #把现程设为守护线程 self.thread.start() #启动线程 def __repr__(self): return ('color:{color}n' 'speed:{speed}n' 'number of particle: {np}n' 'center:[{cx} , {cy}]n' 'ellipse:a={ea} , b={eb}n' 'particle:n{p}n' ).format(color=self.color,speed=self.speed,np=self.nParticle,cx=self.center[0],cy=self.center[1],p=str(self.oneParticle),ea=self.ellipsePara[0],eb=self.ellipsePara[1]) def colorChange(fire): rgb=re.findall(r'(.{2})',fire.color[1:]) cs=fire.status f=lambda x,c: hex(int(int(x,16)*(100-c)/30))[2:] #当粒子寿命到70%时,颜色开始线性衰减 if cs>70: ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs) else: ccr,ccg,ccb=rgb[0],rgb[1],rgb[2] return '#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb) def appendFirework(n=1): #递归生成烟花对象 if n>maxFireworks or len(Fireworks)>maxFireworks: pass elif n==1: cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:]) # 产生一个0~16777215(0xFFFFFF)的随机数,作为随机颜色 a=firework(cl,random.uniform(1.5,3.5),width,height) Fireworks.append( {'particle':a,'points':[]} ) #建立粒子显示列表,‘particle’为一个烟花对象,‘points’为每一个粒子显示时的对象变量集 a.explode() else: appendFirework() appendFirework(n-1) def show(c): for p in Fireworks: #每次刷新显示,先把已有的所以粒子全部删除 for pp in p['points']: c.delete(pp) for p in Fireworks: #根据每个烟花对象,计算其中每个粒子的显示对象 oneP=p['particle'] if oneP.status==100: #状态标识为100,说明烟花寿命结束 Fireworks.remove(p) #移出当前烟花 appendFirework() #新增一个烟花 continue else: li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle] #把中心为原点的椭圆平移到随机圆心坐标上 color=colorChange(oneP) #根据烟花当前状态计算当前颜色 for pp in li: p['points'].append(c.create_oval(pp[0]-oneP.radius, pp[1]-oneP.radius, pp[0]+oneP.radius, pp[1]+oneP.radius, fill=color)) #绘制烟花每个粒子 root.after(50, show,c) #回调,每50ms刷新一次 if __name__=='__main__': appendFirework(maxFireworks) root = tk.Tk() cv = tk.Canvas(root, height=height, width=width) cv.create_rectangle(0, 0, width, height, fill="black") cv.pack() root.after(50, show,cv) root.mainloop()
4.绘制爱心代码 主要是使用turtle库绘制。
# -*- coding='utf-8' -*- ''' 功能:基于python的爱心绘制 作者:Pegasus 时间:2022/12/11 ''' import turtle import time def LittleHeart(): for i in range(200): turtle.right(1) turtle.forward(2) # love = input('请输入表白语句,然后回车,默认为"I Love You":n') # me = input('请输入要表白的人:n') # if love=='': # # 如果未输入表白语句,则使用默认语句 # love='I Love you' love='I Love you' me = '年轻人,就要浪漫点。' turtle.setup(width=900,height=600) # 爱心的画布的大小 turtle.color('red','red') # 爱心的颜色及外边笔的颜色 turtle.pensize(5) # 画笔的粗细 turtle.speed(1000000) # 绘制速度 turtle.up() # 画笔向上 turtle.hideturtle() turtle.goto(0,-180) turtle.showturtle() turtle.down() turtle.speed(5) turtle.begin_fill() # 开始填充 turtle.left(140) turtle.forward(224) LittleHeart() turtle.left(120) LittleHeart() turtle.forward(224) turtle.end_fill() turtle.pensize(5) turtle.up() turtle.hideturtle() turtle.goto(0,0) turtle.showturtle() turtle.color('#CD5C5C','pink') turtle.write(love,font=('gungsuh',30,),align="center") turtle.up() turtle.hideturtle() if me !='': turtle.color('black', 'pink') time.sleep(2) turtle.goto(180,-180) turtle.showturtle() turtle.write(me, font=(20,), align="center", move=True) window=turtle.Screen() window.exitonclick()
相关知识
python实现元旦倒计时、圣诞树、跨年烟花的绘画马上双旦了给大家带来一些python代码 1.元旦节日倒计时代码的实现
烟花秀、打铁花、灯光秀、跨年倒计时、放飞气球...郑州元旦跨年活动攻略来了
程序员的浪漫!用Python实现表白代码!
Python实现识别花卉种类的示例代码
七夕最浪漫的表白,最真挚的感情(Python代码实现)
上海外滩:新年与生命的倒计时
SKYNE/python
基于深度学习的花卉检测与识别系统(YOLOv5清新界面版,Python代码)
用python画花瓣
【Python】基础
网址: python实现元旦倒计时、圣诞树、跨年烟花的绘画马上双旦了给大家带来一些python代码 1.元旦节日倒计时代码的实现 https://m.huajiangbk.com/newsview140171.html
上一篇: Html+Css+js实现春节倒 |
下一篇: “相约北京”奥林匹克文化节暨第2 |