python turtle 各种示例

 

pensize() 函数的示例

复制代码
import turtle
step=100
turtle.penup()
turtle.setposition(-400, 0)
turtle.pendown()
turtle.speed(1)
turtle.write(0.001)
turtle.pensize(0.001)
turtle.forward(step)
turtle.write(0.01)
turtle.pensize(0.01)
turtle.forward(step)
turtle.write(0.01)
turtle.pensize(0.1)
turtle.forward(step)
turtle.write(1)
turtle.pensize(1)
turtle.forward(step)
turtle.write(10)
turtle.pensize(10)
turtle.forward(step)
turtle.write(100)
turtle.pensize(100)
turtle.forward(step)
#turtle.write(1000)
#turtle.pensize(1000)
#turtle.forward(step)
turtle.done()
复制代码

 

 可以看出最小是1像素,最大不限制。

 

turtle 可以用的全部颜色(所有颜色)

 

 

 

 

matplotlib 颜色名称

复制代码
"""
========================
Visualizing named colors
========================

Simple plot example with the named colors and its visual representation.
"""
from __future__ import division

import matplotlib.pyplot as plt
from matplotlib import colors as mcolors


colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)

# Sort colors by hue, saturation, value and name.
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
                for name, color in colors.items())
sorted_names = [name for hsv, name in by_hsv]

n = len(sorted_names)
ncols = 4
nrows = n // ncols + 1

fig, ax = plt.subplots(figsize=(8, 5))

# Get height and width
X, Y = fig.get_dpi() * fig.get_size_inches()
h = Y / (nrows + 1)
w = X / ncols

for i, name in enumerate(sorted_names):
    col = i % ncols
    row = i // ncols
    y = Y - (row * h) - h

    xi_line = w * (col + 0.05)
    xf_line = w * (col + 0.25)
    xi_text = w * (col + 0.3)

    ax.text(xi_text, y, name, fontsize=(h * 0.8),
            horizontalalignment='left',
            verticalalignment='center')

    ax.hlines(y + h * 0.1, xi_line, xf_line,
              color=colors[name], linewidth=(h * 0.6))

ax.set_xlim(0, X)
ax.set_ylim(0, Y)
ax.set_axis_off()

fig.subplots_adjust(left=0, right=1,
                    top=1, bottom=0,
                    hspace=0, wspace=0)
plt.show()
复制代码

 

画图速度 turtle.speed()

复制代码
import turtle
step=200
turtle.penup()
turtle.setposition(-400, 0)
turtle.pendown()

turtle.speed(1)
turtle.forward(step)
turtle.speed(5)
turtle.forward(step)
turtle.speed(10)
turtle.forward(step)
turtle.speed(100)
turtle.forward(step)
turtle.speed(0)
turtle.forward(step)
turtle.done()
复制代码

 

五星红旗,五角星

复制代码
import turtle

turtle.setup(600,400,0,0)
turtle.bgcolor("red")
turtle.fillcolor("yellow")
turtle.color('yellow')
turtle.speed(10)
#主星
turtle.begin_fill()
turtle.up()
turtle.goto(-280,100)
turtle.down()
for i in range (5):
    turtle.forward(150)
    turtle.right(144)
turtle.end_fill()
#第1颗副星
turtle.begin_fill()
turtle.up()
turtle.goto(-100,180)
turtle.setheading(305)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.left(144)
turtle.end_fill()
#第2颗副星
turtle.begin_fill()
turtle.up()
turtle.goto(-50,110)
turtle.setheading(30)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()
#第3颗副星
turtle.begin_fill()
turtle.up()
turtle.goto(-40,50)
turtle.setheading(5)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.right(144)
turtle.end_fill()
#第4颗副星
turtle.begin_fill()
turtle.up()
turtle.goto(-100,10)
turtle.setheading(300)
turtle.down()
for i in range (5):
    turtle.forward(50)
    turtle.left(144)
turtle.end_fill()
turtle.done()
复制代码

 

posted @   emanlee  阅读(1078)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2021-02-20 深度学习资料
2010-02-20 The message received from the server could not be parsed
2010-02-20 ScriptLoader.loadScripts cannot be called while the ScriptLoader is already loading scripts
2009-02-20 C语言程序设计 第六章 结构体和共用体
2009-02-20 C语言程序设计 第七章 指针
2009-02-20 C语言程序设计 第八章 文件
2009-02-20 C语言程序设计 第五章 函数
点击右上角即可分享
微信分享提示