python作业案例_pythonturtle⼩海龟画图(语法基础及作业案
例)
⼩海龟画图
⼩海龟:import turtle 是python种很流⾏的绘制图像的函数库,通过x轴y轴进⾏坐标移动,绘制图形。
常⽤语法
常⽤命令说明pen=turtle.Pen()获取笔
pen.forward(100) backward()向前⾛/后(像素长度)
pen.left(90) right()向左转/右转(⾓度)
pen.circle(50)画圆(半径)
画笔属性说明pen.speed(5)画笔速度(1-10)
pen.pencolor(‘red’)画笔颜⾊(颜⾊)
pen.pensize(5)画笔粗细
画笔动作说明pen.penup()画笔抬起
pen.pendown()画笔落下
<(x,y)将画笔移动到(x,y)
颜⾊填充说明pen.begin_fill()开始填充
pen.fillcolor(‘red’)填充颜⾊
案例1:画⼀个正⽅形
import turtle
t = turtle.Pen() #获取笔
t.speed(1) #画笔速度
t.forward(100) #向前画
t.left(90) #左转
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
turtle.done()
案例2:圆形填充
t = turtle.Pen()
t.pencolor('red')
t.forward(100)
t.begin_fill()
t.fillcolor('green') t.circle(50)
turtle.done()
案例3:阶梯
import turtle
pen=turtle.Pen() pen.speed(1)
pen.forward(100) pen.right(90)
pen.forward(100) pen.left(90)
pen.forward(100) pen.right(90)
pen.forward(100) pen.left(90)
pen.forward(100) pen.right(90)
pen.forward(100) turtle.done()
案例4:五连环import turtle
pen=turtle.Pen() pen.speed(10) pen.pencolor('blue') pen.pensize(5) pen.circle(50) pen.penup()
pen.forward(80)
pen.pendown()
pen.pencolor('black') pen.circle(50)
pen.penup()
pen.forward(80) pen.pendown()
pen.pencolor('red') pen.circle(50)
pen.penup()
pen.left(90)
pen.forward(50) pen.left(90)
pen.forward(50) pen.pendown()
pen.pencolor('green') pen.circle(50)
pen.penup()
pen.forward(80) pen.pendown()
pen.pencolor('yellow') pen.circle(50)
turtle.done()
案例6:糖葫芦
import turtle
t=turtle.Pen()
t.speed(10)
t.penup()
t.pencolor('red')
t.forward(100)
t.pendown()
t.begin_fill()
t.fillcolor('red')
t.circle(50)
<(150,50)
t.begin_fill()
t.fillcolor('green') t.circle(50)
t.penup()
<(200,100)
t.pendown()
t.begin_fill()
t.fillcolor('yellow') t.circle(50)
t.penup()
<(250,150)
t.pendown()
t.begin_fill()
t.fillcolor('purple') t.circle(50)
t.penup()
t.pencolor('orange') t.pensize(7)
t.forward(29)
t.left(90)
t.forward(85)
t.left(137)
t.pendown()
t.forward(380) turtle.done()
案例7:⼩汽车import turtle
t=turtle.Pen()
t.pencolor('red')
t.fillcolor('red')
t.left(90)
<(0,200)
t.right(90)
<(200,200)
t.left(90)
<(200,100)
t.right(90)
<(400,100)
t.right(90)
<(400,0)
<(0,0)
t.begin_fill()
t.fillcolor('black')
<(50,0)
t.circle(50)
案例8:⽤不同的颜⾊的笔画⽅框import turtle
t=turtle.Pen()
color = ['red','yellow','blue','green'] for x in range(100):
t.pencolor(color[x%4])
t.forward(x)
t.left(90)
python新手代码画图案例9:⽤for画螺旋线
import turtle
t=turtle.Pen()
color = ['red','yellow','blue','green'] for x in range(100):
t.pencolor(color[x%4])
t.forward(x)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论