欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

今天你画树了吗?用Python就可以了!

程序员文章站 2024-03-18 15:12:58
...

偶然间看到广大社区里有人在画树,觉得效果还不错,那我不得玩一把!
来了,老弟…不好意思啊哥~
想直接看代码效果的可以直接拉github代码


基本命令 解释
turtle.Screen() 返回一个singleton object of a TurtleScreen subclass
turtle.forward(distance) 向当前画笔方向移动distance像素长
turtle.backward(distance) 向当前画笔相反方向移动distance像素长度
turtle.right(degree) 顺时针移动degree°
turtle.left(degree) 逆时针移动degree°
turtle.pendown() 移动时绘制图形,缺省时也为绘制
turtle.goto(x,y) 将画笔移动到坐标为x,y的位置
turtle.penup() 移动时不绘制图形,提起笔,用于另起一个地方绘制时用
turtle.speed(speed) 画笔绘制的速度范围[0,10]整数
turtle.circle() 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
颜色命令 解释
turtle.fillcolor() 绘制图形的填充颜色
turtle.color(color1,color2) 同时绘制pencolor=color1,fillcolor=color2
turtle.begin_fill() 准备开始填充图形
turtle.end_fill() 填充完成
turtle.hideturtle() 隐藏画笔的turtle形状
turtle.showturtle() 显示画笔的turtle形状
全局控制命令 解释
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始状态
turtle.undo() 撤销上一个turtle动作
stamp() 复制当前图形
turtle.write(s [, font=(“font-name”, font_size, “font_type”)]) 写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项
  1. 樱花树(动图)每次运行画的图不一样!
    今天你画树了吗?用Python就可以了!
    代码:
import turtle as T
import random
import time

# 画樱花的躯干(60,t)
def Tree(branch, t):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                t.color('snow')  # 白
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                t.color('snow')
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 2)
        else:
            t.color('sienna')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        t.forward(branch)
        a = 1.5 * random.random()
        t.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b, t)
        t.left(40 * a)
        Tree(branch - 10 * b, t)
        t.right(20 * a)
        t.up()
        t.backward(branch)
        t.down()

# 掉落的花瓣
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')  # 淡珊瑚色
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)

# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle()  # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')  # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()
  1. 随机飘落
    今天你画树了吗?用Python就可以了!
    代码:
from turtle import *
from random import *
from math import *


def tree(n, l):
    pd()  # 下笔
    # 阴影效果
    t = cos(radians(heading() + 45)) / 8 + 0.25
    pencolor(t, t, t)
    pensize(n / 3)
    forward(l)  # 画树枝

    if n > 0:
        b = random() * 15 + 10  # 右分支偏转角度
        c = random() * 15 + 10  # 左分支偏转角度
        d = l * (random() * 0.25 + 0.7)  # 下一个分支的长度
        # 右转一定角度,画右分支
        right(b)
        tree(n - 1, d)
        # 左转一定角度,画左分支
        left(b + c)
        tree(n - 1, d)

        # 转回来
        right(c)
    else:
        # 画叶子
        right(90)
        n = cos(radians(heading() - 45)) / 4 + 0.5
        pencolor(n, n * 0.8, n * 0.8)
        circle(3)
        left(90)

        # 添加0.3倍的飘落叶子
        if (random() > 0.7):
            pu()
            # 飘落
            t = heading()
            an = -40 + random() * 40
            setheading(an)
            dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)
            forward(dis)
            setheading(t)

            # 画叶子
            pd()
            right(90)
            n = cos(radians(heading() - 45)) / 4 + 0.5
            pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)
            circle(2)
            left(90)
            pu()

            # 返回
            t = heading()
            setheading(an)
            backward(dis)
            setheading(t)

    pu()
    backward(l)  # 退回


bgcolor(0.5, 0.5, 0.5)  # 背景色
ht()  # 隐藏turtle
speed(0)  # 速度,1-10渐进,0最快
# tracer(0, 0)  # 这一行决定是否动态
pu()  # 抬笔
backward(100)
left(90)  # 左转90度
pu()  # 抬笔
backward(300)  # 后退300
tree(12, 100)  # 递归7层
done()
  1. 玫瑰
    今天你画树了吗?用Python就可以了!
    代码:
#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
Created on  sep 17, 2018
@author: stormwen
'''
import turtle

# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)

# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill()

# 花瓣1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60)
turtle.circle(80, 98)
turtle.circle(-90, 40)

# 花瓣2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)

# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()

turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)

# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()

turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)
turtle.done()
  1. 彩色螺旋线
    今天你画树了吗?用Python就可以了!
    代码:
import turtle
import time

turtle.pensize(2)
turtle.bgcolor("black")
colors = ["red", "yellow", 'purple', 'blue']
turtle.tracer(False)
for x in range(400):
    turtle.forward(2 * x)
    turtle.color(colors[x % 4])
    turtle.left(89)  # 变换数字会有不同的效果
turtle.tracer(True)
turtle.done()
  1. 圣诞树
    今天你画树了吗?用Python就可以了!
    代码:
from turtle import *
import random
import time

n = 100.0

speed("fastest")
screensize(bg='seashell')
left(90)
forward(3 * n)
color("orange", "yellow")
begin_fill()
left(126)

for i in range(5):
    forward(n / 5)
    right(144)
    forward(n / 5)
    left(72)
end_fill()
right(126)

color("dark green")
backward(n * 4.8)


def tree(d, s):
    if d <= 0: return
    forward(s)
    tree(d - 1, s * .8)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    backward(s)


tree(15, n)
backward(n / 2)

for i in range(200):
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    up()
    forward(b)
    left(90)
    forward(a)
    down()
    if random.randint(0, 1) == 0:
        color('tomato')
    else:
        color('wheat')
    circle(2)
    up()
    backward(a)
    right(90)
    backward(b)

time.sleep(60)
  1. 小猪佩奇
    今天你画树了吗?用Python就可以了!
    代码:
from turtle import *


def nose(x, y):
    """画鼻子"""
    pensize(5)
    pencolor((255, 155, 192))
    penup()
    # 将海龟移动到指定的坐标
    goto(x, y)
    pendown()
    # 设置海龟的方向(0-东、90-北、180-西、270-南)
    setheading(-30)
    begin_fill()
    fillcolor(255, 192, 203)
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.08
            left(3)
            forward(a)
    end_fill()
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    # 设置画笔的颜色(红, 绿, 蓝)
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()
    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()


def head(x, y):
    """画头"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300, -30)
    circle(100, -60)
    circle(80, -100)
    circle(150, -20)
    circle(60, -95)
    setheading(161)
    circle(-300, 15)
    penup()
    goto(-100, 100)
    pendown()
    setheading(-30)
    a = 0.4
    for i in range(60):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            lt(3)  # 向左转3度
            fd(a)  # 向前走a的步长
        else:
            a = a - 0.08
            lt(3)
            fd(a)
    end_fill()


def ears(x, y):
    """画耳朵"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(90)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 56)
    end_fill()


def eyes(x, y):
    """画眼睛"""
    color((255, 155, 192), "white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    color((255, 155, 192), "white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()


def cheek(x, y):
    """画脸颊"""
    color((255, 155, 192))
    penup()
    goto(x, y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()


def mouth(x, y):
    """画嘴巴"""
    color(239, 69, 19)
    penup()
    goto(x, y)
    pendown()
    setheading(-80)
    circle(30, 40)
    circle(40, 80)


def body(x, y):
    '''画身体'''
    penup()
    goto(x, y)
    pencolor('red')
    fillcolor(250, 106, 106)
    pendown()
    begin_fill()
    setheading(-66)
    circle(-450, 17)
    setheading(180)
    forward(185)
    setheading(85)
    circle(-450, 17)
    end_fill()
    '''右手'''
    penup()
    goto(110, -45)
    pendown()
    pensize(8)
    pencolor(255, 192, 203)
    setheading(30)
    circle(-400, 10)
    penup()
    goto(167, -5)
    pendown()
    setheading(-120)
    forward(20)
    left(100)
    forward(20)
    '''左手'''
    penup()
    goto(-25, -45)
    pendown()
    pencolor(255, 192, 203)
    setheading(150)
    circle(400, 10)
    penup()
    goto(-78, -6)
    pendown()
    setheading(-60)
    forward(20)
    right(100)
    forward(20)


def feet1(x, y):
    pensize(7)
    pencolor(255, 192, 203)
    penup()
    goto(x, y)
    setheading(-90)
    pendown()
    forward(10)
    penup()
    goto(x - 12, y - 10)
    pendown()
    pencolor(238, 201, 0)
    fillcolor(238, 230, 132)
    begin_fill()
    setheading(0)
    forward(24)
    right(90)
    forward(36)
    right(90)
    forward(40)
    circle(-10, 180)
    forward(16)
    left(90)
    forward(12)
    end_fill()


def feet2(x, y):
    pensize(7)
    pencolor(255, 192, 203)
    penup()
    goto(x, y)
    setheading(-90)
    pendown()
    forward(10)
    penup()
    goto(x - 12, y - 10)
    pendown()
    pencolor(238, 201, 0)
    fillcolor(238, 230, 132)
    begin_fill()
    setheading(0)
    forward(24)
    right(90)
    forward(36)
    right(90)
    forward(40)
    circle(-10, 180)
    forward(16)
    left(90)
    forward(12)
    end_fill()


def tail(x, y):
    pensize(8)
    penup()
    goto(x, y)
    pendown()
    pencolor(255, 192, 203)
    setheading(-5)
    circle(30, 100)
    circle(10, 180)
    circle(20, 150)


def backg(x):
    penup()
    goto(-420, x)
    setheading(0)
    fillcolor(50, 205, 50)
    begin_fill()
    forward(840)
    right(90)
    forward(300)
    right(90)
    forward(840)
    right(90)
    forward(300)
    end_fill()
    setheading(0)
    fillcolor(0, 191, 255)
    begin_fill()
    forward(840)
    left(90)
    forward(600)
    left(90)
    forward(840)
    left(90)
    forward(600)
    end_fill()


def cloude1(x, y):
    """画云"""
    penup()
    goto(x, y)
    setheading(90)
    fillcolor(255, 255, 255)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.14
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.15
            left(3)
            forward(a)
    end_fill()


def cloude2(x, y):
    """画云"""
    penup()
    goto(x, y)
    setheading(90)
    fillcolor(255, 255, 255)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.15
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.13
            left(3)
            forward(a)
    end_fill()


def setting():
    """设置参数"""
    pensize(5)
    # 隐藏海龟
    hideturtle()
    colormode(255)
    color((255, 155, 192), "pink")
    setup(840, 700)
    speed(10)


def main():
    """主函数"""

    setting()
    backg(0)
    body(105, -20)
    nose(-100, 100)
    head(-69, 167)
    ears(0, 160)
    eyes(0, 140)
    cheek(80, 10)
    mouth(-20, 30)
    feet1(10, -150)
    feet2(90, -150)
    tail(130, -110)
    cloude1(-200, 200)
    cloude2(300, 300)
    done()


if __name__ == '__main__':
    main()
  1. 彩色光球
    今天你画树了吗?用Python就可以了!
    代码:
import turtle as t
import random

t.speed('fast')
t.hideturtle()
t.bgcolor('black')

i = 0
temp = ''
colors = ['blue', 'red', 'yellow', 'green', 'cyan', 'purple',
          'white', 'orange', 'brown']

while i < 180:
    rand_color = random.choice(colors)
    while rand_color == temp:
        rand_color = random.choice(colors)
    t.pencolor(rand_color)
    t.penup()
    t.goto(0, 0)
    t.forward(100)
    t.pendown()
    if i % 2 == 0:
        t.forward(300)
    else:
        t.forward(200)
    t.left(2)
    i += 1
    temp = rand_color
  1. 弹簧隧道
    今天你画树了吗?用Python就可以了!
    代码:
import turtle as t
import random

t.speed('fast')
t.hideturtle()
t.bgcolor('black')

i = 0
while i < 135:
    t.pencolor('cyan')
    t.penup()
    t.goto(0, 0)
    t.forward(200)
    t.pendown()
    t.circle(100)
    t.left(2)
    i += 1
  1. 画蛋糕
    今天你画树了吗?用Python就可以了!
    代码1:
# Layer Cake Challenge - www.101computing.net/layer-cake/
from turtle import *
from shapes import *  # 另写shapes.py

myPen = Turtle()
myPen.shape("turtle")
myPen.speed(10)
myPen.hideturtle() 
window = turtle.Screen()
window.bgcolor("#69D9FF")
y = -140

#Inititalise the dictionary
ingredients = {}
#Add items to the dictionary
ingredients["strawberry"]="pink"
ingredients["milk chocolate"]="#BF671F"
ingredients["matcha"]="#93c572"
ingredients["icing sugar"]="#FFFFFF"

### Now let's preview the layer cake

#let's draw the plate
draw_rectangle(turtle, "white", -150, y-10, +300, 10)

#Iterate through each layer of the list
draw_rectangle(myPen, ingredients["milk chocolate"], -120, y, 240, 30)
y+=30
draw_rectangle(myPen, ingredients["strawberry"], -120, y, 240, 35)
y+=35
addIcing(myPen, ingredients["icing sugar"],120,y)
y+=10
draw_rectangle(myPen, ingredients["milk chocolate"], -100, y, 200, 20)
y+=20
draw_rectangle(myPen, ingredients["strawberry"], -100, y, 200, 40)
y+=40
addIcing(myPen, ingredients["icing sugar"],100,y)
y+=10
draw_rectangle(myPen, ingredients["milk chocolate"], -70, y, 140, 24)
y+=24
draw_rectangle(myPen, ingredients["strawberry"], -70, y, 140, 36)
y+=36
addIcing(myPen, ingredients["icing sugar"],70,y)
y+=10
draw_rectangle(myPen, ingredients["matcha"], -4, y, 8, 60)
y+=65
draw_star(myPen, "white", 2, y, 10)

代码2:shapes.py

# This is a custom module we've made.  
# Modules are files full of code that you can import into your programs.
# This one teaches our turtle to draw various shapes.

import turtle
import math

def draw_circle(turtle, color, x, y, radius):
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(x,y)
	turtle.pendown()
	turtle.begin_fill()
	turtle.circle(radius)
    turtle.end_fill()
  
def draw_rectangle(turtle, color, x, y, width, height):
    turtle.hideturtle()
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(x,y)
    turtle.pendown()
    turtle.begin_fill()
    for i in range (2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
    
    turtle.end_fill()
    turtle.setheading(0)  
  
def draw_star(turtle, color, x, y, size):
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(x,y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.right(144)
    for i in range(5):
        turtle.forward(size)
        turtle.right(144)
        turtle.forward(size)
    turtle.end_fill()
    turtle.setheading(0)
  
def addIcing(turtle,color,x,y):
    turtle.penup()
    turtle.color(color)
    turtle.fillcolor(color)
    turtle.goto(-x-2,y+10)
    turtle.pendown()
    turtle.begin_fill()

    for z in range(-x-3,x+3):
        turtle.goto(z,y-10-10*math.cos((z/100)*2*math.pi))
  
    turtle.goto(x+3,y+10)
    turtle.goto(-x-3,y+10)
    turtle.end_fill()
  1. 贪吃蛇游戏
    今天你画树了吗?用Python就可以了!
    代码:
"""Snake, classic arcade game.
Excercises
1. How do you make the snake faster or slower?
2. How can you make the snake go around the edges?
3. How would you move the food?
4. Change the snake to respond to arrow keys.
"""

from turtle import *
from random import randrange
from freegames import square, vector

food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)

def change(x, y):
    "Change snake direction."
    aim.x = x
    aim.y = y

def inside(head):
    "Return True if head inside boundaries."
    return -200 < head.x < 190 and -200 < head.y < 190

def move():
    "Move snake forward one segment."
    head = snake[-1].copy()
    head.move(aim)

    if not inside(head) or head in snake:
        square(head.x, head.y, 9, 'red')
        update()
        return

    snake.append(head)

    if head == food:
        print('Snake:', len(snake))
        food.x = randrange(-15, 15) * 10
        food.y = randrange(-15, 15) * 10
    else:
        snake.pop(0)

    clear()

    for body in snake:
        square(body.x, body.y, 9, 'black')

    square(food.x, food.y, 9, 'green')
    update()
    ontimer(move, 100)

setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()
  1. 表白
    今天你画树了吗?用Python就可以了!
    代码:
import turtle
import math

t = turtle.pen()
t = turtle
t.up()
t.goto(0, 150)
t.down()
t.color('red')
t.begin_fill()
t.fillcolor('red')
t.speed(1)
t.left(45)
t.forward(150)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(250 + math.sqrt(2) * 100)
t.right(90)
t.speed(2)
t.forward(250 + 100 * math.sqrt(2))
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(150)
t.end_fill()
t.goto(-10, 0)
t.pencolor('white')
# L
t.pensize(10)
t.goto(-50, 0)
t.goto(-50, 80)
t.up()
# I
t.goto(-100, 0)
t.down()
t.goto(-160, 0)
t.goto(-130, 0)
t.goto(-130, 80)
t.goto(-160, 80)
t.goto(-100, 80)
t.up()
# O
t.goto(10, 25)
t.down()
t.right(45)
t.circle(25, extent=180)
t.goto(60, 55)
t.circle(25, extent=180)
t.goto(10, 25)
t.up()
t.goto(75, 80)
t.down()
t.goto(100, 0)
t.goto(125, 80)
t.up()
t.goto(180, 80)
t.down()
t.goto(140, 80)
t.goto(140, 0)
t.goto(180, 0)
t.up()
t.goto(180, 40)
t.down()
t.goto(140, 40)
# U
t.up()
t.goto(-40, -30)
t.down()
t.goto(-40, -80)
t.circle(40, extent=180)
t.goto(40, -30)
t.hideturtle()
  1. 小黄人
    今天你画树了吗?用Python就可以了!
    代码:
import turtle

t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255)
t.hideturtle()
t.speed(10)
t.penup()
t.pensize(4)
t.goto(100, 0)
t.pendown()
t.left(90)
t.color((0, 0, 0), (255, 255, 0))
# 身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100, 180)
t.forward(200)
t.circle(100, 180)
t.end_fill()
# 右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100, 200)
t.pendown()
t.right(100)
t.circle(500, 23)

t.pensize(3)
t.penup()
t.goto(0, 200)
t.pendown()
t.seth(270)
t.color("black", "white")
t.begin_fill()
t.circle(30)
t.end_fill()

t.penup()
t.goto(15, 200)
t.pendown()
t.color("black", "black")
t.begin_fill()
t.circle(15)
t.end_fill()

t.penup()
t.goto(35, 205)
t.color("black", "white")
t.begin_fill()
t.circle(5)
t.end_fill()
# 左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0, 200)
t.pendown()
t.seth(90)
t.color("black", "white")
t.begin_fill()
t.circle(30)
t.end_fill()

t.penup()
t.goto(-15, 200)
t.pendown()
t.color("black", "black")
t.begin_fill()
t.circle(15)
t.end_fill()

t.penup()
t.goto(-35, 205)
t.color("black", "white")
t.begin_fill()
t.circle(5)
t.end_fill()

# 嘴绘制上色
t.penup()
t.goto(-20, 100)
t.pendown()
t.seth(270)
t.color("black", "white")
t.begin_fill()
t.circle(20, 180)
t.left(90)
t.forward(40)
t.end_fill()

# 裤子绘制上色
t.penup()
t.goto(-100, 0)
t.pendown()
t.seth(0)
t.color("black", "blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100, 0)
t.circle(100, 180)
t.end_fill()

# 左裤子腰带
t.penup()
t.goto(-70, 20)
t.pendown()
t.color("black", "blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70, 30)
t.dot()

# 右裤腰带
t.penup()
t.goto(70, 20)
t.pendown()
t.color("black", "blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()

t.left(180)
t.goto(70, 30)

t.dot()

# 脚
t.penup()
t.goto(4, -100)
t.pendown()
t.seth(270)
t.color("black", "black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10, 180)
t.circle(400, 2)
t.seth(90)
t.forward(20)
t.goto(4, -100)
t.end_fill()

t.penup()
t.goto(-4, -100)
t.pendown()
t.seth(270)
t.color("black", "black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10, -225)
t.circle(400, -3)
t.seth(90)
t.forward(21)
t.goto(-4, -100)
t.end_fill()

# 左手
t.penup()
t.goto(-100, 50)
t.pendown()
t.seth(225)
t.color("black", "yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
# 右手
t.penup()
t.goto(100, 50)
t.pendown()
t.seth(315)
t.color("black", "yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()

#
t.penup()
t.goto(0, -100)
t.pendown()
t.forward(30)

#
t.penup()
t.goto(0, -20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10, 180)
t.right(90)
t.circle(10, 180)
t.forward(20)
t.end_fill()

#
t.penup()
t.color("black")
t.goto(-100, -20)
t.pendown()
t.circle(30, 90)

t.penup()
t.goto(100, -20)
t.pendown()
t.circle(30, -90)
# 头顶
t.penup()
t.goto(2, 300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100, 40)
t.end_fill()

t.penup()
t.goto(2, 300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100, 40)
t.end_fill()

最后推荐给大家一个有意思的网址:
https://turtletoy.net/turtle/browse/love/


turtle官方文档:https://docs.python.org/3/library/turtle.html


参考链接:

  1. https://www.zhihu.com/question/271643290
  2. https://www.zhihu.com/question/279662259
  3. https://zhuanlan.zhihu.com/p/90767333
  4. https://zhuanlan.zhihu.com/p/54346834
  5. https://zhuanlan.zhihu.com/p/60075373
  6. https://zhuanlan.zhihu.com/p/36609642
  7. https://blog.csdn.net/weixin_43943977/article/details/102691392
相关标签: turtle 趣味编程