1. 使用turtle 画一辆卡车。这个我画得很撮。 代码如下:
-
def draw_truck():
-
t=Turtle()
-
screen=t.getscreen()
-
-
#wheels
-
t.circle(30,360)
-
t.penup()
-
t.forward(200)
-
t.pendown()
-
t.circle(30,360)
-
t.penup()
-
t.left(90)
-
t.forward(60)
-
t.right(90)
-
t.forward(50)
-
t.pendown()
-
#draw car
-
t.left(90)
-
t.forward(50)
-
t.left(90)
-
t.forward(50)
-
t.right(30)
-
t.forward(50)
-
t.left(30)
-
t.forward(30)
-
t.left(90)
-
t.forward(25)
-
t.right(90)
-
t.forward(180)
-
t.left(90)
-
t.forward(50)
-
t.left(90)
-
t.forward(300)
-
screen.exitonclick()
第二段代码: parse xml,然后使用turtle画画,原始代码是通过minidom来parse xml,我把这段代码改成了ElementTree,感觉ElementTree 要比dom 简单些,对于attributes的操作直接上字典就行了。
flowerandbg.xml 可以在这里下载:
-
from xml.dom import minidom
-
def parse_flowerandbg():
-
xmldoc=minidom.parse("flowerandbg.xml")
-
graphicsCommand=xmldoc.getElementsByTagName("GraphicsCommands")[0]
-
commands=graphicsCommand.getElementsByTagName("Command")
-
commandList=[]
-
xlist=[]
-
yList=[]
-
widthList=[]
-
colorList=[]
-
radiusList=[]
-
-
for command in commands:
-
#print(command.firstChild.data.strip())
-
commandList.append(command.firstChild.data.strip())
-
attr=command.attributes
-
if "x" in attr:
-
xlist.append(attr["x"].value)
-
else:
-
xlist.append(None)
-
if "y" in attr:
-
yList.append(attr["y"].value)
-
else:
-
yList.append(None)
-
-
if "width" in attr:
-
widthList.append(attr["width"].value)
-
else:
-
widthList.append(None)
-
if "color" in attr:
-
colorList.append(attr["color"].value)
-
else:
-
colorList.append(None)
-
-
if "radius" in attr:
-
radiusList.append(attr["radius"].value)
-
else:
-
radiusList.append(None)
下面是我改写的代码,是用了ElementTree来parse xml文件,然后使用turtle 画画,使用turtle 画画的代码相同,故而上面的代码省略了。
-
import xml.etree.ElementTree
-
def parse_flowerandbg3():
-
content=xml.etree.ElementTree.parse("flowerandbg.xml")
-
root=content.getroot()
-
#print (root.tag,root.attrib)
-
graphiccommand=content.findall("GraphicsCommands")
-
#command=content.findtext("Command")
-
commands=content.findall("Command")
-
commandList=[]
-
xList=[]
-
yList=[]
-
widthList=[]
-
colorList=[]
-
radiusList=[]
-
for command in commands:
-
#print(command.tag,command.attrib,command.text)
-
#print(command.text)
-
commandList.append(command.text.strip())
-
attr=command.attrib
-
if "x" in attr:
-
#print(attr["x"])
-
xList.append(attr["x"])
-
else:
-
xList.append(None)
-
if "y" in attr:
-
yList.append(attr["y"])
-
else:
-
yList.append(None)
-
-
if "width" in attr:
-
widthList.append(attr["width"])
-
else:
-
widthList.append(None)
-
if "color" in attr:
-
colorList.append(attr["color"])
-
else:
-
colorList.append(None)
-
-
if "radius" in attr:
-
radiusList.append(attr["radius"])
-
else:
-
radiusList.append(None)
-
-
#begin to draw
-
t=turtle.Turtle()
-
screen=t.getscreen()
-
screen.colormode(255)
-
screen.tracer(0)
-
for i in range(len(commandList)):
-
command=commandList[i]
-
if command=="PenUp":
-
t.penup()
-
elif command=="PenDown":
-
t.pendown()
-
elif command=="GoTo":
-
x=float(xList[i])
-
y=float(yList[i])
-
width=float(widthList[i])
-
color=colorList[i]
-
t.width(width)
-
t.color(color)
-
t.goto(x,y)
-
elif command=="Circle":
-
radius=float(radiusList[i])
-
width=float(widthList[i])
-
color=colorList[i]
-
t.width(width)
-
t.pencolor(color)
-
t.circle(radius)
-
elif command=="BeginFill":
-
color=colorList[i]
-
t.fillcolor(color)
-
t.begin_fill()
-
elif command=="EndFill":
-
t.end_fill()
-
else:
-
print("Unknown command:",command)
-
-
screen.update()
-
screen.exitonclick()
效果如下:
阅读(1658) | 评论(0) | 转发(0) |