python自定义Drawturtle类在命令提示符下通过命令行参数运行

"""在程序中定义了一个继承自Turtle的类,给它增加了一些画多边形,画8字等方法。
请给本程序增加功能,让在命令提示符下输入 draw.py circle 时,会画一个圆圈"""

from turtle import *
import sys

import colorsys
def coloradd(color,dh):
    """颜色增加原理:
        color是三元组,分别为0-255的值.此函数把颜色转换成hls模式,对h进行增加dh的操作
       然后转换回去,dh是小于1的浮点数.
    """
    if len(color)==3 :
        h,l,s, = colorsys.rgb_to_hls(color[0]/255,color[1]/255,color[2]/255)
        h =  h + dh
        r,g,b = colorsys.hls_to_rgb(h,l,s)
        return int(r*255),int(g*255),int(b*255)
    else:
        return color
addcolor = coloradd   # 定义别名

def colorset(color):
    """设定颜色,color范围为1到360"""
    color = color % 360
    color = color / 360.0    
    r,g,b = colorsys.hsv_to_rgb(color,1.0,1.0)
    
    return int(r*255),int(g*255),int(b*255)
    
setcolor = colorset    # 定义别名

class Drawturtle(Turtle):
    yanse = (255,0,0)
    def __init__(self,shape = "turtle",visible = True,undobuffersize = 1000):
        """初始化海龟"""
        Turtle.__init__(self,shape = shape,visible = visible,undobuffersize = undobuffersize)
        if self.screen.colormode()!=255:self.screen.colormode(255)
        self.pencolor(Drawturtle.yanse)
        
    def draw_polygon(self,number,length):
        """画正多边形,number边数,length,边长"""        
        
        for i in range(number):
            self.fd(length)
            self.rt(360/number)
            Drawturtle.yanse = addcolor(Drawturtle.yanse,0.01)
            self.pencolor(Drawturtle.yanse)
            
    def draw_sun(self,diameter,number,length):
        """画太阳,直径,number边数,length,边长"""        
        self.penup()
        for i in range(number):            
            self.fd(length/2)
            self.pendown()
            self.fd(length/2)
            self.penup()
            self.bk(length)
            self.rt(360/number)
            Drawturtle.yanse = addcolor(Drawturtle.yanse,0.1)
            self.pencolor(Drawturtle.yanse)
        self.dot(diameter)
    
    def draw_star(self,length):
        """画星星,length,边长"""        
  
        for i in range(5):     
            self.fd(length)  
            self.rt(144)
            Drawturtle.yanse = addcolor(Drawturtle.yanse,0.1)
            self.pencolor(Drawturtle.yanse)
            
    def draw8(self,step):
        
        for i in range(20):     
            self.fd(step)  
            self.lt(9)
            Drawturtle.yanse = addcolor(Drawturtle.yanse,0.1)
            self.pencolor(Drawturtle.yanse)
        for i in range(40):     
            self.fd(step)  
            self.rt(9)
            Drawturtle.yanse = addcolor(Drawturtle.yanse,0.1)
            self.pencolor(Drawturtle.yanse)
        for i in range(20):     
            self.fd(step)  
            self.lt(9)
            Drawturtle.yanse = addcolor(Drawturtle.yanse,0.1)
            self.pencolor(Drawturtle.yanse)
        
  

if __name__ == "__main__":

    parameters = ['polygon','sun','star','8']
    helpinfo = """本程序目前支持以下四种用法:\n    
    draw.py polygon"
    draw.py sun
    draw.py star
    draw.py 8

    """
     
    p = sys.argv                          # 命令行参数列表

    if len(p) == 1 :
        print(helpinfo)
        sys.exit(0)

    p = p[1]

    if not p in parameters:sys.exit(0)

    screen = Screen()    
    screen.setup(640,480)
    screen.colormode(255)
    screen.bgcolor("black")
    screen.title("Python海龟画图_命令行参数练习")

    t = Drawturtle()
    t.color("cyan")
    t.pensize(3)
    t.screen.delay(2)
    t.setheading(180)    
    
    if p == "polygon" :
        t.draw_polygon(5,150)
    elif p == "sun":
        t.draw_sun(60,10,100)
    elif p == "star":
        t.draw_star(100)
    elif p == "8":
        t.draw8(10)

    t.screen.exitonclick()