Turtle库复习

简单摘抄一些说明

一、使用Python内嵌的Turtle模块。

Turtle是Python内嵌的绘制线、圆以及其他形状(包括文本)的图形模块。它很容易学习并且使用简单。

一个Turtle实际上是一个对象,在导入Turtle模块时,就创建了对象,然后,可以调用Turtle对象的各种方法完成各种不同的风骚操作。

二、Turtle模块用笔来绘制图形。

那么首先要选一只好看一点的笔,使用

  • turtle.pensize():设置画笔的宽度;
  • turtle.pencolor(); 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如”green”, “red”,也可以是RGB

  • 其中还有一种方法,这里再复习一下;
  • 一种为运动命令
  • 一种为画笔控制命令
  • 还有一种是全局控制命令
  • turtle绘制窗体

    turtle.setup(width,height,startx,starty)

    -setup()设置窗体大小和位置

    -4个参数中后两个可选

    一二两个参数是指窗口的长度与宽度

    startx指窗体的左上角的位置的坐标

  •  

     

turtle.goto(x, y)

让在任何位置的海龟,无论它在哪里,去到达某一个坐标位置。

turtle.fk(d)

往海龟的正前方向运行。

turtle.bk(d)

往海龟的反方向运行。

turtle.circle(r, angle)

以海龟当前位置左侧的某一个点为圆心进行曲线运行。

三、命令详解

3.1 turtle.circle(radius, extent=None, steps=None)

描述:以给定半径画圆

参数:

radius(半径):半径为正(负),表示圆心在画笔的左边(右边)画圆;

extent(弧度) (optional);

steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)。

 

举例:

circle(50) # 整圆;

circle(50,steps=3) # 三角形;

circle(120, 180) # 半圆

实操是最好的学习和复习的方法

下面两个表是控制笔的绘制状态的方法和移动Turtle的方法:

#正方形
import turtle

turtle.screensize(400,400)
turtle.penup()
turtle.goto(-350,250)
turtle.pendown()
turtle.pencolor('red')
turtle.begin_fill()
turtle.fillcolor('green')

for i in range(4):
    turtle.forward(80)
    turtle.left(90)
turtle.end_fill()

绘制正方形??

from turtle import *


# 设置笔刷宽度:
width(4)


# 前进:
forward(200)
# 右转90度:
right(90)


# 笔刷颜色:
pencolor('red')
forward(100)
right(90)


pencolor('green')
forward(200)
right(90)


pencolor('blue')
forward(100)
right(90)


# 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

# 矩形
turtle.penup()
turtle.goto(-200,250)
turtle.pendown()
turtle.pencolor('blue')
turtle.begin_fill()
turtle.fillcolor('blue')

for i in range(1,5):
    if i % 2 == 1:
        n = 120
    elif i % 2 == 0:
        n = 80
    turtle.forward(n)
    turtle.left(90)

turtle.end_fill()
turtle.penup()

#立体的正方形_极坐标


x = 0
y = 200
z = 80
turtle.goto (x, y)
turtle.pendown ()
turtle.pencolor ('black')
turtle.begin_fill ()
turtle.fillcolor ('black')
for i in range (4):
    turtle.forward (n)
    turtle.left (90)
turtle.end_fill ()
turtle.penup ()
turtle.goto (x, y + z)
turtle.pendown ()
turtle.fillcolor ('gray')
turtle.begin_fill ()
turtle.left (45)
turtle.forward (int (z * 0.6))  # 上方左侧斜线
turtle.right (45)
turtle.forward (z)  # 上方横线
turtle.left (360 - 135)
turtle.forward (int (z * 0.6))  ##上方右侧斜线
turtle.end_fill ()

turtle.left (45)
turtle.penup ()
turtle.goto (x + z, y)

turtle.pendown ()
turtle.left (135)
turtle.forward (int (z * 0.6))
turtle.left (45)
turtle.forward (z)
turtle.right (90)  # 方向还原,向左
turtle.penup ()


#当然,这里是复习,还是一个一个的敲比较好!!0...0

既然是复习,当然是看官网的

官网

 

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 drawSnake(rad, angle, len, neckrad):

    for _ in range(len):
        turtle.circle(rad, angle)
        turtle.circle(-rad, angle)

    turtle.circle(rad, angle/2)
    turtle.forward(rad/2)  # 直线前进
    turtle.circle(neckrad, 180)
    turtle.forward(rad/4)

if __name__ == "__main__":

    turtle.setup(1500, 1400, 0, 0)
    turtle.pensize(30)  # 画笔尺寸
    turtle.pencolor("green")
    turtle.seth(-40)    # 前进的方向
    drawSnake(70, 80, 2, 15)
    turtle.write ('by scriptchild', font=("Bradley Hand ITC", 30, "bold"))
    #添加自己的代号、、、
    turtle.mainloop ()



#个性一点,虽然是复习,但还是要骚起来
小蛇

 

 

 

 

import turtle
import time


turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")

turtle.begin_fill()

for _ in range(5):
    turtle.forward(200)
    turtle.right(144)
turtle.end_fill()
time.sleep(2)

turtle.penup()
turtle.goto(-150,-120)
turtle.color("violet")
turtle.write ('by scriptchild', font=("Bradley Hand ITC", 30, "bold"))
time.sleep(5)
骚气的五角星

 

 

 

 

from turtle import *
import time

setup (600, 800, 0, 0)
speed (0)
penup ()
seth (90)
fd (340)
seth (0)
pendown ()

speed (5)
begin_fill ()
fillcolor ('red')
circle (50, 30)

for i in range (10):
    fd (1)
    left (10)

circle (40, 40)

for i in range (6):
    fd (1)
    left (3)

circle (80, 40)

for i in range (20):
    fd (0.5)
    left (5)

circle (80, 45)

for i in range (10):
    fd (2)
    left (1)

circle (80, 25)

for i in range (20):
    fd (1)
    left (4)

circle (50, 50)

time.sleep (0.1)

circle (120, 55)

speed (0)

seth (-90)
fd (70)

right (150)
fd (20)

left (140)
circle (140, 90)

left (30)
circle (160, 100)

left (130)
fd (25)

penup ()
right (150)
circle (40, 80)
pendown ()

left (115)
fd (60)

penup ()
left (180)
fd (60)
pendown ()

end_fill ()

right (120)
circle (-50, 50)
circle (-20, 90)

speed (1)
fd (75)

speed (0)
circle (90, 110)

penup ()
left (162)
fd (185)
left (170)
pendown ()
circle (200, 10)
circle (100, 40)
circle (-52, 115)
left (20)
circle (100, 20)
circle (300, 20)
speed (1)
fd (250)

penup ()
speed (0)
left (180)
fd (250)
circle (-300, 7)
right (80)
circle (200, 5)
pendown ()

left (60)
begin_fill ()
fillcolor ('green')
circle (-80, 100)
right (90)
fd (10)
left (20)
circle (-63, 127)
end_fill ()

penup ()
left (50)
fd (20)
left (180)

pendown ()
circle (200, 25)

penup ()
right (150)

fd (180)

right (40)
pendown ()
begin_fill ()
fillcolor ('green')
circle (-100, 80)
right (150)
fd (10)
left (60)
circle (-80, 98)
end_fill ()

penup ()
left (60)
fd (13)
left (180)

pendown ()
speed (1)
circle (-200, 23)

exitonclick ()
爱情的玫瑰

 

 

 

 

from turtle import *

colormode(255)

lt(90)

lv = 14
l = 120
s = 45

width(lv)

r = 0
g = 0
b = 0
pencolor(r, g, b)

penup()
bk(l)
pendown()
fd(l)

def draw_tree(l, level):
    global r, g, b
    # save the current pen width
    w = width()

    # narrow the pen width
    width(w * 3.0 / 4.0)
    # set color:
    r = r + 1
    g = g + 2
    b = b + 3
    pencolor(r % 200, g % 200, b % 200)

    l = 3.0 / 4.0 * l

    lt(s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    rt(2 * s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    lt(s)

    # restore the previous pen width
    width(w)

speed("fastest")

draw_tree(l, 4)

done()

# 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

#此篇参考廖雪峰的个人博客,有介绍
骚气的树

 

 

 

 

from  turtle import *
from datetime import *


def Skip(step):
    penup()
    forward(step)
    pendown()

def mkHand(name,length):

    reset()
    Skip(-length *0.1)
    begin_poly ()
    forward(length * 1.1)
    end_poly()
    handForm = get_poly()
    register_shape(name,handForm)

def Init():
    global secHand,minHand ,hurHand,printer
    mode ("logo")
    mkHand("secHand",135)
    mkHand('minHand',110)
    mkHand('hurHand',90)
    secHand = Turtle()
    secHand.shape('secHand')
    minHand = Turtle()
    minHand.shape('minHand')
    hurHand = Turtle()
    hurHand.shape('hurHand')

    for hand in secHand,minHand,hurHand:
        hand.shapesize(1,1,3)
        hand.speed(0)
    printer = Turtle()
    printer.hideturtle()
    printer.penup()

def SetupClock(radius):
    reset()
    pensize(7)
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            forward(20)
            Skip(-radius - 20 )
        else:
            dot(5)
            Skip(-radius)
        right(6)

def Week(t):
    week = ['星期一','星期二','星期三','星期四','星期五','星期六','星期天',]
    return week[t.weekday()]

def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s - %d - %d" % (y,m,d )

def Tick():
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second/60.0
    hour = t.hour + minute/60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    hurHand.setheading(30 * hour)

    tracer(False)

    printer.forward(65)
    printer.write(Week(t),align = 'center',font=('Courier',14,'bold'))
    printer.back(130)
    printer.write(Date(t),align = 'center',font=('Courier',14,'bold'))
    printer.back(50)
    printer.write('SCript',align = 'center',font=('Courier',14,'bold'))

    printer.home()
    tracer(True)

    ontimer(Tick,1000)

def main():
    tracer(False)
    Init()
    SetupClock(160)
    tracer(True)
    Tick()
    mainloop()

if __name__ == '__main__':
    main()



#写法上有很大的不一样,也有很大的改正空间
送一口钟

 

 

 

 

# 绘制小猪佩奇
# =======================================
import turtle as t
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), "pink")
t.setup(840, 500)
t.speed(10)

# 鼻子
t.pu()
t.goto(-100,100)
t.pd()
t.seth(-30)
t.begin_fill()
a = 0.4
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a = a+0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
        t.end_fill()

t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()

t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()

#
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
    if 0 <= i < 30 or 60 <= i <90:
        a = a+0.08
        t.lt(3)  # 向左转3度
        t.fd(a)  # 向前走a的步长
    else:
        a = a-0.08
        t.lt(3)
        t.fd(a)
        t.end_fill()

# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()

t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()

#眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()

t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()

t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()

#
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()

#
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)

# 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100,10)
t.circle(300,30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300,30)
t.circle(100,3)
t.color((255,155,192),(255,100,100))
t.seth(-135)
t.circle(-80,63)
t.circle(-150,24)
t.end_fill()

#
t.color((255,155,192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300,15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20,90)

t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300,15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20,90)

#
t.pensize(10)
t.color((240,128,128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)

# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done()
猪哥

 

 

 

 

这不马上快2020了嘛,在想一个“小猪佩奇祝福2020新春”内容就简单上了——2020新年快乐!

思路:猪哥+2020新年快乐字样

后续添加:当猪哥和字样显示完毕,放鞭炮(烟花)

 

控制笔的绘制状态和移动笔

import turtle
 
turtle.pendown()           
turtle.penup()             
turtle.pensize(width)      
 
turtle.forward(d)           # Moves the turtle forward by the specified distance in the direction the turtle is headed.
turtle.backward(d)          # Moves the turtle backward by the specified distance in the opposite direction the turtle is headed. The turtle's direction is not changed.
turtle.right(angle)         # Turns the turtle right by the specified angle.
turtle.left(angle)          # Turns the turtle left by the specified angle.
turtle.goto(x,y)            # Moves the turtle to an absolute position.
turtle.setx(x)              # Moves the turtle's x-coordinate to the specified position.
turtle.sety(y)              # Moves the turtle's y-coordinate to the specified position.
turtle.setheading(angle)    # Sets the orientation of the turtle to a specified angle. 0-East, 90-North, 180-West, 270-South.
turtle.home()               # Moves the turtle to the origin (0,0) and east direction.
turtle.circle(r, ext, step) # Draws a circle with the specified radius, extent, and step.
turtle.dot(diameter, color) # Draws a circle with the specified diameter and color.
turtle.undo()               # Undo(repeatedly) the last turtle action(s).
turtle.speed(s)             # Sets the turtle's speed to an integer between 1 and 10, with 10 being the fastest.

 

(1)画笔运动命令:

commanddescription
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() 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

(2)画笔控制命令:

commanddescription
turtle.pensize(width) 绘制图形时的宽度
turtle.pencolor() 画笔颜色
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.color(color1, color2) 同时设置pencolor=color1, fillcolor=color2
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 准备开始填充图形
turtle.end_fill() 填充完成;
turtle.hideturtle() 隐藏箭头显示;
turtle.showturtle() 与hideturtle()函数对应

(3) 全局控制命令

commanddescription
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始状态
turtle.undo() 撤销上一个turtle动作
turtle.isvisible() 返回当前turtle是否可见
stamp() 复制当前图形
turtle.write(s[,font=(“font-name”,font_size,”font_type”)]) 写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项

函数

描述

pendown()

放下画笔

penup()

提起画笔,与pendown()配对使用

pensize(width)

设置画笔线条的粗细为指定大小

color()

设置画笔的颜色

begin_fill()

填充图形前,调用该方法

end_fill()

填充图形结束

filling()

返回填充的状态,True为填充,False为未填充

clear()

清空当前窗口,但不改变当前画笔的位置

reset()

清空当前窗口,并重置位置等状态为默认值

screensize()

设置画布的长和宽

hideturtle()

隐藏画笔的turtle形状

showturtle()

显示画笔的turtle形状

isvisible()

如果turtle可见,则返回True

write(str,font=None)

输出font字体的字符串

 

想画一下叮当猫

from turtle import *


# 无轨迹跳跃
def my_goto(x, y):
    penup()
    goto(x, y)
    pendown()

# 眼睛
def eyes():
    fillcolor("#ffffff")
    begin_fill()

    tracer(False)
    a = 2.5
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a -= 0.05
            lt(3)
            fd(a)
        else:
            a += 0.05
            lt(3)
            fd(a)
    tracer(True)
    end_fill()


# 胡须
def beard():
    my_goto(-32, 135)
    seth(165)
    fd(60)

    my_goto(-32, 125)
    seth(180)
    fd(60)

    my_goto(-32, 115)
    seth(193)
    fd(60)

    my_goto(37, 135)
    seth(15)
    fd(60)

    my_goto(37, 125)
    seth(0)
    fd(60)

    my_goto(37, 115)
    seth(-13)
    fd(60)

# 嘴巴
def mouth():
    my_goto(5, 148)
    seth(270)
    fd(100)
    seth(0)
    circle(120, 50)
    seth(230)
    circle(-120, 100)

# 围巾
def scarf():
    fillcolor('#e70010')
    begin_fill()
    seth(0)
    fd(200)
    circle(-5, 90)
    fd(10)
    circle(-5, 90)
    fd(207)
    circle(-5, 90)
    fd(10)
    circle(-5, 90)
    end_fill()

# 鼻子
def nose():
    my_goto(-10, 158)
    seth(315)
    fillcolor('#e70010')
    begin_fill()
    circle(20)
    end_fill()

# 黑眼睛
def black_eyes():
    seth(0)
    my_goto(-20, 195)
    fillcolor('#000000')
    begin_fill()
    circle(13)
    end_fill()

    pensize(6)
    my_goto(20, 205)
    seth(75)
    circle(-10, 150)
    pensize(3)

    my_goto(-17, 200)
    seth(0)
    fillcolor('#ffffff')
    begin_fill()
    circle(5)
    end_fill()
    my_goto(0, 0)



#
def face():

    fd(183)
    lt(45)
    fillcolor('#ffffff')
    begin_fill()
    circle(120, 100)
    seth(180)
    # print(pos())
    fd(121)
    pendown()
    seth(215)
    circle(120, 100)
    end_fill()
    my_goto(63.56,218.24)
    seth(90)
    eyes()
    seth(180)
    penup()
    fd(60)
    pendown()
    seth(90)
    eyes()
    penup()
    seth(180)
    fd(64)

# 头型
def head():
    penup()
    circle(150, 40)
    pendown()
    fillcolor('#00a0de')
    begin_fill()
    circle(150, 280)
    end_fill()

# 画哆啦A梦
def Doraemon():
    # 头部
    head()

    # 围脖
    scarf()

    #
    face()

    # 红鼻子
    nose()

    # 嘴巴
    mouth()

    # 胡须
    beard()

    # 身体
    my_goto(0, 0)
    seth(0)
    penup()
    circle(150, 50)
    pendown()
    seth(30)
    fd(40)
    seth(70)
    circle(-30, 270)


    fillcolor('#00a0de')
    begin_fill()

    seth(230)
    fd(80)
    seth(90)
    circle(1000, 1)
    seth(-89)
    circle(-1000, 10)

    # print(pos())

    seth(180)
    fd(70)
    seth(90)
    circle(30, 180)
    seth(180)
    fd(70)

    # print(pos())
    seth(100)
    circle(-1000, 9)

    seth(-86)
    circle(1000, 2)
    seth(230)
    fd(40)

    # print(pos())


    circle(-30, 230)
    seth(45)
    fd(81)
    seth(0)
    fd(203)
    circle(5, 90)
    fd(10)
    circle(5, 90)
    fd(7)
    seth(40)
    circle(150, 10)
    seth(30)
    fd(40)
    end_fill()

    # 左手
    seth(70)
    fillcolor('#ffffff')
    begin_fill()
    circle(-30)
    end_fill()

    #
    my_goto(103.74, -182.59)
    seth(0)
    fillcolor('#ffffff')
    begin_fill()
    fd(15)
    circle(-15, 180)
    fd(90)
    circle(-15, 180)
    fd(10)
    end_fill()

    my_goto(-96.26, -182.59)
    seth(180)
    fillcolor('#ffffff')
    begin_fill()
    fd(15)
    circle(15, 180)
    fd(90)
    circle(15, 180)
    fd(10)
    end_fill()

    # 右手
    my_goto(-133.97, -91.81)
    seth(50)
    fillcolor('#ffffff')
    begin_fill()
    circle(30)
    end_fill()

    # 口袋
    my_goto(-103.42, 15.09)
    seth(0)
    fd(38)
    seth(230)
    begin_fill()
    circle(90, 260)
    end_fill()

    my_goto(5, -40)
    seth(0)
    fd(70)
    seth(-90)
    circle(-70, 180)
    seth(0)
    fd(70)

    #铃铛
    my_goto(-103.42, 15.09)
    fd(90)
    seth(70)
    fillcolor('#ffd200')
    # print(pos())
    begin_fill()
    circle(-20)
    end_fill()
    seth(170)
    fillcolor('#ffd200')
    begin_fill()
    circle(-2, 180)
    seth(10)
    circle(-100, 22)
    circle(-2, 180)
    seth(180-10)
    circle(100, 22)
    end_fill()
    goto(-13.42, 15.09)
    seth(250)
    circle(20, 110)
    seth(90)
    fd(15)
    dot(10)
    my_goto(0, -150)

    # 画眼睛
    black_eyes()

if __name__ == '__main__':
    screensize(800,600, "#f0f0f0")
    # pencolor('brown')   #控制画笔颜色
    pensize(3)  # 画笔宽度
    speed(9)    # 画笔速度
    Doraemon()
    my_goto(100, -300)
    write('by scriptchild', font=("Bradley Hand ITC", 30, "bold"))
    mainloop()
叮当猫

 

 

 

 

想画一下皮卡丘

import turtle


def getPosition(x, y):
    turtle.setx (x)
    turtle.sety (y)
    print (x, y)


class Pikachu:

    def __init__(self):
        self.t = turtle.Turtle ()
        t = self.t
        t.pensize (3)
        t.speed (9)
        t.ondrag (getPosition)

    def noTrace_goto(self, x, y):
        self.t.penup ()
        self.t.goto (x, y)
        self.t.pendown ()

    def leftEye(self, x, y):
        self.noTrace_goto (x, y)
        t = self.t
        t.seth (0)
        t.fillcolor ('#333333')
        t.begin_fill ()
        t.circle (22)
        t.end_fill ()

        self.noTrace_goto (x, y + 10)
        t.fillcolor ('#000000')
        t.begin_fill ()
        t.circle (10)
        t.end_fill ()

        self.noTrace_goto (x + 6, y + 22)
        t.fillcolor ('#ffffff')
        t.begin_fill ()
        t.circle (10)
        t.end_fill ()

    def rightEye(self, x, y):
        self.noTrace_goto (x, y)
        t = self.t
        t.seth (0)
        t.fillcolor ('#333333')
        t.begin_fill ()
        t.circle (22)
        t.end_fill ()

        self.noTrace_goto (x, y + 10)
        t.fillcolor ('#000000')
        t.begin_fill ()
        t.circle (10)
        t.end_fill ()

        self.noTrace_goto (x - 6, y + 22)
        t.fillcolor ('#ffffff')
        t.begin_fill ()
        t.circle (10)
        t.end_fill ()

    def mouth(self, x, y):
        self.noTrace_goto (x, y)
        t = self.t

        t.fillcolor ('#88141D')
        t.begin_fill ()
        # 下嘴唇
        l1 = []
        l2 = []
        t.seth (190)
        a = 0.7
        for i in range (28):
            a += 0.1
            t.right (3)
            t.fd (a)
            l1.append (t.position ())

        self.noTrace_goto (x, y)

        t.seth (10)
        a = 0.7
        for i in range (28):
            a += 0.1
            t.left (3)
            t.fd (a)
            l2.append (t.position ())

        # 上嘴唇

        t.seth (10)
        t.circle (50, 15)
        t.left (180)
        t.circle (-50, 15)

        t.circle (-50, 40)
        t.seth (233)
        t.circle (-50, 55)
        t.left (180)
        t.circle (50, 12.1)
        t.end_fill ()

        # 舌头
        self.noTrace_goto (17, 54)
        t.fillcolor ('#DD716F')
        t.begin_fill ()
        t.seth (145)
        t.circle (40, 86)
        t.penup ()
        for pos in reversed (l1[:20]):
            t.goto (pos[0], pos[1] + 1.5)
        for pos in l2[:20]:
            t.goto (pos[0], pos[1] + 1.5)
        t.pendown ()
        t.end_fill ()

        # 鼻子
        self.noTrace_goto (-17, 94)
        t.seth (8)
        t.fd (4)
        t.back (8)

    # 红脸颊
    def leftCheek(self, x, y):
        turtle.tracer (False)
        t = self.t
        self.noTrace_goto (x, y)
        t.seth (300)
        t.fillcolor ('#DD4D28')
        t.begin_fill ()
        a = 2.3
        for i in range (120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
                t.lt (3)
                t.fd (a)
            else:
                a += 0.05
                t.lt (3)
                t.fd (a)
        t.end_fill ()
        turtle.tracer (True)

    def rightCheek(self, x, y):
        t = self.t
        turtle.tracer (False)
        self.noTrace_goto (x, y)
        t.seth (60)
        t.fillcolor ('#DD4D28')
        t.begin_fill ()
        a = 2.3
        for i in range (120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
                t.lt (3)
                t.fd (a)
            else:
                a += 0.05
                t.lt (3)
                t.fd (a)
        t.end_fill ()
        turtle.tracer (True)

    def colorLeftEar(self, x, y):
        t = self.t
        self.noTrace_goto (x, y)
        t.fillcolor ('#000000')
        t.begin_fill ()
        t.seth (330)
        t.circle (100, 35)
        t.seth (219)
        t.circle (-300, 19)
        t.seth (110)
        t.circle (-30, 50)
        t.circle (-300, 10)
        t.end_fill ()

    def colorRightEar(self, x, y):
        t = self.t
        self.noTrace_goto (x, y)
        t.fillcolor ('#000000')
        t.begin_fill ()
        t.seth (300)
        t.circle (-100, 30)
        t.seth (35)
        t.circle (300, 15)
        t.circle (30, 50)
        t.seth (190)
        t.circle (300, 17)
        t.end_fill ()

    def body(self):
        t = self.t

        t.fillcolor ('#F6D02F')
        t.begin_fill ()
        # 右脸轮廓
        t.penup ()
        t.circle (130, 40)
        t.pendown ()
        t.circle (100, 105)
        t.left (180)
        t.circle (-100, 5)

        # 右耳朵
        t.seth (20)
        t.circle (300, 30)
        t.circle (30, 50)
        t.seth (190)
        t.circle (300, 36)

        # 上轮廓
        t.seth (150)
        t.circle (150, 70)

        # 左耳朵
        t.seth (200)
        t.circle (300, 40)
        t.circle (30, 50)
        t.seth (20)
        t.circle (300, 35)
        # print(t.pos())

        # 左脸轮廓
        t.seth (240)
        t.circle (105, 95)
        t.left (180)
        t.circle (-105, 5)

        # 左手
        t.seth (210)
        t.circle (500, 18)
        t.seth (200)
        t.fd (10)
        t.seth (280)
        t.fd (7)
        t.seth (210)
        t.fd (10)
        t.seth (300)
        t.circle (10, 80)
        t.seth (220)
        t.fd (10)
        t.seth (300)
        t.circle (10, 80)
        t.seth (240)
        t.fd (12)
        t.seth (0)
        t.fd (13)
        t.seth (240)
        t.circle (10, 70)
        t.seth (10)
        t.circle (10, 70)
        t.seth (10)
        t.circle (300, 18)

        t.seth (75)
        t.circle (500, 8)
        t.left (180)
        t.circle (-500, 15)
        t.seth (250)
        t.circle (100, 65)

        # 左脚
        t.seth (320)
        t.circle (100, 5)
        t.left (180)
        t.circle (-100, 5)
        t.seth (220)
        t.circle (200, 20)
        t.circle (20, 70)

        t.seth (60)
        t.circle (-100, 20)
        t.left (180)
        t.circle (100, 20)
        t.seth (300)
        t.circle (10, 70)

        t.seth (60)
        t.circle (-100, 20)
        t.left (180)
        t.circle (100, 20)
        t.seth (10)
        t.circle (100, 60)

        # 横向
        t.seth (180)
        t.circle (-100, 10)
        t.left (180)
        t.circle (100, 10)
        t.seth (5)
        t.circle (100, 10)
        t.circle (-100, 40)
        t.circle (100, 35)
        t.left (180)
        t.circle (-100, 10)

        # 右脚
        t.seth (290)
        t.circle (100, 55)
        t.circle (10, 50)

        t.seth (120)
        t.circle (100, 20)
        t.left (180)
        t.circle (-100, 20)

        t.seth (0)
        t.circle (10, 50)

        t.seth (110)
        t.circle (100, 20)
        t.left (180)
        t.circle (-100, 20)

        t.seth (30)
        t.circle (20, 50)

        t.seth (100)
        t.circle (100, 40)

        # 右侧身体轮廓
        t.seth (200)
        t.circle (-100, 5)
        t.left (180)
        t.circle (100, 5)
        t.left (30)
        t.circle (100, 75)
        t.right (15)
        t.circle (-300, 21)
        t.left (180)
        t.circle (300, 3)

        # 右手
        t.seth (43)
        t.circle (200, 60)

        t.right (10)
        t.fd (10)

        t.circle (5, 160)
        t.seth (90)
        t.circle (5, 160)
        t.seth (90)

        t.fd (10)
        t.seth (90)
        t.circle (5, 180)
        t.fd (10)

        t.left (180)
        t.left (20)
        t.fd (10)
        t.circle (5, 170)
        t.fd (10)
        t.seth (240)
        t.circle (50, 30)

        t.end_fill ()
        self.noTrace_goto (130, 125)
        t.seth (-20)
        t.fd (5)
        t.circle (-5, 160)
        t.fd (5)

        # 手指纹
        self.noTrace_goto (166, 130)
        t.seth (-90)
        t.fd (3)
        t.circle (-4, 180)
        t.fd (3)
        t.seth (-90)
        t.fd (3)
        t.circle (-4, 180)
        t.fd (3)

        # 尾巴
        self.noTrace_goto (168, 134)
        t.fillcolor ('#F6D02F')
        t.begin_fill ()
        t.seth (40)
        t.fd (200)
        t.seth (-80)
        t.fd (150)
        t.seth (210)
        t.fd (150)
        t.left (90)
        t.fd (100)
        t.right (95)
        t.fd (100)
        t.left (110)
        t.fd (70)
        t.right (110)
        t.fd (80)
        t.left (110)
        t.fd (30)
        t.right (110)
        t.fd (32)

        t.right (106)
        t.circle (100, 25)
        t.right (15)
        t.circle (-300, 2)
        ##############
        # print(t.pos())
        t.seth (30)
        t.fd (40)
        t.left (100)
        t.fd (70)
        t.right (100)
        t.fd (80)
        t.left (100)
        t.fd (46)
        t.seth (66)
        t.circle (200, 38)
        t.right (10)
        t.fd (10)
        t.end_fill ()

        # 尾巴花纹
        t.fillcolor ('#923E24')
        self.noTrace_goto (126.82, -156.84)
        t.begin_fill ()

        t.seth (30)
        t.fd (40)
        t.left (100)
        t.fd (40)
        t.pencolor ('#923e24')
        t.seth (-30)
        t.fd (30)
        t.left (140)
        t.fd (20)
        t.right (150)
        t.fd (20)
        t.left (150)
        t.fd (20)
        t.right (150)
        t.fd (20)
        t.left (130)
        t.fd (18)
        t.pencolor ('#000000')
        t.seth (-45)
        t.fd (67)
        t.right (110)
        t.fd (80)
        t.left (110)
        t.fd (30)
        t.right (110)
        t.fd (32)
        t.right (106)
        t.circle (100, 25)
        t.right (15)
        t.circle (-300, 2)
        t.end_fill ()

        # 帽子、眼睛、嘴巴、脸颊
        self.cap (-134.07, 147.81)
        self.mouth (-5, 25)
        self.leftCheek (-126, 32)
        self.rightCheek (107, 63)
        self.colorLeftEar (-250, 100)
        self.colorRightEar (140, 270)
        self.leftEye (-85, 90)
        self.rightEye (50, 110)
        t.hideturtle ()

    def cap(self, x, y):
        self.noTrace_goto (x, y)
        t = self.t
        t.fillcolor ('#CD0000')
        t.begin_fill ()
        t.seth (200)
        t.circle (400, 7)
        t.left (180)
        t.circle (-400, 30)
        t.circle (30, 60)
        t.fd (50)
        t.circle (30, 45)
        t.fd (60)
        t.left (5)
        t.circle (30, 70)
        t.right (20)
        t.circle (200, 70)
        t.circle (30, 60)
        t.fd (70)
        # print(t.pos())
        t.right (35)
        t.fd (50)
        t.circle (8, 100)
        t.end_fill ()
        self.noTrace_goto (-168.47, 185.52)
        t.seth (36)
        t.circle (-270, 54)
        t.left (180)
        t.circle (270, 27)
        t.circle (-80, 98)

        t.fillcolor ('#444444')
        t.begin_fill ()
        t.left (180)
        t.circle (80, 197)
        t.left (58)
        t.circle (200, 45)
        t.end_fill ()

        self.noTrace_goto (-58, 270)
        t.pencolor ('#228B22')
        t.dot (35)

        self.noTrace_goto (-30, 280)
        t.fillcolor ('#228B22')
        t.begin_fill ()
        t.seth (100)
        t.circle (30, 180)
        t.seth (190)
        t.fd (15)
        t.seth (100)
        t.circle (-45, 180)
        t.right (90)
        t.fd (15)
        t.end_fill ()
        t.pencolor ('#000000')

    def start(self):
        self.body ()


def main():
    print ('Painting the Pikachu... ')
    turtle.screensize (800, 600)
    turtle.title ('皮卡丘')
    turtle.write ('by scriptchild', font=("Bradley Hand ITC", 30, "bold"))
    pikachu = Pikachu ()
    pikachu.start ()
    turtle.mainloop ()


if __name__ == '__main__':
    main ()
皮卡丘

 

 

 

 

 没有不忘的人,没有不忘的操作,只有“骚”陪伴着我

实操还是要参考官网。。

posted @ 2019-03-12 14:01  脚本小孩  阅读(733)  评论(0)    收藏  举报