详解Python 画蝰蛇(适合初学者)
运行截图
代码
代码片
.
// An highlighted block
import turtle
def drawsnake(rad,angle,len,neckrad):a = ['blue','red','yellow','green','pink']for i in range(0,len):turtle.pencolor(a[i])turtle.circle(rad,angle)turtle.circle(-rad,angle)turtle.pencolor('black') 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)pythonsize = 30turtle.pensize(pythonsize)turtle.seth(-40)drawsnake(40,80,5,pythonsize/2)
main()
注解
turtle.setup(1300,800,0,0)中1300和800表示窗口长宽分别为1300像素和800像素,0,0表示离左和上距离均为0
pensize表示蝰蛇的宽度
seth表示前进方向,与坐标轴中单位圆定义相同,0为X轴正方向,90为Y轴正方向,
对于drawsnake函数,四个参数分别为半径,角度,长度和半径(脖子)
半径为正值,表示圆弧在小乌龟的右边,为负值,则在左边,
fd是直行函数,
angle表示圆的弧度。