"""在程序中定义了一个继承自Turtle的类,给它增加了一些画多边形,画8字等方法。请给本程序增加功能,让在命令提示符下输入 draw.py circle 时,会画一个圆圈""" from turtle import * import sys import colorsys def coloradd(color,dh): """颜色增加原理: color是三元组,分别为0-255的值.此函数把颜色转换成hls模式,对h进行增加dh的操作 然后转换回去,dh是小于1的浮点数. """ addcolor = coloradd # 定义别名 def colorset(color): """设定颜色,color范围为1到360""" 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) 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()
如需要查看完整代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)