海龟绘图法

简单的绘图工具-----海龟绘图(Turtle Graphics)

使用海龟绘图首先我们需要导入turtle:

from turtle import *   # 将turtle中的所有方法导入

海龟绘图属性:

 (1)位置
 (2)方向
 (3)画笔(画笔的属性,颜色、画线的宽度)

操纵海龟绘图有着许多的命令,这些命令可以划分为两种:一种为运动命令,一种为画笔控制命令

(1)运动命令:

1 forward(degree)  #向前移动距离degree代表距离
2 backward(degree)  #向后移动距离degree代表距离
3 right(degree)    #向右移动多少度
4 left(degree)  #向左移动多少度
5 goto(x,y)  #将画笔移动到坐标为x,y的位置
6 stamp()     #复制当前图形
7 speed(speed)  #画笔绘制的速度范围[0,10]整数
View Code

(2)画笔控制命令:

1 down() #移动时绘制图形,缺省时也为绘制
2 up() #移动时不绘制图形
3 pensize(width) #绘制图形时的宽度
4 color(colorstring) #绘制图形时的颜色
5 fillcolor(colorstring) #绘制图形的填充颜色
6 fill(Ture)
7 fill(false)
View Code

*******************************************************************************************************************

实例:

 1 from turtle import *
 2 color('red', 'yellow')
 3 begin_fill()
 4 while True:
 5     forward(200)
 6     left(170)
 7     if abs(pos()) < 1:
 8         break
 9 end_fill()
10 done()
View Code
 1 import turtle
 2 
 3 def drawMulticolorSqure(t,sz):
 4     """Make turtle t draw a multi-color square of sz"""
 5     for i in ['red','purple','hotpink','blue']:
 6         t.color(i)
 7         t.forward(sz)
 8         t.left(90)
 9 
10 wn = turtle.Screen()
11 wn.bgcolor('lightgreen')
12 
13 alex = turtle.Turtle()
14 alex.pensize(3)
15 
16 size = 20
17 for i in range(15):
18     drawMulticolorSqure(alex,size)
19     size = size + 10
20     alex.forward(10)
21     alex.right(18)
22 
23 wn.exitonclick()
View Code

 

转载于:https://www.cnblogs.com/zijue/p/10146557.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值