一、图形化程序设计
1.1基础
基础语法:这里先从海龟画图开始讲解,探索python是如何,
只需要几行命令就可画出复杂图片
>>> import turtle #导入turtle 模块
>>> turtle.showturtle() #显示箭头
>>> turtle.write("python") #写字符串
>>> turtle.forward(300) #前进 300 像素
>>> turtle.color("red") #画笔颜色改为red
>>> turtle.left(90) #箭头左转 90 度
>>> turtle.forward(300)
>>> turtle.goto(0,50) #去坐标(0,50)
>>> turtle.goto(0,0)
>>> turtle.penup() #抬笔。这样,路径就不会画出来
>>> turtle.goto(0,300)
>>> turtle.pendown() #下笔。这样,路径就会画出来
>>> turtle.circle(100) #画圆
以下是一段奥运五环的代码:
import turtle
turtle.width(10)
turtle.penup()
turtle.goto(-200,0)
turtle.pendown()
turtle.color("blue")
turtle.circle(50)
turtle.penup()
turtle.goto(-80,0)
turtle.pendown()
turtle.color("black")
turtle.circle(50)
turtle.penup()
turtle.goto(40,0)
turtle.pendown()
turtle.color("red")
turtle.circle(50)
turtle.penup()
turtle.goto(-140,-50)
turtle.pendown()
turtle.color("yellow")
turtle.circle(50)
turtle.penup()
turtle.goto(-20,-50)
turtle.pendown()
turtle.color("green")
turtle.circle(50)
运行后会得到如结果
具体的也可以参考以下网址比较详细:
https://www.cnblogs.com/nowgood/p/turtle.html#_nav_1.
1.2 设置画布大小
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
参数为画布 [ 宽, 高, 背景颜色 ]
turtle.screensize(800, 600, "green")
turtle.screensize() #返回默认大小(400, 300)
1.3 画笔
turtle.pensize():设置画笔的宽度;
turtle.pencolor(); 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组,
>>> pencolor('brown')
>>> tup = (0.2, 0.8, 0.55)
>>> pencolor(tup)
>>> pencolor()
'#33cc8c'
turtle.speed(speed): 设置画笔移动速度,画笔绘制的速度范围[0,10]整数, 数字越大越快
1.4 绘图命令
(1)画笔运动命令:
命令 说明
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)画笔控制命令:
命令 说明
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) 全局控制命令
命令 说明
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的参数也是可选项
二、总结
熟练使用画笔命令就会得到各种想要的图片。
代码如下:
import turtle
import random
from turtle import *
from time import sleep
t = turtle.Turtle()
w = turtle.Screen()
def tree(branchLen, t):
if branchLen > 3:
if 8 <= branchLen <= 12:
if random.randint(0, 2) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branchLen / 3)
elif branchLen < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branchLen / 2)
else:
t.color('sienna')
t.pensize(branchLen / 10)
t.forward(branchLen)
a = 1.5 * random.random()
t.right(20*a)
b = 1.5 * random.random()
tree(branchLen-10*b, t)
t.left(40*a)
tree(branchLen-10*b, t)
t.right(20*a)
t.up()
t.backward(branchLen)
t.down()
def petal(m, t): # 树下花瓣
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color("lightcoral")
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
def main():
t = turtle.Turtle()
myWin = turtle.Screen()
getscreen().tracer(5, 0)
turtle.screensize(bg='wheat')
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
tree(60, t)
petal(100, t)
myWin.exitonclick()
main()
部内内容转自链接:https://blog.csdn.net/zyx_ly/article/details/87980091