Python画一个象棋棋盘
时间: 2023-03-28 10:04:23 浏览: 123
guojixiangqi.rar_象棋棋盘
可以使用Python的turtle库来画一个象棋棋盘,代码如下:
```python
import turtle
# 设置棋盘大小
board_size = 400
# 设置格子大小
grid_size = board_size // 9
# 初始化画笔
pen = turtle.Turtle()
pen.speed()
pen.hideturtle()
# 画棋盘边框
pen.penup()
pen.goto(-board_size // 2, board_size // 2)
pen.pendown()
pen.goto(board_size // 2, board_size // 2)
pen.goto(board_size // 2, -board_size // 2)
pen.goto(-board_size // 2, -board_size // 2)
pen.goto(-board_size // 2, board_size // 2)
# 画横线
for i in range(8):
pen.penup()
pen.goto(-board_size // 2, board_size // 2 - grid_size * (i + 1))
pen.pendown()
pen.goto(board_size // 2, board_size // 2 - grid_size * (i + 1))
# 画竖线
for i in range(8):
pen.penup()
pen.goto(-board_size // 2 + grid_size * (i + 1), board_size // 2)
pen.pendown()
pen.goto(-board_size // 2 + grid_size * (i + 1), -board_size // 2)
# 画九宫格
pen.penup()
pen.goto(-board_size // 2 + grid_size * 3, board_size // 2)
pen.pendown()
pen.goto(-board_size // 2 + grid_size * 5, board_size // 2)
pen.goto(-board_size // 2 + grid_size * 5, board_size // 2 - grid_size * 2)
pen.goto(-board_size // 2 + grid_size * 3, board_size // 2 - grid_size * 2)
pen.goto(-board_size // 2 + grid_size * 3, board_size // 2)
pen.penup()
pen.goto(-board_size // 2 + grid_size * 3, -board_size // 2 + grid_size * 2)
pen.pendown()
pen.goto(-board_size // 2 + grid_size * 5, -board_size // 2 + grid_size * 2)
pen.goto(-board_size // 2 + grid_size * 5, -board_size // 2)
pen.goto(-board_size // 2 + grid_size * 3, -board_size // 2)
pen.goto(-board_size // 2 + grid_size * 3, -board_size // 2 + grid_size * 2)
# 画楚河汉界
pen.penup()
pen.goto(-board_size // 2 + grid_size * 3, board_size // 2 - grid_size * 4)
pen.pendown()
pen.goto(-board_size // 2 + grid_size * 5, board_size // 2 - grid_size * 6)
pen.penup()
pen.goto(-board_size // 2 + grid_size * 5, board_size // 2 - grid_size * 4)
pen.pendown()
pen.goto(-board_size // 2 + grid_size * 3, board_size // 2 - grid_size * 6)
# 隐藏画笔
pen.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
运行以上代码,即可在窗口中看到一个完整的象棋棋盘。
阅读全文