当前位置:网站首页>Easily take you to the gate of turtle drawing
Easily take you to the gate of turtle drawing
2022-07-23 08:13:00 【Scattered prosperity for a lifetime, and colored glass for a li】
Turtle, Also called Turtle renderer , Use Turtle Kuhua is also called turtle drawing .Python Of turtle Kuo is Python A very popular function library of drawing image in language , Imagine a little turtle , On a horizontal axis x、 The vertical axis is y The origin of the coordinate system ,(0,0) Position start , It is controlled by a set of function instructions , Moving in this plane coordinate system , So it draws a graph on its crawling path .
1. Install and import turtle library
open PyCharm Terminal or cmd Command line window , Enter the command
pip install turtle
To complete the installation , Use in the compiler after installation import turtle Just import
2. Basic drawing commands
Imagine ourselves drawing on a picture book , In the center of the canvas, create a x-y Coordinate system , First, you need to determine the location of the drawing , The second is the direction of drawing . And in the turtle in , The default direction of drawing is right , Location is (0,0), That is, the center of the canvas , And canvas coordinates (x,y) The range of change is
− 220 ≤ x ≤ 220 -220 \leq x\leq 220 −220≤x≤220, − 220 ≤ y ≤ 220 -220 \leq y\leq 220 −220≤y≤220.
There are many commands for manipulating turtle drawings , These commands can be divided into 3 Kind of :
A motion command for a brush ; A control command for a brush ; Another is the global control command ;
1. Brush motion command
| command | explain |
|---|---|
| turtle.forward(distance) | Move to brush distance Pixel length |
| turtle.backward(distance) | Move in the opposite direction of the brush distance Pixel length |
| turtle.right(degree) | Move clockwise degree degree |
| turtle.left(degree) | Move counterclockwise degree degree |
| turtle.penup() | write , That is, when the brush moves, it does not draw graphics |
| turtle.pendown() | Set pen to paper , Draw the graph when you move after writing |
| turtle.goto(x,y) | Move the brush to the coordinate (x,y) The location of |
| turtle.speed(speed) | Change the speed of the brush ,speed=0 It's the biggest when I'm young |
| turtle.circle() | A circle , Radius is positive ( negative ), It is on the left of the brush ( Right ) Side flower circle |
2. Brush control command
| command | explain |
|---|---|
| turtle.pensize(width) | Set the width of the drawing |
| turtle.pencolor(color) | Set the color of the brush |
| turtle.fillcolor(color) | Set the color of the filled figure |
| turtle.color(color1,color2) | Set the color of the brush and the color of the filled figure at the same time |
| turtle.begin_fill() | Ready to start filling in figures |
| turtle.end_fill() | Filling complete |
| turtle.hideturtle() | Hide arrows to show |
| turtle.showturtle() | And hideturtle() contrary , Show arrows for |
3. Global control command
| command | explain |
|---|---|
| turtle.clear(() | Empty turtle window , however turtle The position and direction of the turtle will not change |
| turtle.reset() | Clear the window , Reset turtle The state is the starting state |
| turtle.undo() | Undo the last turtle action |
| turtle.write(s) | Put the text content s Write in the direction of the current turtle |
3.turtle A simple case
1. love
import turtle as t
t.speed(0)
def curvemove():
for i in range(200):
t.right(1)
t.forward(1)
t.color('red','pink')
t.begin_fill()
t.left(140)
t.forward(111.65)
curvemove()
t.left(120)
curvemove()
t.forward(111.65)
t.end_fill()
t.hideturtle()
t.done()

2. Colorful balls floating
import turtle as t
import random # random number
t.colormode(255)
t.speed(0)
for i in range(20):
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
# Random coordinates x y
x = random.randint(-220, 220)
y = random.randint(0, 220)
# write
t.penup()
t.goto(x, y)
t.pendown()
t.color(red, green, blue)
t.begin_fill()
t.circle(30)
t.end_fill()
t.right(90)
t.forward(30)
t.left(90)
# RGB red green blue
t.hideturtle()
t.done()

3. shield
import turtle as t
t.speed(0)
t.penup()
t.goto(0,-200)
t.pendown()
t.color('red')
t.begin_fill()
t.circle(200)
t.end_fill()
t.penup()
t.goto(0,-150)
t.pendown()
t.color('white')
t.begin_fill()
t.circle(150)
t.end_fill()
t.penup()
t.goto(0,-100)
t.pendown()
t.color('red')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(0,-50)
t.pendown()
t.color('blue')
t.begin_fill()
t.circle(50)
t.end_fill()
# Draw five pointed stars
t.penup()
t.goto(-40,10)
t.pendown()
t.color('white')
t.begin_fill()
for i in range(5):
t.forward(80)
t.right(144)
t.end_fill()
t.hideturtle()
t.done()

All in all , As long as you master the mathematical composition skills and principles of graphics , Any complex figure we can pass through a loop +turtle The drawing command is completed .
边栏推荐
- Arduino中断实现上升沿检测,并执行其他函数
- 这不是真正意义上的元宇宙,元宇宙应当具备自身鲜明的特质和独特的发展逻辑
- Flink高级API(三)
- Mysql A left(right) join B on A.id=B. ID and a.age=1 and a left (right) join b on a.id=b id where A.age=1
- Leetcode day 26
- leetcode-382.链表随机节点
- U盘被格式化数据能恢复吗,U盘被格式化了怎样恢复
- 21 -- product of arrays other than itself
- Matlab保存数据到csv文件的方法分享
- 笔者认为,元宇宙与互联网的发展逻辑在某种成都上是截然不同的
猜你喜欢

开发者分享|MindSpore Lite 体验,一键实现图像分割

ZABBIX agent creates monitoring items

Web resource sharing

SQL报错盲注实例分析

三种缓存策略:Cache Aside 策略、Read/Write Through 策略、Write Back 策略

Restclient operation index library - initialize restclient

押注全场景,荣耀胜算几何?

pip更新一个package
![Reading notes - > statistics] construction of 12-02 confidence interval -t distribution concept introduction](/img/4d/25b4d3d6af0fb30c222613d3c428b7.png)
Reading notes - > statistics] construction of 12-02 confidence interval -t distribution concept introduction

VMware virtual machine changes static IP and reports an error unit network Service entered failed state solution
随机推荐
TensorRT的插件实战(1)
[reading notes > statistics] 12-01 construction of confidence interval - Introduction to the concept of confidence interval
Worthington:来自酵母的己糖激酶的特性及其它参数说明
主控芯片CSU18M92开发智能体重秤方案
text-align:center居中
WPS data splitting
HCIP --- BGP综合实验
LeetCode 第26天
开发者分享|『啃书吧:深度学习与MindSpore实践』第一章
php数组下标是不是只能从0开始
leetcode-382.链表随机节点
数据脱敏的场景与价值【总结】
直播预告 | 开源安全治理模型和工具直播研讨会
Web resource sharing
js 正则删除span标签以及标签里面的内容
2022杭电多校联赛第二场 题解
C#中C/S端如何实现WebService服务
How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
Flink原理初探和流批一体API(二)v2
Wechat applet project practice