当前位置: 代码网 > it编程>前端脚本>Python > 如何用Python绘制简易动态圣诞树

如何用Python绘制简易动态圣诞树

2025年01月03日 Python 我要评论
代码:import randomimport timefrom math import pi, cos, sinfrom tkinter import *canvas_width = 640 # 画

代码:

import random
import time
from math import pi, cos, sin
from tkinter import *

canvas_width = 640  # 画布的宽
canvas_height = 640  # 画布的高
tree_color = "#2e8b57"  # 树的颜色
trunk_color = "#8b4513"  # 树干的颜色
star_color = "#ffff00"  # 星星的颜色
light_color = "#ff4500"  # 灯光的颜色
background_color = "#000000"  # 背景颜色
text_color = "#ffffff"  # 文字颜色
text_font = ('arial', 24, 'bold')  # 文字的字体

# 圣诞树的大小和位置
tree_bottom = canvas_height - 50
tree_top = tree_bottom - 200
tree_width = 200
trunk_width = 40
trunk_height = 50


def draw_tree(canvas):
    # 树的底部三角形部分
    canvas.create_polygon(
        canvas_width // 2, tree_top,  # 顶部
        canvas_width // 2 - tree_width, tree_bottom,  # 左下角
        canvas_width // 2 + tree_width, tree_bottom,  # 右下角
        fill=tree_color, outline="green", width=2
    )

    # 树的中部三角形部分
    canvas.create_polygon(
        canvas_width // 2, tree_top + 40,  # 顶部
        canvas_width // 2 - tree_width * 0.8, tree_bottom - 40,  # 左下角
        canvas_width // 2 + tree_width * 0.8, tree_bottom - 40,  # 右下角
        fill=tree_color, outline="green", width=2
    )

    # 树的顶部三角形部分
    canvas.create_polygon(
        canvas_width // 2, tree_top + 80,  # 顶部
        canvas_width // 2 - tree_width * 0.6, tree_bottom - 80,  # 左下角
        canvas_width // 2 + tree_width * 0.6, tree_bottom - 80,  # 右下角
        fill=tree_color, outline="green", width=2
    )

    # 树干
    canvas.create_rectangle(
        canvas_width // 2 - trunk_width // 2, tree_bottom,  # 左上角
        canvas_width // 2 + trunk_width // 2, tree_bottom + trunk_height,  # 右下角
        fill=trunk_color, outline=trunk_color
    )


def draw_star(canvas, x, y, size):
    # 绘制一个星星
    canvas.create_oval(
        x - size, y - size, x + size, y + size,  # 左上角和右下角
        fill=star_color, outline=star_color
    )


def draw_lights(canvas, x, y, size, color):
    # 绘制一颗灯泡
    canvas.create_oval(
        x - size, y - size, x + size, y + size,  # 左上角和右下角
        fill=color, outline=color
    )


def draw_dynamic_star(canvas, frame):
    # 让星星闪烁
    if frame % 30 < 15:
        draw_star(canvas, canvas_width // 2, tree_top - 40, 15)


def draw_dynamic_lights(canvas, frame):
    # 动态灯泡
    num_lights = 15
    for i in range(num_lights):
        angle = 2 * pi * i / num_lights  # 每个灯泡的位置角度
        x = canvas_width // 2 + (tree_width * 0.6) * cos(angle)
        y = tree_top + 80 + (tree_width * 0.6) * sin(angle)

        # 每个灯泡随机闪烁
        color = light_color if random.random() > 0.5 else "black"
        draw_lights(canvas, x, y, 5, color)


def draw_text(canvas):
    # 显示圣诞祝福
    canvas.create_text(canvas_width // 2, canvas_height - 20, text="merry christmas!", fill=text_color, font=text_font)


def draw_dynamic_tree(main, canvas, frame):
    # 清空画布
    canvas.delete('all')

    # 绘制圣诞树
    draw_tree(canvas)

    # 绘制闪烁的星星
    draw_dynamic_star(canvas, frame)

    # 绘制动态灯光
    draw_dynamic_lights(canvas, frame)

    # 绘制祝福语
    draw_text(canvas)

    # 设置每帧调用
    main.after(100, draw_dynamic_tree, main, canvas, frame + 1)


if __name__ == '__main__':
    root = tk()  # 创建一个tk对象
    canvas = canvas(root, bg=background_color, height=canvas_height, width=canvas_width)
    canvas.pack()

    # 动态绘制圣诞树
    draw_dynamic_tree(root, canvas, 0)

    # 启动应用
    root.mainloop()

效果:

总结 

到此这篇关于如何用python绘制简易动态圣诞树的文章就介绍到这了,更多相关python绘制动态圣诞树内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

  • 使用Python绘制词云图的详细教程

    使用Python绘制词云图的详细教程

    引言词云(word cloud)是一种数据可视化技术,用于显示文本数据中的频繁单词。它通过将出现频率较高的词汇以较大的字体显示,频率较低的词汇则以较小的字体显示... [阅读全文]
  • python中_init_.py的作用

    最近有几个小伙伴问了我一个经典问题:“__init__.py 到底有啥用?”其实这个问题挺常见的,尤其是对python新手来说简直就是一团迷雾。今天就站在一…

    2025年01月04日 前端脚本
  • python subprocess.run中的具体使用

    python subprocess.run中的具体使用

    一、详解subprocess.run 是 python 3.5 及以上版本中引入的一个函数,用于运行子进程。它是 subprocess 模块的一部分,提供了一种... [阅读全文]
  • python中poetry安装依赖

    前言poetry 是一个用于管理 python 项目的依赖和构建过程的工具。它简化了包管理和虚拟环境的创建,让开发者更容易管理项目的依赖关系。它的出现让我们对依赖卸载更干净。1. …

    2025年01月04日 前端脚本
  • 浅析Python中的基本交易算法应用

    浅析Python中的基本交易算法应用

    在金融市场中,算法交易已成为一种重要的交易方式。它通过自动化的程序来执行交易策略,可以在短时间内分析大量数据并做出交易决策。python语言由于其强大的数据处理... [阅读全文]
  • python中GIL锁的实现

    python中GIL锁的实现

    什么是python的 gil 锁?gil的全称是global interpreter lock(全局解释器锁),它是 cpython(python 的主流实现)... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com