Python浪漫之星星与文字构造的错位图

效果图:

完整代码:

import tkinter as tk
import random
import math
from tkinter.constants import *

width = 888
height = 500
heartx = width / 2
hearty = height / 2
side = 11

class Star:
    def __init__(self, canvas, x, y, size):
        self.canvas = canvas
        self.x = x
        self.y = y
        self.size = size
        self.star_id = None

    def draw(self):
        if self.star_id is not None:
            self.canvas.delete(self.star_id)

        # 计算星星的五个点
        points = []
        for i in range(5):
            angle = math.radians(i * 144)  # 星星的角度
            x = self.x + self.size * math.cos(angle)
            y = self.y + self.size * math.sin(angle)
            points.append((x, y))

        # 创建空心星星
        self.star_id = self.canvas.create_polygon(points, outline='yellow', fill='', width=2)

    def update(self):
        # 随机散列和抖动
        scatter_x = random.uniform(-5, 5)  # 散列范围
        scatter_y = random.uniform(-5, 5)
        self.x += scatter_x
        self.y += scatter_y
        self.draw()


class StarAnimation:
    def __init__(self, root):
        self.canvas = tk.Canvas(root, width=800, height=600, bg='black')
        self.canvas.pack()

        # 创建一颗大星星
        self.star = Star(self.canvas, 400, 300, 200)  # 在(400, 300)位置绘制大小为100的星星
        self.animate()

        tk.Label(root, text="Dynamic Hollow Star Animation", bg="black", fg="#FF99CC", font="Helvetic 25 bold").place(relx=.5, rely=.5,anchor=CENTER)

    def animate(self):
        self.star.update()

if __name__ == '__main__':
    root = tk.Tk()
    root.title("Dynamic Hollow Star Animation")
    animation = StarAnimation(root)

    root.resizable(0, 0)
    root.wm_attributes("-toolwindow", 1)
    root.mainloop()

    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    widths = 300
    heights = 100
    x = (screenwidth - widths) / 2
    y = (screenheight - heights) / 2 - 66
    root.destroy()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值