找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
[AD]畅连VPN(Let’s Conn VPN)[AD]Netflix520专业流媒体解锁服务[AD]Haita高速中转机场
[AD]赛博支付-专业机场支付
查看: 152|回复: 2

分享一个表白流星的python源码

[复制链接]

1

主题

6

回帖

47

积分

新手上路

积分
47
发表于 2024-10-21 16:20:31 | 显示全部楼层 |阅读模式
  1. import turtle as tu
  2. import random as ra
  3. import tkinter as tk
  4. import math
  5. def Meteors():
  6.     tu.setup(1.0, 1.0)
  7.     tu.screensize(1.0, 1.0)    #设置画布大小
  8.     tu.bgcolor('black')  #设置画布颜色
  9.     tu.title("流星雨")
  10.     t = tu.Pen()
  11.     t.hideturtle()               #隐藏画笔
  12.     # colors = ['skyblue', 'white', 'cyan', 'aqua']   #流星的颜色列表,蓝色
  13.     colors = ['pink', 'lightpink', 'deeppink']  #粉色
  14.     class Star():    #流星类
  15.         def __init__(self):
  16.             self.r = ra.randint(50,100)
  17.             self.t = ra.randint(1,3)
  18.             self.x = ra.randint(-2000,1000)   #流星的横坐标
  19.             self.y = ra.randint(444, 999)  #流星的纵坐标
  20.             self.speed = ra.randint(5,10)     #流星移动速度
  21.             self.color = ra.choice(colors)    #流星的颜色
  22.             self.outline = 1                 #流星的大小
  23.         def star(self):                #画流星函数
  24.             t.pensize(self.outline)    #流星的大小
  25.             t.penup()                  #提笔
  26.             t.goto(self.x,self.y)      #随机位置
  27.             t.pendown()                #落笔
  28.             t.color(self.color)
  29.             t.begin_fill()
  30.             t.fillcolor(self.color)
  31.             t.setheading(-30)
  32.             t.right(self.t)
  33.             t.forward(self.r)
  34.             t.left(self.t)
  35.             t.circle(self.r*math.sin(math.radians(self.t)),180)
  36.             t.left(self.t)
  37.             t.forward(self.r)
  38.             t.end_fill()
  39.         def move(self):                    #流星移动函数
  40.             if self.y >= -500:            #当流星还在画布中时
  41.                 self.y -= self.speed     #设置上下移动速度
  42.                 self.x += 2*self.speed   #设置左右移动速度
  43.             else:
  44.                 self.r = ra.randint(50,100)
  45.                 self.t = ra.randint(1,3)
  46.                 self.x = ra.randint(-2000,1000)
  47.                 self.y = 444
  48.                 self.speed = ra.randint(5,10)
  49.                 self.color = ra.choice(colors)
  50.                 self.outline = 1
  51.     Stars = []            #用列表保存所有流星
  52.     for i in range(100):
  53.         Stars.append(Star())
  54.     while True:           #开始绘制
  55.         tu.tracer(0)
  56.         t.clear()
  57.         for i in range(99):    #99个流星
  58.             Stars<i>.move()
  59.             Stars<i>.star()
  60.         tu.update()
  61.     tu.mainloop()

  62. def love():
  63.     root = tk.Tk()
  64.     root.title('❤')
  65.     root.resizable(0, 0)
  66.     root.wm_attributes("-toolwindow", 1)
  67.     screenwidth = root.winfo_screenwidth()
  68.     screenheight = root.winfo_screenheight()
  69.     widths = 300
  70.     heights = 100
  71.     x = (screenwidth - widths) / 2
  72.     y = (screenheight - heights) / 2
  73.     root.geometry('%dx%d+%d+%d' % (widths, heights, x, y))  # 设置在屏幕中居中显示
  74.     tk.Label(root, text='亲爱的,做我女朋友好吗?', width=37, font=('宋体', 12)).place(x=0, y=10)


  75.     def OK():  # 同意按钮
  76.         root.destroy()
  77.         # 同意后显示相应的效果
  78.         Meteors()

  79.     def NO():  # 拒绝按钮,拒绝不会退出,必须同意才可以退出哦~
  80.         tk.messagebox.showwarning('❤', '再给你一次机会!')


  81.     def closeWindow():
  82.         tk.messagebox.showwarning('❤', '逃避是没有用的哦')


  83.     tk.Button(root, text='好哦', width=5, height=1, command=OK).place(x=80, y=50)
  84.     tk.Button(root, text='不要', width=5, height=1, command=NO).place(x=160, y=50)
  85.     root.protocol('WM_DELETE_WINDOW', closeWindow)  # 绑定退出事件
  86.     root.mainloop()

  87. if __name__ == "__main__":
  88.     love()
复制代码

7

主题

7

回帖

138

积分

管理员

打酱油的

积分
138
发表于 2024-11-20 03:02:37 | 显示全部楼层
还不错,帮你设置了下代码格式

1

主题

6

回帖

47

积分

新手上路

积分
47
 楼主| 发表于 2024-11-20 09:11:26 | 显示全部楼层
moshao 发表于 2024-11-20 03:02
还不错,帮你设置了下代码格式

我也是网上找的,不是自己写的,我没这个水平
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|FFQ社区

GMT+8, 2025-1-22 18:12 , Processed in 0.023187 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表