1.正方形螺旋线的绘制。
(1)利用turtle库绘制一个正方形螺旋线,效果如下图所示。
import turtle
turtle.speed("fastest")
turtle.pensize(2)
for x in range(100):
turtle.forward(2*x)
turtle.left(90)
(2)修改上面程序,绘制斜螺旋线,效果如下图所示。
import turtle
turtle.speed("fastest")
turtle.pensize(2)
for x in range(100):
turtle.forward(2*x)
turtle.left(91)
(3) 修改上面程序,绘制彩色斜螺旋线,效果如下图所示。
import turtle
turtle.speed("fastest")
turtle.pensize(2)
colors=["red","yellow",'purple','blue']
for x in range(100):
turtle.pencolor(colors[x%4])
turtle.forward(2*x)
turtle.left(91)
4 彩色蟒蛇绘制。对 Python 蟒蛇的每个部分采用不同颜色,绘制一条彩色蟒蛇。
import turtle
def drawSnake(rad, angle, len, neckrad):
colors = ["purple", "red", "yellow", "pink", "blue"]
for i in range(len):
turtle.color(colors[i])
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.color("green")
turtle.circle(rad, angle / 2)
turtle.fd(rad)
turtle.circle(neckrad + 1, 180)
turtle.fd(rad * 2 / 3)
def main():
turtle.setup(1300, 800, 0, 0)
turtle.penup()
turtle.goto(-350, 0)
turtle.pendown()
pythonsize = 30
turtle.pensize(pythonsize)
turtle.seth(-40)
drawSnake(40, 80, 5,pythonsize / 2)
main()
4.绘制奥运五环图,其中五种颜色分别为蓝色、黑色、红色、黄色和绿色。
import turtle
turtle.pensize(10)
turtle.color("blue")
turtle.penup()
turtle.goto(-110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("black")
turtle.penup()
turtle.goto(0,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("red")
turtle.penup()
turtle.goto(110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("yellow")
turtle.penup()
turtle.goto(-55,-75)
turtle.pendown()
turtle.circle(45)
turtle.color("green")
turtle.penup()
turtle.goto(55,-75)
turtle.pendown()
turtle.circle(45)
5.制作一个模拟windows启动界面的文本进度条。
import time
scale=50
print()
for i in range(1,12):
if i in(1,6,11):
print("{0: ^50}".format('-'*20))
else:
print("{0:>15}{1: ^20}{2: <15}".format('|','|','|'))
print("正在启动windows...".center(scale,'-'))
print()
for i in range(scale+1):
a='|'*i
b='.'*(scale-i)
c=(i/scale)*100
print("\r[{}{}]{:^3.0f}%".format(a,b,c),end='')
time.sleep(0.3)
print()
print("\n"+"启动完成!".center(scale,'-'))