文章目录
- 背景颜色
- 画圆
- 太阳花
- 树
- 椭圆
背景颜色
import turtle
pen = turtle.Turtle()
turtle.Screen().bgcolor("blue")
pen.color("cyan")
for i in range(10):
for i in range(2):
pen.forward(100)
pen.right(60)
pen.forward(100)
pen.right(120)
pen.right(36)
turtle.done()
画圆
import turtle
pen = turtle.Turtle()
pen.pencolor('red') # 设置画笔颜色
pen.fillcolor('red') # 设置填充颜色
# pen.goto(-100, 150) # 移动画笔到坐标点(-100, 150)
pen.begin_fill()
turtle.circle(100, 80)
# pen.end_fill()
pen.hideturtle()
turtle.done() # 结束绘画
太阳花
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
树
import turtle
def y(sz, level):
""" 定义Y函数,sz表示是树的尺寸, level是树的层次级别 """
if level > 0:
# 按照树的层次级别(level)设置树的颜色
pen.pencolor(0, 255 // level, 0)
pen.forward(sz)
pen.right(angle)
# 递归调用y函数,绘制右边子树
y(0.8 * sz, level - 1)
pen.pencolor(0, 255 // level, 0)
pen.left(2 * angle)
# 递归调用y函数,绘制左边子树
y(0.8 * sz, level - 1)
pen.pencolor(0, 255 // level, 0)
pen.right(angle)
pen.forward(-sz)
# if __name__ == '__main__':
pen = turtle.Turtle()
# 设置绘图速度
pen.speed('fastest')
# 设置颜色模式为255,即颜色范围是0~255
turtle.colormode(255)
# 设置画笔朝向
pen.right(-90)
# 设置树杈角度
angle = 30
# 创建尺寸为80,层次级别是7的数
y(80, 7)
turtle.done() # 结束绘画
椭圆
import turtle
def draw(rad):
for i in range(2):
turtle.circle(rad, 90)
turtle.circle(rad // 2, 90)
turtle.seth(-45)
draw(150)
turtle.done()