赞
踩
Turtle库是Python语言中的一个标准库,它提供了一种有趣的方式来介绍编程和图形绘制的基本概念。Turtle库使用一个虚拟的“海龟”来绘制图形。你可以控制海龟的方向、速度和位置,通过向前移动、向左转或向右转等命令来绘制线条、圆弧多边形等图形。
---------------turtle源码集合---------------
Python教程36:海龟画图turtle写春联
Python源码35:海龟画图turtle画中国结
Python源码31:海龟画图turtle画七道彩虹
Python源码30:海龟画图turtle画紫色的小熊
Python源码29:海龟画图turtle画太极图
Python源码28:海龟画图turtle画熊猫
Python源码27:海龟画图turtle画动态圆舞曲
Python源码26:海龟画图turtle画向日葵
Python源码25:海龟画图turtle画小猪佩奇
Python源码24:使用海龟画图turtle画滑板
Python源码23:使用海龟画图turtle画小狗狗
Python源码22:使用海龟画图turtle画今天日期
Python源码21:使用海龟画图turtle画太阳,云朵,房子,绿树
Python源码20:使用海龟画图turtle画一个会动的星空
Python源码19:海龟画图turtle画螺旋的彩色的逐渐放大的文字
Python源码18:使用海龟画图turtle画捂脸表情
Python源码17:使用海龟画图turtle画五星红旗
Python源码16:使用海龟画图turtle画会动的时钟
Python源码15:使用海龟画图turtle画小黄人
Python源码14:使用海龟画图turtle画我的城堡
Python源码分享13:使用海龟画图turtle画一个会眨眼的皮卡丘
Python源码分享12:使用turtle画彩色六边形
Python源码分享11:使用海龟画图turtle画航天火箭
Python源码分享10:使用海龟画图turtle画哆啦A梦
Python源代码分享:02海龟画图五角星
Python源代码分享:03画一个奥运五环图
Python源代码分享:05使用turtle模块绘制一个彩色螺旋图案
Python源代码分享:07画满天繁星
Python源码分享08:使用turtle画一朵玫瑰花
Python源码分享10:使用海龟画图turtle画哆啦A梦
Python源码分享11:使用海龟画图turtle画航天火箭
Python源码分享12:使用turtle画彩色六边形
import random from math import sin, cos, pi, log from tkinter import * CANVAS_WIDTH = 640 CANVAS_HEIGHT = 480 CANVAS_CENTER_X = CANVAS_WIDTH / 2 CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 IMAGE_ENLARGE = 11 HEART_COLOR = "#FF99CC" def center_window(root, width, height): screenwidth = root.winfo_screenwidth() # 获取显示屏宽度 screenheight = root.winfo_screenheight() # 获取显示屏高度 size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # 设置窗口居中参数 root.geometry(size) # 让窗口居中显示 def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE): x = 16 * (sin(t) ** 3) y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)) # 放大 x *= shrink_ratio y *= shrink_ratio # 移到画布中央 x += CANVAS_CENTER_X y += CANVAS_CENTER_Y return int(x), int(y) def scatter_inside(x, y, beta=0.15): ratio_x = - beta * log(random.random()) ratio_y = - beta * log(random.random()) dx = ratio_x * (x - CANVAS_CENTER_X) dy = ratio_y * (y - CANVAS_CENTER_Y) return x - dx, y - dy def shrink(x, y, ratio): force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6) dx = ratio * force * (x - CANVAS_CENTER_X) dy = ratio * force * (y - CANVAS_CENTER_Y) return x - dx, y - dy def curve(p): return 2 * (2 * sin(4 * p)) / (2 * pi) class Heart: def __init__(self, generate_frame=20): self._points = set() # 原始爱心坐标集合 self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合 self._center_diffusion_points = set() # 中心扩散效果点坐标集合 self.all_points = {} # 每帧动态点坐标 self.build(2000) self.random_halo = 1000 self.generate_frame = generate_frame for frame in range(generate_frame): self.calc(frame) def build(self, number): for _ in range(number): t = random.uniform(0, 2 * pi) x, y = heart_function(t) self._points.add((x, y)) # 爱心内扩散 for _x, _y in list(self._points): for _ in range(3): x, y = scatter_inside(_x, _y, 0.05) self._edge_diffusion_points.add((x, y)) # 爱心内再次扩散 point_list = list(self._points) for _ in range(4000): x, y = random.choice(point_list) x, y = scatter_inside(x, y, 0.17) self._center_diffusion_points.add((x, y)) @staticmethod def calc_position(x, y, ratio): force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520) dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1) dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1) return x - dx, y - dy def calc(self, generate_frame): ratio = 10 * curve(generate_frame / 10 * pi) halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi))) halo_number = int( 3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2)) all_points = [] # 光环 heart_halo_point = set() for _ in range(halo_number): t = random.uniform(0, 2 * pi) x, y = heart_function(t, shrink_ratio=11.6) x, y = shrink(x, y, halo_radius) if (x, y) not in heart_halo_point: heart_halo_point.add((x, y)) x += random.randint(-14, 14) y += random.randint(-14, 14) size = random.choice((1, 2, 2)) all_points.append((x, y, size)) # 轮廓 for x, y in self._points: x, y = self.calc_position(x, y, ratio) size = random.randint(1, 3) all_points.append((x, y, size)) # 内容 for x, y in self._edge_diffusion_points: x, y = self.calc_position(x, y, ratio) size = random.randint(1, 2) all_points.append((x, y, size)) self.all_points[generate_frame] = all_points for x, y in self._center_diffusion_points: x, y = self.calc_position(x, y, ratio) size = random.randint(1, 2) all_points.append((x, y, size)) self.all_points[generate_frame] = all_points def render(self, render_canvas, render_frame): for x, y, size in self.all_points[render_frame % self.generate_frame]: render_canvas.create_rectangle( x, y, x + size, y + size, width=0, fill=HEART_COLOR) def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0): render_canvas.delete('all') render_heart.render(render_canvas, render_frame) main.after(160, draw, main, render_canvas, render_heart, render_frame + 1) if __name__ == '__main__': root = Tk() root.title("爱心粒子") center_window(root, CANVAS_WIDTH, CANVAS_HEIGHT) # 窗口居中显示 canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH) canvas.pack() heart = Heart() draw(root, canvas, heart) Label(root, text="我爱你", bg="black", fg="#FF99CC", font="Helvetic 20 bold").place( relx=.5, rely=.5, anchor=CENTER) root.mainloop()
文字版爱心
import time
y = 2.5
while y>=-1.6:
x = -3.0
while x<=4.0:
if (x*x+y*y-1)**3<=3.6*x*x*y*y*y or (x>-2.4 and x<-2.1 and y<1.5 and y>-1) or (((x<2.5 and x>2.2)or(x>3.4 and x<3.7)) and y>-1 and y<1.5) or (y>-1 and y<-0.6 and x<3.7 and x>2.2):
print(' ',end="")
else:
print('*',end="")
x += 0.1
print()
time.sleep(0.25)
y -= 0.2
完毕!!感谢您的收看
----------★★历史博文集合★★----------
我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。