古老的五子棋

news2025/7/19 11:21:24

午休忽然想起我奶奶喜欢下的一种古老的五子棋游戏,于是花了半小时开发出来了~

源代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>棋盘图案</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #faf1c0;
        }
        canvas {
            background-color: #DAA520; /* 黄棕色 */
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <canvas id="chessboard" width="600" height="600"></canvas>

    <script>
        const canvas = document.getElementById('chessboard');
        const ctx = canvas.getContext('2d');

        // 棋子信息存储
        let pieces = [];

        // 棋盘信息存储
        let board = { size: canvas.width, margin: canvas.width * 0.05, step: (canvas.width - canvas.width * 0.1) / 4 };
        board.drawableSize = board.size - 2 * board.margin;

        // 当前拖拽的棋子
        let currentPiece = null;

        function drawPiece(x, y, color, isDragging = false) {
            const radius = board.size / 28;  // 棋子的半径,可以根据需要调整
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, Math.PI * 2);
            ctx.fillStyle = color;
            ctx.fill();
            if (isDragging) {
                ctx.strokeStyle = 'red';
                ctx.stroke();
            }
            ctx.closePath();
            return { x, y, radius, color };
        }

        function drawChessboard() {
            ctx.clearRect(0, 0, board.size, board.size);
            for (let i = 0; i <= 4; i++) {
                ctx.moveTo(board.margin, board.margin + i * board.step);
                ctx.lineTo(board.margin + board.drawableSize, board.margin + i * board.step);
                ctx.moveTo(board.margin + i * board.step, board.margin);
                ctx.lineTo(board.margin + i * board.step, board.margin + board.drawableSize);
            }
            let size = board.size
            let step = board.step
            let margin =  board.margin

            // 画对角线
            ctx.moveTo(margin, margin);
            ctx.lineTo(size-margin, size-margin);

            ctx.moveTo(margin, size-margin);
            ctx.lineTo(size-margin, margin);

            ctx.moveTo(margin, 2*step+margin);
            ctx.lineTo(2 * step+margin, size-margin);

            ctx.moveTo(2*step+margin, margin);
            ctx.lineTo(size-margin, 2 * step+margin);

            ctx.moveTo(margin, 2 * step+margin);
            ctx.lineTo(2 * step+margin, margin);

            ctx.moveTo(2*step+margin, size-margin);
            ctx.lineTo(size-margin, 2*step+margin);
            ctx.stroke();
        
            ctx.stroke();
        }

        // 初始化棋子
        function initPieces() {
            pieces = [];
            // 画白色棋子
            for (let i = 0; i <= 4; i++) {
                pieces.push(drawPiece(i * board.step + board.margin, board.margin, 'white'));
            }

            // 画黑色棋子
            for (let i = 0; i <= 4; i++) {
                pieces.push(drawPiece(i * board.step + board.margin, board.size - board.margin, 'black'));
            }
        }

        // 检测坐标是否在棋子上
        function isPieceUnderCoordinate(x, y, piece) {
            const distance = Math.sqrt((x - piece.x) ** 2 + (y - piece.y) ** 2);
            return distance < piece.radius;
        }

        // 开始拖拽
        canvas.onmousedown = function(event) {
            const rect = canvas.getBoundingClientRect();
            const x = event.clientX - rect.left;
            const y = event.clientY - rect.top;
            for (const piece of pieces) {
                if (isPieceUnderCoordinate(x, y, piece)) {
                    currentPiece = piece;
                    break;
                }
            }
        };

        // 拖拽移动
        canvas.onmousemove = function(event) {
            if (currentPiece) {
                const rect = canvas.getBoundingClientRect();
                const x = event.clientX - rect.left;
                const y = event.clientY - rect.top;
                drawChessboard();
                for (const piece of pieces) {
                    if (piece === currentPiece) {
                        drawPiece(x, y, piece.color, true);
                    } else {
                        drawPiece(piece.x, piece.y, piece.color);
                    }
                }
            }
        };

        // 放下棋子
        canvas.onmouseup = function(event) {
            if (currentPiece) {
                const rect = canvas.getBoundingClientRect();
                const x = event.clientX - rect.left;
                const y = event.clientY - rect.top;

                // 计算最接近的交点
                const closestX = Math.round((x - board.margin) / board.step) * board.step + board.margin;
                const closestY = Math.round((y - board.margin) / board.step) * board.step + board.margin;

                // 更新棋子位置
                currentPiece.x = closestX;
                currentPiece.y = closestY;

                drawChessboard();
                for (const piece of pieces) {
                    drawPiece(piece.x, piece.y, piece.color);
                }
            }
            currentPiece = null;
        };
        // 双击切换棋子颜色
        canvas.ondblclick = function(event) {
            const rect = canvas.getBoundingClientRect();
            const x = event.clientX - rect.left;
            const y = event.clientY - rect.top;
            for (const piece of pieces) {
                if (isPieceUnderCoordinate(x, y, piece)) {
                    // 切换颜色
                    piece.color = piece.color === 'white' ? 'black' : 'white';

                    // 重新绘制棋盘和棋子
                    drawChessboard();
                    for (const piece of pieces) {
                        drawPiece(piece.x, piece.y, piece.color);
                    }
                    break;
                }
            }
        };

        drawChessboard();
        initPieces();
    </script>
</body>
</html>

效果图:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1158464.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

iframe 给嵌入的 vue页面发送数据(vue2、vue3 都ok)

需求描述 1.有一个网页A&#xff0c;还有一个vue项目B 2.网页A中 使用 iframe 的方式引入了vue项目B 3.现在需要在网页A中 给 vue项目B 发送消息&#xff0c;让vue项目B 做出响应动作。案例代码 iframe 端 这就是一个普通的网页 <!DOCTYPE html> <html lang"en…

踩坑日记之:last-of-type选中的不是最后一个元素

有一说一&#xff0c;这个子类元素选择器太变态了…实际效果不是自己所想。先记录一下&#xff0c;还没想出来解决办法。 结果是&#xff0c;不止最后一个元素被选中了&#xff0c;其他li里面的最后一个元素也被选中了&#xff0c;li内部的<a>也被选中了

如何在idea中使用maven搭建tomcat环境

目录 一、创建maven项目 二、完善代码结构 三、引入依赖和插件 四、启动tomcat&#xff0c;运行项目 &#xff08;1&#xff09;点击添加配置 &#xff08;2&#xff09;点击左上角的加号&#xff0c;选择maven &#xff08;3&#xff09;输入运行命令 五、验证 一、创建…

4.4 多态

思维导图&#xff1a; 4.4 多态 在深入研究对象的世界后&#xff0c;我们已经学到了封装和继承。接下来&#xff0c;我们要讨论的是面向对象编程的另一个核心概念&#xff1a;多态。 4.4.1 多态概述 多态是面向对象的三大特性之一&#xff0c;它允许我们以统一的方式处理不同…

在IDEA运行spark程序(搭建Spark开发环境)

建议大家写在Linux上搭建好Hadoop的完全分布式集群环境和Spark集群环境&#xff0c;以下在IDEA中搭建的环境仅仅是在window系统上进行spark程序的开发学习&#xff0c;在window系统上可以不用安装hadoop和spark&#xff0c;spark程序可以通过pom.xml的文件配置&#xff0c;添加…

学生成绩这样分发

作为一名老师&#xff0c;经常被问到这样的问题&#xff1a;“老师&#xff0c;我的成绩什么时候发&#xff1f;”、“老师&#xff0c;我的成绩出来了吗&#xff1f;”等等。倍感烦恼&#xff0c;需要花费时间来回答这些问题&#xff0c;而且有时候学生还会因为成绩不佳而抱怨…

前端Vue选项卡tab切换【切换传不同参数】

目录 实例1&#xff1a;源码如下实例2&#xff1a;实战1&#xff1a;图片如下实例2&#xff1a;实战1&#xff1a;源码如下实例3&#xff1a;实战2&#xff1a;图片如下实例3&#xff1a;实战2&#xff1a;源码如下最后 适用于所有前端&#xff0c;用uniapp写的 实例1&#xff…

【k8s】pod进阶

一、资源限制 1、资源限制的概念 当定义 Pod 时可以选择性地为每个容器设定所需要的资源数量。 最常见的可设定资源是 CPU 和内存大小&#xff0c;以及其他类型的资源。 当为 Pod 中的容器指定了 request 资源时&#xff0c;调度器就使用该信息来决定将 Pod 调度到哪个节点上…

pt权重转onnx记录

方法: 1.一般YOLO 会自带一个权重pt转onnx模型的代码 export.py 2.打开export.py &#xff0c;我们运行该文件可以通过命令行运行&#xff0c;对照修改argument参数即可 3.运行命令行如下&#xff1a; python ./models/export.py --weights ./models/best.pt weights&#x…

1、Flink基础概念

1、基础知识 &#xff08;1&#xff09;、数据流上的有状态计算 &#xff08;2&#xff09;、框架和分布式处理引擎&#xff0c;用于对无界和有界数据流进行有状态计算。 &#xff08;3&#xff09;、事件驱动型应用&#xff0c;有数据流就进行处理&#xff0c;无数据流就不…

探路、寻解,SaaS企业的“中国路径”在哪?

当下的中国SaaS企业&#xff0c;需要建立的是一个可持续增长模式。 这种可持续增长是基于企业所在赛道的价值思考&#xff0c;即相信SaaS能给企业带来的价值&#xff0c;基于需求不断打磨产品&#xff0c;寻找优质的企业进行服务&#xff0c;与优质企业共生共长&#xff0c;同…

OpenCV学习(七)——图像阈值

图像阈值 7.1 简单阈值7.2 自适应阈值 7.1 简单阈值 cv2.threshold(src, thresh, maxval, type, dstNone)图像阈值 src&#xff1a;源图像&#xff0c;应该为灰度图。thresh&#xff1a;阈值&#xff0c;像素值小于阈值&#xff0c;则将其设置为0&#xff0c;否则将其设置为最…

使用 Curl 和 DomCrawler 下载抖音视频链接并存储到指定文件夹

项目需求 假设我们需要从抖音平台上下载一些特定的视频&#xff0c;以便进行分析、编辑或其他用途。为了实现这个目标&#xff0c;我们需要编写一个爬虫程序来获取抖音视频的链接&#xff0c;并将其保存到本地文件夹中。 目标分析 在开始编写爬虫之前&#xff0c;我们需要了…

2021年09月 Python(三级)真题解析#中国电子学会#全国青少年软件编程等级考试

Python等级考试&#xff08;1~6级&#xff09;全部真题・点这里 一、单选题&#xff08;共25题&#xff0c;每题2分&#xff0c;共50分&#xff09; 第1题 使用map函数可以实现列表数据元素类型的转换&#xff0c;而无需通过循环。则将列表L[‘1’,‘3’,‘5’,‘7’,‘9’]转…

deeplog打印损失

deeplog 即 日志异常检测工具&#xff0c;该工具使用torchtrain整合训练代码 step1 查找文件 找到安装包 torchtrain, 打开 module.py 文件 删除文件夹 __pycache__ step2 修改文件 # Loop over each epochdef fit(self, X, y,epochs 10,batch_size 32,learn…

一图看懂CodeArts Release三大特性,带你玩转发布管理服务

体验通道&#xff1a; 发布管理 CodeArts Release-华为云

基于深度学习的语音识别算法的设计与实现

收藏和点赞&#xff0c;您的关注是我创作的动力 文章目录 概要 一、课题内容二、需求分析2.1 算法需求分析2.2 语音录制2.3 声学模型2.4 语言模型2.5 训练集和测试集2.6 深度神经网络 三 算法设计原理3.1 语音识别系统3.1.1 声学模型3.1.2 语言模型3.1.3 发音词典 四 简单问答…

树结构及其算法-二叉树节点的插入

目录 树结构及其算法-二叉树节点的插入 C代码 树结构及其算法-二叉树节点的插入 二叉树节点插入的情况和查找相似&#xff0c;重点是插入后仍要保持二叉查找树的特性。如果插入的节点已经在二叉树中&#xff0c;就没有插入的必要了&#xff0c;如果插入的值不在二叉树中&…

AI开源 - LangChain UI 之 Flowise

原文&#xff1a;AI开源 - LangChain UI 之 Flowise 一、Flowise 简介 Flowise 是一个为 LangChain 设计的用户界面(UI)&#xff0c;使得使用 LangChain 变得更加容易&#xff08;低代码模式&#xff09;。 通过拖拽可视化的组件&#xff0c;组建工作流&#xff0c;就可以轻…

基于GB28181-2022实现web无插件播放H265视频

目前发布的GB28181-2022增加了对前端设备视频H265编码格式的支持&#xff0c;所以实现国标平台通过浏览器对H265视频流的无插件的解码播放将是未来的趋势。 目前大多的方案都是通过平台端把H265转码为H264&#xff0c;再推送到web前端进行解码播放&#xff0c;这种方式因为需要…