蟒蛇绘制学习笔记

import turtle
#引入外部库
#函数库的基本使用,如math,random
#引用有两种方式:import turtle,引用时turtle.fd(100)
#                from turtle import * 或者from turtle import <函数名>,引用时直接使用fd(100)即可,注意函数名不能重复,保证唯一性
def drawSnake(rad, angle, len, neckrad):
#def定义函数,该函数未经调用是不会执行的
    for i in range(len):
        turtle.circle(rad, angle)
        #圆形轨迹爬行,rad为正值,小乌龟左侧rad远的地方为圆心
        #rad为负值,小乌龟右侧rad远的地方为圆心
        #小乌龟沿着圆心爬行的角度值angle,旋转方向由笔尖方向决定
        turtle.circle(-rad, angle)
    turtle.circle(rad, angle/2)
    turtle.fd(rad)
    #小乌龟沿着直线爬行的距离,也可为turtle.forward(rad)
    turtle.circle(neckrad + 1, 180)
    turtle.fd(rad*2/3)
    
def main():
    turtle.setup(1300, 500, 0, 0)
    #setup启动窗口,长度x,宽度y,起始点坐标位置,左上角为原点
    pythonsize = 30
    turtle.pensize(pythonsize)
    #笔的宽度
    turtle.pencolor("#3B9909")
    #笔的颜色,也可以用"blue"
    turtle.seth(-40)
    #笔的运行角度值,按象限方向0 -> 360,按顺时针0 -> -360
    drawSnake(40, 80, 5, pythonsize/2)

main()
#这一句为该程序第一个执行的函数语句

彩色蟒蛇绘制

import turtle

def drawSnake(rad, angle, len, neckrad):
    for i in range(len):
        changeColor(i)
        turtle.circle(rad, angle)
        turtle.circle(-rad, angle)
    turtle.pencolor("#3B9909")
    turtle.circle(rad, angle/2)
    turtle.fd(rad)
    turtle.circle(neckrad + 1, 180)
    turtle.fd(rad*2/3)
    print (locals())

def changeColor(color):
    if color == 0:
        turtle.pencolor("yellow")
    elif color == 1:
        turtle.pencolor("green")
    elif color == 2:
        turtle.pencolor("blue")
    elif color == 3:
        turtle.pencolor("red")
    else:
        turtle.pencolor("purple")
    print (locals())
        
def main():
    turtle.setup(1300, 500, -50, 0)
    pythonsize = 30
    turtle.pensize(pythonsize)
    turtle.seth(-50)
    drawSnake(40, 80, 5, pythonsize/2)
    print (locals())
    print (globals())

main()
发布了12 篇原创文章 · 获赞 0 · 访问量 78

猜你喜欢

转载自blog.csdn.net/qq_41819076/article/details/104181030
今日推荐