Python

Python turtle模块一(常用API)

2019-05-01  本文已影响25人  代码充电宝
(1)概述
(2)简单例子
# 导入turtle包的所有内容:
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()
(3)画布API
turtle.screensize(800, 600, "green")
turtle.screensize() #返回默认大小(400, 300)
turtle.setup(width=0.6, height=0.6)
turtle.setup(width=800, height=800, startx=100, starty=100)
turtle.write("Done", font=('Arial', 40, 'normal'))
(4)画笔控制命令
# 设置画笔宽度
turtle.pensize(10)
# 设置画笔颜色方式一:字符串
turtle.pencolor('red')
# 设置画笔颜色方式一:RGB tuple
tup = (0.2,0.8,0.6)
turtle.pencolor(tup)
# 返回当前画笔颜色tuple
print(turtle.pencolor())
# 设置画笔速度
turtle.speed(0)
(5)画笔运动命令
# 一当前画笔位置为圆上一点,正上方30px为圆心画圆
turtle.circle(30)
turtle.forward(100)
# 一当前画笔位置为圆上一点,正下方30px为圆心画圆
turtle.circle(-30)
# 三角形
circle(50,steps=3) 
# 半圆
circle(120, 180) 
上一篇下一篇

猜你喜欢

热点阅读