原生js+canvas写一个五子棋

HTML

    <div id="pox"><canvas style="border:5px solid rgb(43, 42, 42)" width="600" height="600">您的浏览器不支持canvas绘图技术,请更换浏览器</canvas><div id="box"><div id="text"></div><div id="coordinate"></div></div></div>

CSS

    canvas {background-color: rgb(221, 220, 219);float: left;}#box {float: left;}#text{width: 200px;height: 50px;line-height: 50px;text-align: center;background-color: #333;font-size: 15px;color: aliceblue;}

JS

        let myCanvas = document.getElementsByTagName("canvas")[0];let ctx = myCanvas.getContext("2d");for (var i = 0; i <= 12; i++) {ctx.moveTo(0, i * 50)ctx.lineTo(600, i * 50)ctx.lineWidth = 2;ctx.stroke()ctx.beginPath();ctx.moveTo(i * 50, 0)ctx.lineTo(i * 50, 600)ctx.lineWidth = 2;ctx.stroke()ctx.beginPath();}let boo = true;let booArray = [];for (let j = 0; j <= 12; j++) {for (let i = 0; i <= 12; i++) {//存入所有初始坐标booArray.push({x: i * 50,y: j * 50,color: ''})}}let text = document.getElementById("text");if (boo) {text.innerText = "当前执子为黑色,请黑方落子"}else{text.innerText = "当前执子为白色,请白方落子"}// let coordinate = document.getElementById("coordinate");// coordinate.innerText = "上次落点坐标为( 0 , 0 )"myCanvas.onclick = function (e) {let tempx = Math.round(e.offsetX / 50) * 50let tempy = Math.round(e.offsetY / 50) * 50//判断当前点位有没有棋子//#region let booRutern = false;booArray.forEach(item => {if (item.x === tempx && item.y === tempy && item.color != "") {console.log("当前位置有棋子");booRutern = true;}})//#endregionif (booRutern) return// 修改提示文字if (boo) {text.innerText = "当前执子为白色,请白方落子"}else{text.innerText = "当前执子为黑色,请黑方落子"}//更改下次落子颜色boo = !boo;ctx.arc(tempx, tempy, 15, 0, Math.PI / 180 * 360)//记录本次落子颜色let color;if (boo) {ctx.fillStyle = "white"color = "white"ctx.stroke()} else {ctx.fillStyle = "black"color = "black"}//修改落子位置数组对应的颜色booArray.forEach(item => {if (item.x === tempx && item.y === tempy) {item.color = color;}})//横线赢let r = 0;//纵向赢let wh = 0;//一三象限赢let ot = 0;//二四象限赢let tf = 0;//横向判断//#region //横向判断右侧a: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x == tempx + u * 50 && booArray[index].y == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++r++;} else {break a;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {r++} else {break a;}}}}}//横向判断左侧b: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x == tempx - 50 - u * 50 && booArray[index].y == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++r++;} else {break b;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {r++} else {break b;}}}}}//#endregion//纵向判断//#region //纵向判断上方c: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x == tempx && booArray[index].y - 50 - u * 50 == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++wh++;} else {break c;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {wh++} else {break c;}}}}}//纵向判断下方d: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x == tempx && booArray[index].y + u * 50 == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++wh++;} else {break d;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {wh++} else {break d;}}}}}//#endregion// 一三象限判断//#region //一象限判断e: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x + 50 + u * 50 == tempx && booArray[index].y - 50 - u * 50 == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++ot++;} else {break e;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {ot++} else {break e;}}}}}//三象限f: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x - u * 50 == tempx && booArray[index].y + u * 50 == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++ot++;} else {break f;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {ot++} else {break f;}}}}}//#endregion//二四象限判断//#region //二象限判断g: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x - 50 - u * 50 == tempx && booArray[index].y - 50 - u * 50 == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++tf++;} else {break g;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {tf++} else {break g;}}}}}//四象限判断h: for (let u = 0; u < 5; u++) {//tempx+index*50for (let index = 0; index < booArray.length; index++) {if (booArray[index].x + u * 50 == tempx && booArray[index].y + u * 50 == tempy) {if (boo) {//当前是白色console.log(booArray[index], "白色色色,当前是白");if (booArray[index].color == 'white') {// wr++tf++;} else {break h;}} else {//当前是黑色console.log(booArray[index], "黑黑黑黑,当前是黑");if (booArray[index].color == 'black') {tf++} else {break h;}}}}}//#endregionconsole.log("当前:", boo ? "白色" : "黑色", " 横向:", r, " 纵向:", wh, " 一三象限: ", ot, "二四象限: ", tf);ctx.fill()ctx.beginPath()//这里用延时器的目的是为了异步弹窗//因为弹窗的速度比canvas绘制速度快, 有时候会出现还没绘制棋子就弹窗胜利了//看到的人如果有更好的思路可以评论提供setTimeout(() => {//横向if (r >= 5) {if (boo) {alert("白横线胜利");} else {alert("黑横线胜利");}}//纵向if (wh >= 5) {if (boo) {alert("白纵向胜利");} else {alert("黑纵向胜利");}}//一三象限if (ot >= 5) {if (boo) {alert("白一三象限胜利");} else {alert("黑一三象限胜利");}}//二四象限if (tf >= 5) {if (boo) {alert("白二四象限胜利");} else {alert("黑二四象限胜利");}}}, 10)}

canvas画图实现一个简单的五子棋小游戏相关推荐

  1. 教你怎么用c++基本语法实现一个简单的五子棋小游戏

    这个小游戏是在2020年5月份无聊写的,代码量不大,权当娱乐哈 基本思路: 1.创建一个15*15棋盘类,并设计相关函数(输出棋盘,下黑棋,下白棋等) 2.编写judge()函数,判断胜负条件 3.主 ...

  2. 一个简单的五子棋小游戏

    利用c语言编写,在vs2017上编译运行 废话不多说直接上完整代码 #include <stdio.h> //基本输入输出头文件 #include "graphics.h&quo ...

  3. 做一个简单的java小游戏--单机版五子棋

    做一个简单的java小游戏–单机版五子棋 学了java有一段时间了,今天就来搞一个简单的单机版五子棋游戏. 实现功能:那必须能进行基础的输赢判断.还有重新开始的功能,悔棋的功能,先手设置的功能和退出的 ...

  4. python简单小游戏代码_一个简单的python小游戏---七彩同心圆

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 用pygame做一个简单的python小游戏-七彩同心圆 玩法:每次点击鼠标时,会以鼠标为圆心,不断 ...

  5. 用pygame做一个简单的python小游戏---贪吃蛇

    用pygame做一个简单的python小游戏-贪吃蛇 贪吃蛇游戏博客链接:(方法一样,语言不一样) c++贪吃蛇:https://blog.csdn.net/weixin_46791942/artic ...

  6. 用pygame做一个简单的python小游戏---七彩同心圆

    用pygame做一个简单的python小游戏-七彩同心圆 这个小游戏原是我同学python课的课后作业,并不是很难,就简单实现了一下,顺便加强一下pygame库的学习. 玩法:每次点击鼠标时,会以鼠标 ...

  7. 用pygame做一个简单的python小游戏---生命游戏

    用pygame做一个简单的python小游戏-生命游戏 生命游戏(Game of Life) 生命游戏(Game of Life)是剑桥大学约翰·何顿·康威(John Horton Conway)教授 ...

  8. python七彩同心圆_用pygame做一个简单的python小游戏---七彩同心圆

    用pygame做一个简单的python小游戏---七彩同心圆 用pygame做一个简单的python小游戏-七彩同心圆 这个小游戏原是我同学python课的课后作业,并不是很难,就简单实现了一下,顺便 ...

  9. 一个简单的纸牌小游戏

    一个简单的纸牌小游戏 初始化页面布局 function initView(){var html = html2 = '';for(var i=1;i<=10;i++){html += '< ...

最新文章

  1. leveldb源码分析:Open启动流程
  2. android studio撤销按钮,Android Studio无法撤消(Android Studio Can't Undo)
  3. 记录一下git 的常用命令
  4. chap6_2 Parallax mapping in OGRE
  5. C++产生指定范围内的随机数/随机小数
  6. walle多渠道打包+Tinker(bugly)热更新集成+360加固(乐固)
  7. spool命令、创建一个表,创建而且copy表,查看别的用户下的表,rowid行地址 索引的时候使用,表的增删改查,删除表,oracle的回收站...
  8. MongoDb学习(四)--Repository
  9. C#界面控件DotNetBar使用详解
  10. JAVA将多个Pdf合并成一个Pdf
  11. 计算机领域获奖感言,计算机编程比赛获奖感言
  12. 二线制、三线制、四线制,PT100,电桥
  13. 硬件学习笔记(器件篇)—— 电感(二)
  14. NLP的“第四范式”之Prompt Learning总结:44篇论文逐一梳理
  15. 吴恩达:回顾2021,这些大事件影响了AI这一年
  16. 从输入 URL 到页面加载完成的过程中都发生了什么事情?
  17. google海底光缆图_2019全球海底光缆分布图
  18. 私有化部署 给数据安全加把“锁”!
  19. ADS设计日志(一):阻抗变换器详讲
  20. 好用的国产无线蓝牙耳机有哪些?盘点好口碑国产蓝牙耳机

热门文章

  1. Windows用户及组管理
  2. Android N调用系统安装APK方法报错原因整理及解决方案
  3. [Python]-Wechat工具
  4. 【观察】企业级开源软件大时代,PingCAP的格局与胜局
  5. Epic 商店解锁国区;苹果在应用商店反垄断案中败诉,30% 佣金或被削减
  6. 从零开始学AI(数学基础之线性代数和高等数学)
  7. Web中间件常见漏洞总结
  8. transform使用导致元素内字体出现模糊的坑
  9. 计算机专业集齐七龙珠,集齐七颗龙珠 为你召唤一台飞行堡垒8
  10. ChatGPT有话说:虚拟现实 VS 增强现实