Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1783027
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2015-12-21 10:36:49

1. 使用turtle 画一辆卡车。这个我画得很撮。 代码如下:

点击(此处)折叠或打开

  1. def draw_truck():
  2.     t=Turtle()
  3.     screen=t.getscreen()

  4.     #wheels
  5.     t.circle(30,360)
  6.     t.penup()
  7.     t.forward(200)
  8.     t.pendown()
  9.     t.circle(30,360)
  10.     t.penup()
  11.     t.left(90)
  12.     t.forward(60)
  13.     t.right(90)
  14.     t.forward(50)
  15.     t.pendown()
  16.     #draw car
  17.     t.left(90)
  18.     t.forward(50)
  19.     t.left(90)
  20.     t.forward(50)
  21.     t.right(30)
  22.     t.forward(50)
  23.     t.left(30)
  24.     t.forward(30)
  25.     t.left(90)
  26.     t.forward(25)
  27.     t.right(90)
  28.     t.forward(180)
  29.     t.left(90)
  30.     t.forward(50)
  31.     t.left(90)
  32.     t.forward(300)
  33.     screen.exitonclick()

第二段代码: parse xml,然后使用turtle画画,原始代码是通过minidom来parse xml,我把这段代码改成了ElementTree,感觉ElementTree 要比dom 简单些,对于attributes的操作直接上字典就行了。
flowerandbg.xml 可以在这里下载:

点击(此处)折叠或打开

  1. from xml.dom import minidom
  2. def parse_flowerandbg():
  3.     xmldoc=minidom.parse("flowerandbg.xml")
  4.     graphicsCommand=xmldoc.getElementsByTagName("GraphicsCommands")[0]
  5.     commands=graphicsCommand.getElementsByTagName("Command")
  6.     commandList=[]
  7.     xlist=[]
  8.     yList=[]
  9.     widthList=[]
  10.     colorList=[]
  11.     radiusList=[]

  12.     for command in commands:
  13.         #print(command.firstChild.data.strip())
  14.         commandList.append(command.firstChild.data.strip())
  15.         attr=command.attributes
  16.         if "x" in attr:
  17.             xlist.append(attr["x"].value)
  18.         else:
  19.             xlist.append(None)
  20.         if "y" in attr:
  21.             yList.append(attr["y"].value)
  22.         else:
  23.             yList.append(None)

  24.         if "width" in attr:
  25.             widthList.append(attr["width"].value)
  26.         else:
  27.             widthList.append(None)
  28.         if "color" in attr:
  29.             colorList.append(attr["color"].value)
  30.         else:
  31.             colorList.append(None)

  32.         if "radius" in attr:
  33.             radiusList.append(attr["radius"].value)
  34.         else:
  35.             radiusList.append(None)
下面是我改写的代码,是用了ElementTree来parse xml文件,然后使用turtle 画画,使用turtle 画画的代码相同,故而上面的代码省略了。


点击(此处)折叠或打开

  1. import xml.etree.ElementTree
  2. def parse_flowerandbg3():
  3.     content=xml.etree.ElementTree.parse("flowerandbg.xml")
  4.     root=content.getroot()
  5.     #print (root.tag,root.attrib)
  6.     graphiccommand=content.findall("GraphicsCommands")
  7.     #command=content.findtext("Command")
  8.     commands=content.findall("Command")
  9.     commandList=[]
  10.     xList=[]
  11.     yList=[]
  12.     widthList=[]
  13.     colorList=[]
  14.     radiusList=[]
  15.     for command in commands:
  16.         #print(command.tag,command.attrib,command.text)
  17.         #print(command.text)
  18.         commandList.append(command.text.strip())
  19.         attr=command.attrib
  20.         if "x" in attr:
  21.             #print(attr["x"])
  22.             xList.append(attr["x"])
  23.         else:
  24.             xList.append(None)
  25.         if "y" in attr:
  26.             yList.append(attr["y"])
  27.         else:
  28.             yList.append(None)

  29.         if "width" in attr:
  30.             widthList.append(attr["width"])
  31.         else:
  32.             widthList.append(None)
  33.         if "color" in attr:
  34.             colorList.append(attr["color"])
  35.         else:
  36.             colorList.append(None)

  37.         if "radius" in attr:
  38.             radiusList.append(attr["radius"])
  39.         else:
  40.             radiusList.append(None)

  41.     #begin to draw
  42.     t=turtle.Turtle()
  43.     screen=t.getscreen()
  44.     screen.colormode(255)
  45.     screen.tracer(0)
  46.     for i in range(len(commandList)):
  47.         command=commandList[i]
  48.         if command=="PenUp":
  49.             t.penup()
  50.         elif command=="PenDown":
  51.             t.pendown()
  52.         elif command=="GoTo":
  53.             x=float(xList[i])
  54.             y=float(yList[i])
  55.             width=float(widthList[i])
  56.             color=colorList[i]
  57.             t.width(width)
  58.             t.color(color)
  59.             t.goto(x,y)
  60.         elif command=="Circle":
  61.             radius=float(radiusList[i])
  62.             width=float(widthList[i])
  63.             color=colorList[i]
  64.             t.width(width)
  65.             t.pencolor(color)
  66.             t.circle(radius)
  67.         elif command=="BeginFill":
  68.             color=colorList[i]
  69.             t.fillcolor(color)
  70.             t.begin_fill()
  71.         elif command=="EndFill":
  72.             t.end_fill()
  73.         else:
  74.             print("Unknown command:",command)
  75.     
  76.     screen.update()
  77.     screen.exitonclick()




效果如下:
阅读(1658) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~