最近学习python刚入门,有java编程经验,就跳过基础语法学习,直接做一些小程序来玩
目前学到turtle,这个乌龟画图太有意思了,分享几个画图,代码如下,原理不解释。
例子一:
#SquareSpiral1.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
sides=6
colors=["red","yellow","green","blue","orange","purple"]
for x in range(360):
t.pencolor(colors[x%sides])
t.forward(x*3/sides+x)
t.left(360/sides+1)
t.width(x*sides/200)
print("####结束####")
效果如下图:
呃。。。没想到图这么大,sides可以换各种数字看有什么效果。图片太大就不上传了。
例子二:
可以做成交互的,利用eval函数获得用户想绘制的边数
代码:
#SquareSpiral1.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
sides=eval(input("输入要绘制的边的数目,请输入2-6的数字!"))
colors=["red","yellow","green","blue","orange","purple"]
for x in range(100):
t.pencolor(colors[x%sides])
t.forward(x*3/sides+x)
t.left(360/sides+1)
t.width(x*sides/200)
print("####结束####")
效果:两条边
例子三:
绘制橡皮筋球体
#SquareSpiral1.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
#sides=eval(input("输入要绘制的边的数目,请输入2-6的数字!"))
sides=6
colors=["red","yellow","green","blue","orange","purple"]
for x in range(360):
t.pencolor(colors[x%sides])
t.forward(x*3/sides+x)
t.left(360/sides+1)
t.width(x*sides/180)
t.left(91)
print("####结束####")
例子四:绘图加上文字
#SquareSpiral1.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
my_name=turtle.textinput("输入你的姓名","你的名字?")
colors=["red","yellow","purple","blue"]
for x in range(100):
t.pencolor(colors[x%4])
t.penup()
t.forward(x*4)
t.pendown()
t.write(my_name,font=("Arial",int((x+4)/4),"bold"))
t.left(92)
print("####结束####")
准备好辣眼睛吧!