一、画五角星
描述
画一个五角星,画笔用黄色,用红色填充,效果如下所示。
代码如下:
#画五角星
import turtle
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for i in range(5):
turtle.forward(120)
turtle.right(144)
turtle.forward(120)
turtle.left(72)
turtle.end_fill()
turtle.hideturtle() #隐藏画笔
turtle.done() #结束绘制
效果图如下:
二、画一组同心圆
描述
利用turtle库画一组同心圆。用户输入最小圆的半径、圆的个数和画笔颜色,每个相邻圆半径相差20。
输入格式
第一行输入一个正整数,作为最小圆的半径
第二行输入一个正整数,作为圆的个数
第三行输入画笔颜色的英文名,如red, blue, green等
输出格式
代码如下:
#画一组同心圆
rmin=eval(input())
number=eval(input())
color=input()
import turtle as t
t.pencolor(color)
t.pensize(4)
for i in range (number):
t.circle(rmin)
rmin=rmin+20
t.penup()
t.seth(-90)
t.fd(20)
t.seth(0)
t.pendown()
t.hideturtle()
t.done()
输入:
50
5pink
效果图如下:
三、渐变的圆
描述
利用turtle库的circle(50)函数可以画半径为50的圆,circle(50,steps=n)可以画半径为50的圆的内接正n边形,利用这个方法绘制示例中的图形,设置画笔为蓝色并用黄色填充图形。n由用户输入,要求n>=3且小于10。(注意:最后一个必须是圆,不能是正多边形)
输入格式
一个大于等于3且小于10的正整数
输出格式
代码如下:
#渐变的圆
import turtle
number = int(input()) #把用户输入转成整数
turtle.screensize(600,500,'white')
turtle.pensize(3) #设置画笔宽度为3
turtle.pencolor('blue') #设置画笔颜色为黑色
turtle.fillcolor('yellow') #设置填充颜色为黄色
turtle.begin_fill() #开始填充
turtle.forward(-50)
for i in range(3,number):
turtle.circle(50, steps=i)
turtle.forward(100)
turtle.circle(50, steps=number)
if number == 1:
turtle.circle(50)
else:
turtle.forward(100)
turtle.circle(50)
turtle.end_fill()
turtle.hideturtle() #隐藏海龟
turtle.done()
输入:
9
效果图如下:
五、画奥运五环
描述
参考以下代码,利用turtle库画奥运五环,圆半径为45.
代码如下:
#画奥运五环
import turtle
coordA=(-100,0,100,-50,70)
coordB=(-20,-20,-20,-70,-70)
turtle.width(5)
turtle.color("red")
turtle.penup()
turtle.goto(coordA[0],coordB[0])
turtle.pendown()
turtle.circle(45)
turtle.color("blue")
turtle.penup()
turtle.goto(coordA[1],coordB[1])
turtle.pendown()
turtle.circle(45)
turtle.color("green")
turtle.penup()
turtle.goto(coordA[2],coordB[2])
turtle.pendown()
turtle.circle(45)
turtle.color("yellow")
turtle.penup()
turtle.goto(coordA[3],coordB[3])
turtle.pendown()
turtle.circle(45)
turtle.color("black")
turtle.penup()
turtle.goto(coordA[4],coordB[4])
turtle.pendown()
turtle.circle(45)
turtle.hideturtle()
turtle.done()
效果图如下:
六、画太极图
描述
利用turtle库画以下太极图形状.
代码如下:
#绘制太极图
from turtle import *
setup(800,800,100,100)
#绘制左半部分
fillcolor('#FFFFFF')
begin_fill()
circle(100,180)
circle(200,180)
seth(180)
circle(-100,180)
end_fill()
seth(90)
penup()
fd(85)
pendown()
seth(0)
fillcolor('#000000')
begin_fill()
circle(25)
end_fill()
seth(-90)
penup()
fd(85)
pendown()
seth(180)
#绘制右半部分
fillcolor('#000000')
begin_fill()
circle(100,180)
circle(200,180)
seth(0)
circle(-100,180)
end_fill()
seth(-90)
penup()
fd(85)
pendown()
seth(-180)
fillcolor('#FFFFFF')
begin_fill()
circle(25)
end_fill()
hideturtle()
done()
效果图如下: