利用turtle库,主要调用goto()函数、circle()函数和改变角度的left()函数,通过不断地尝试,最终实现QQ表情“蹭一蹭”的绘制,代码如下。
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 21:34:21 2018
@author: 魅梦
"""
from turtle import *
def drawcircle(x, y, r, angle, size):
pensize(size)
up()
goto(x, y)
down()
begin_fill()
circle(r, angle)
color("black", "white")
end_fill()
def draweye(x, y, startangle, changeangle, step, stepplus, times):
pensize(3)
up()
goto(x, y)
down()
setheading(startangle)
a = step
for i in range(times):
a = a + stepplus
left(changeangle)
forward(a)
#Turtle().screen.delay(0)
hideturtle()
drawcircle(0, 0, 45, 360, 2)
drawcircle(20, -1, 15, 360, 2)
drawcircle(-5, -1, 15, 360, 2)
draweye(-15, 70, -30, 3, 0.1, 0.03, 25)
draweye(-20, 58, -30, 2, 0.1, 0.03, 35)
draweye(22, 75, -75, 3, 0.1, 0.02, 30)
draweye(17, 65, -75, 2, 0.1, 0.02, 40)
setheading(90)
drawcircle(0, 55, -5, -180, 3)
setheading(90)
drawcircle(10, 55, -5, -180, 3)
begin_fill()
draweye(4, 50, -100, 0.8, 0.05, 0.01, 55)
draweye(16, 50, -80, -0.8, 0.05, 0.01, 55)
color("black", "red")
end_fill()
setheading(90)
drawcircle(0, 55, -5, -180, 3)
setheading(90)
drawcircle(10, 55, -5, -180, 3)
pensize(8)
setheading(5)
up()
goto(-25, 50)
down()
begin_fill()
color("pink", "pink")
forward(15)
end_fill()
up()
goto(30, 50)
down()
begin_fill()
color("pink", "pink")
forward(10)
end_fill()
绘制效果如下图。
接下来实现更简单的“I LOVE U”字母的绘制,代码如下。
#<程序:绘制LOVE>
from turtle import *
def jumpto(x,y):
up();goto(x,y);down()
def go():
hideturtle()
jumpto(-190-60,50)
forward(60)
jumpto(-160-60,50)
right(90)
forward(100)
left(90)
jumpto(-190-60,-50)
forward(60)
jumpto(-100,50)
forward(0)
right(90)
forward(100)
left(90)
forward(60)
jumpto(-30,50)
forward(60)
right(90)
forward(100)
right(90)
forward(60)
right(90)
forward(100)
jumpto(40,50)
forward(0)
right(160.71)
forward(105.9)
left(141.42)
forward(105.9)
jumpto(120,50)
right(160.71)
forward(100)
left(90)
forward(60)
jumpto(120,0)
forward(60)
jumpto(120,50)
forward(60)
jumpto(270,50)
right(90)
forward(75)
circle(30,180)
forward(75)
right(90)
color("red")
width(8)
go()
color("yellow")
width(6)
go()
color("red")
width(4)
go()
color("yellow")
width(2)
go()
color("red")
width(0)
go()
s=Screen();s.exitonclick()
绘制效果可自行进行尝试,这里就不做展示了。接下来还有一个通过循环绘制的烟花图像,首先将画笔颜色设置为白色,填充颜色设置为黑色,通过循环实现画笔的绘制,但此时看不到任何图形,直到调用end_fill()开始填充,这时屏幕上就突然呈现出烟花的图像,然后将画笔颜色设置为白色,填充颜色也设置为白色,调用time.sleep(1)函数,经过一秒后执行clear()函数清空画布,实现烟花图像的消失,由于出现时间较为短暂,我将其命名为“惊鸿一现”。具体代码如下。
from turtle import *
import time
color('white','black')
begin_fill()
while True:
speed("fastest")
forward(115)
left(175)
if abs(pos())<1:
break
end_fill()
color('white','white')
time.sleep(1)
clear()
“惊鸿”现时的画面如下图。
在实践过程中你也可以尝试改变不同的画笔颜色与填充颜色从而实现更加绚丽的效果。