圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了python图形工具turtle绘制国际象棋棋盘的具体代码,供大家参考,具体内容如下

#编写程序绘制一个国际象棋的棋盘
import turtle
turtle.speed(30)
turtle.penup()
off = True
for y in range(-40, 30 + 1, 10):
 for x in range(-40, 30 + 1, 10):
 if off:
 turtle.goto(x, y)
 turtle.pendown()
 turtle.begin_fill()
 turtle.color("black")
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.end_fill()
 turtle.penup()
 else:
 turtle.goto(x, y)
 turtle.pendown()
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.penup()
 off = bool(int(off) - 1)
 off = bool(int(off) - 1)
turtle.hideturtle()
turtle.done()

通过函数的重用优化代码:

先建立一个UsefulTurtleFunctions.py 的文件夹

import turtle
#Draw a square
def drawSquare():
 turtle.pendown()
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.forward(10)
 turtle.left(90)
 turtle.penup()

再在test中调用它

#编写程序绘制一个国际象棋的棋盘
 
import turtle
from UsefulTurtleFunctions import *
turtle.speed(30)
turtle.penup()
off = True
for y in range(-40, 30 + 1, 10):
 for x in range(-40, 30 + 1, 10):
 if off:
 turtle.goto(x, y)
 turtle.begin_fill()
 turtle.color("black")
 drawSquare()
 turtle.end_fill()
 turtle.penup()
 else:
 turtle.goto(x, y)
 drawSquare()
 off = bool(int(off) - 1)
 off = bool(int(off) - 1)
turtle.hideturtle()
turtle.done()

最后结果:

python图形工具turtle绘制国际象棋棋盘

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python,turtle绘制国际象棋棋盘,python绘制国际象棋棋盘,python绘制象棋棋盘

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com