刚刚学习了《Python入门教程(一)——Python语言基础视频课程》(http://edu.51cto.com/course/12194.html)
中间有道作业就是用turtle画中国象棋棋盘,试了半天,终于画出来了,
不知道是不是这么画的,
所以贴上来,求指点,求更好的方法!
#画中国象棋棋盘
import turtle
import math
turtle.speed(10)#绘图速度
a = 30 #每个格子的宽度
#绘制底板并上色
turtle.up()
turtle.goto( -10, -10)
turtle.down()
turtle.fillcolor("Khaki")
turtle.begin_fill()
turtle.fd( a * 8 + 20)
turtle.left(90)
turtle.fd( a * 9 + 20)
turtle.left(90)
turtle.fd( a * 8 + 20)
turtle.left(90)
turtle.fd( a * 9 + 20)
turtle.left(90)
turtle.end_fill()
#画笔回位
turtle.up()
turtle.goto(0,0)
turtle.down()
#画横线
for i in range(10):
turtle.fd( a * 8 )
turtle.up()
turtle.bk( a * 8)
turtle.left( 90 )
turtle.fd( a )
turtle.right( 90 )
turtle.down()
turtle.up()
turtle.goto( 0, 0)
turtle.down()
#画竖线
turtle.left(90)
for i in range(9):
if i == 0 or i == 8:
turtle.fd( a * 9 )
turtle.up()
turtle.bk( a * 9 )
turtle.right(90)
turtle.fd(a)
turtle.left(90)
turtle.down()
else:
turtle.fd( a * 4 )
turtle.up()
turtle.fd(a)
turtle.down()
turtle.fd( a * 4)
turtle.up()
turtle.bk( a * 9)
turtle.right(90)
turtle.fd(a)
turtle.left(90)
turtle.down()
# 画米字格
turtle.up()
turtle.goto( a * 4, a)
turtle.down()
turtle.right(45)
for i in range(4):
turtle.fd(math.sqrt( a * a * 2))
turtle.bk(math.sqrt( a * a * 2))
turtle.right(90)
turtle.up()
turtle.goto(a*4,a*8)
turtle.down()
for i in range(4):
turtle.fd(math.sqrt( a * a * 2))
turtle.bk(math.sqrt( a * a * 2))
turtle.right(90)
turtle.done()
运行结果
原文地址:http://blog.51cto.com/12068679/2069625
时间: 2024-11-06 03:52:15