You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用graphics模块创建跳棋棋盘(禁止使用数组等)

用graphics模块制作跳棋棋盘实现方案

你需要严格使用graphics模块绘制8x8的红黑跳棋棋盘,且不能用数组,以下是完整实现代码:

代码修正与完整实现

原代码存在格式问题,修正后补充完整棋盘绘制逻辑:

from graphics import *

# 创建图形窗口
def graphics_window():
    win = GraphWin('Checkers', 600, 600)
    win.setBackground("white")
    return win

# 绘制跳棋棋盘
def Checkersboard():
    win = graphics_window()
    square_size = 600 // 8  # 8x8棋盘适配600x600窗口,每个方块边长75像素
    
    # 逐行逐列绘制方块
    for i in range(8):
        for j in range(8):
            # 计算当前方块的对角坐标
            x1 = j * square_size
            y1 = i * square_size
            x2 = x1 + square_size
            y2 = y1 + square_size
            
            # 按行列奇偶性切换颜色
            if (i + j) % 2 == 0:
                fill_color = "black"
            else:
                fill_color = "red"
            
            # 绘制并填充方块
            square = Rectangle(Point(x1, y1), Point(x2, y2))
            square.setFill(fill_color)
            square.draw(win)
    
    win.getMouse()
    win.close()

# 启动程序
Checkersboard()

核心逻辑说明

  • 用嵌套循环遍历8行8列,通过坐标计算逐个绘制方块,全程无需数组存储棋盘数据
  • 依靠(行号+列号)的奇偶性判断配色,实现红黑方块交替排列的棋盘效果
  • 固定方块尺寸为窗口边长的1/8,确保8x8方块刚好填满整个窗口

内容的提问来源于stack exchange,提问作者imaan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 03:21:36