Python用turtle库绘制中国象棋棋盘
时间: 2023-04-01 07:03:36 浏览: 272
可以使用turtle库绘制中国象棋棋盘,具体实现可以参考以下代码:
import turtle
# 设置画布大小
turtle.setup(800, 800)
# 设置画笔颜色和粗细
turtle.pensize(2)
turtle.pencolor("black")
# 绘制棋盘边框
turtle.penup()
turtle.goto(-300, 300)
turtle.pendown()
turtle.goto(300, 300)
turtle.goto(300, -300)
turtle.goto(-300, -300)
turtle.goto(-300, 300)
# 绘制棋盘横线
turtle.penup()
turtle.goto(-300, 240)
turtle.pendown()
turtle.goto(300, 240)
turtle.penup()
turtle.goto(-300, 180)
turtle.pendown()
turtle.goto(300, 180)
turtle.penup()
turtle.goto(-300, 120)
turtle.pendown()
turtle.goto(300, 120)
turtle.penup()
turtle.goto(-300, 60)
turtle.pendown()
turtle.goto(300, 60)
turtle.penup()
turtle.goto(-300, )
turtle.pendown()
turtle.goto(300, )
turtle.penup()
turtle.goto(-300, -60)
turtle.pendown()
turtle.goto(300, -60)
turtle.penup()
turtle.goto(-300, -120)
turtle.pendown()
turtle.goto(300, -120)
turtle.penup()
turtle.goto(-300, -180)
turtle.pendown()
turtle.goto(300, -180)
turtle.penup()
turtle.goto(-300, -240)
turtle.pendown()
turtle.goto(300, -240)
# 绘制棋盘竖线
turtle.penup()
turtle.goto(-240, 300)
turtle.pendown()
turtle.goto(-240, -300)
turtle.penup()
turtle.goto(-180, 300)
turtle.pendown()
turtle.goto(-180, -300)
turtle.penup()
turtle.goto(-120, 300)
turtle.pendown()
turtle.goto(-120, -300)
turtle.penup()
turtle.goto(-60, 300)
turtle.pendown()
turtle.goto(-60, -300)
turtle.penup()
turtle.goto(, 300)
turtle.pendown()
turtle.goto(, -300)
turtle.penup()
turtle.goto(60, 300)
turtle.pendown()
turtle.goto(60, -300)
turtle.penup()
turtle.goto(120, 300)
turtle.pendown()
turtle.goto(120, -300)
turtle.penup()
turtle.goto(180, 300)
turtle.pendown()
turtle.goto(180, -300)
turtle.penup()
turtle.goto(240, 300)
turtle.pendown()
turtle.goto(240, -300)
# 隐藏画笔
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
阅读全文