基本使用
导入turtle模块
使用海龟绘图首先我们需要导入turtle模块,如下所示:
import turtle
turtle基本用法
(1)turtle.showturtle()//显示箭头
(2)turtle.goto(x,y)//将海龟走到该坐标位置,x为横坐标,y为纵坐标
(3)turtle.circle(r,angle)//以r为半径画圆,角度为angle,r和rangle都可以为负数。例如:
turtle.circle(20,180)//画出以20为半径的右半圆
(4)turtle.left(angle)//左转,angle为度数
(5)turtle.right(angle)//右转,angle为度数
(6)penup()//抬起画笔
(7)pendown()//落下画笔
(8)color()//笔的颜色
(9)pensize()//笔的大小
(10)turtle.done() 画完之后不关闭窗口
(11)turtle.hideturtle() 隐藏海龟
(12)forward(distance) #向前移动距离distance代表距离
(13)backward(distance) #向后移动距离distance代表距离
(14)speed(speed) #画笔绘制的速度范围[0,10]整数
实例分析
实例分析一
这个例子是自己码的,还有很多不足,希望大家指出改正
import turtle,pygame,sys,math
pygame.init() #初始化
turtle.showturtle()
turtle.color("red")
turtle.pensize(5)
#I的画图
turtle.penup()
turtle.goto(-200,170)
turtle.pendown()
turtle.goto(-180,170)
turtle.goto(-190,170)
turtle.goto(-190,100)
turtle.penup()
turtle.goto(-210,100)
turtle.pendown()
turtle.goto(-170,100)
turtle.penup()
#L的画法
turtle.goto(-190,70)
turtle.pendown()
turtle.goto(-190,0)
turtle.goto(-150,0)
turtle.penup()
#O的画图
turtle.goto(-120,0)
turtle.pendown()
turtle.circle(20)
turtle.penup()
#V的画图
turtle.goto(-90,40)
turtle.pendown()
turtle.goto(-70,0)
turtle.goto(-50,40)
turtle.penup()
#e的画图
turtle.goto(-40,20)
turtle.pendown()
turtle.goto(0,20)
turtle.penup()
turtle.goto(-20,0)
turtle.pendown()
turtle.circle(20,-270)
turtle.penup()
#u的画图
turtle.goto(-20,-30)
turtle.pendown()
turtle.goto(-20,-70)
turtle.goto(20,-70)
turtle.goto(20,-30)
实例分析二
可以尝试着自己码出以下代码,会有惊喜哦~
import turtle
t = turtle.Pen()
for x in range(360):
t.forward(x)
t.left(59)
也可以尝试着改变left中的值,会有不同的效果哦~