博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
团队-象棋游戏-开发文档
阅读量:5891 次
发布时间:2019-06-19

本文共 5325 字,大约阅读时间需要 17 分钟。

项目托管平台地址:https://gitee.com/zixiao520/Chinesechess/blob/master/                     象棋游戏.zip

我负责的是绘制棋盘棋子,并把棋子放到各自起始位置

成品图:

 

1 function Board(ctx, cfg){  2     this.width = cfg.width;  3     this.height = cfg.height;  4     this.margin = cfg.margin ? cfg.margin : 0;  5     this.space = (this.width-2*this.margin)/8;  6     this.array = [[],[],[],[],[],[],[],[],[],[]];  7     this.step = 0;  8     this.steps = [];  9     this.init(ctx) 10 } 11 Board.prototype.init = function(ctx){ 12     var ctx = canvas.getContext("2d"); 13     var i, j, x; 14     //象棋位置数组 15     var xiangqi = [ 16         [Rook, Knight, Elephant, Mandarin, King], 17         [0, 0, 0, 0, 0], 18         [0, Cannon, 0, 0, 0], 19         [Pawn, 0, Pawn, 0, Pawn], 20         [0, 0, 0, 0, 0], 21         [0, 0, 0, 0, 0], 22         [Pawn, 0, Pawn, 0, Pawn], 23         [0, Cannon, 0, 0, 0], 24         [0, 0, 0, 0, 0], 25         [Rook, Knight, Elephant, Mandarin, King] 26     ]; 27     for(i=0; i<9; i++){ 28         x = i>4 ? (i-(i-4)*2) : i; 29         for(j=0; j<10; j++){ 30             this.array[i][j] = {}; 31             this.array[i][j].point = { 32                 x:this.margin+this.space*i, 33                 y:this.margin+this.space*j 34             }; 35             this.array[i][j].xiangqi =  xiangqi[j][x] != 0 ? new xiangqi[j][x](j > 4 ? 0 : 1) : 0; 36         } 37     } 38     //画棋盘 39     this.draw(ctx); 40     //落子 41     this.locate(ctx); 42     //事件 43     this.initEvents(canvas); 44     this.current = false; 45 } 46 Board.prototype.draw = function(ctx){ 47     var i; 48     ctx.beginPath(); 49     ctx.strokeStyle = "rgb(0, 0, 0)"; 50     //frame 51     ctx.moveTo(0, 0); 52     ctx.lineTo(this.width, 0); 53     ctx.lineTo(this.width, this.height); 54     ctx.lineTo(0, this.height); 55     ctx.lineTo(0, 0); 56     ctx.stroke(); 57     //horizontal lines 58     for(i=0; i<10; i++){ 59         ctx.moveTo(this.margin, (i*this.space)+this.margin); 60         ctx.lineTo(this.margin+this.space*8, (i*this.space)+this.margin); 61     } 62     //vertical lines 63     for(i=0; i<9; i++){ 64         ctx.moveTo((i*this.space)+this.margin, this.margin); 65         ctx.lineTo((i*this.space)+this.margin, this.margin+this.space*4); 66         ctx.moveTo((i*this.space)+this.margin, this.margin+this.space*5); 67         ctx.lineTo((i*this.space)+this.margin, this.margin+this.space*9); 68     } 69     ctx.stroke(); 70  71     //the path of mandarins 72     ctx.moveTo(this.margin+3*this.space, this.margin); 73     ctx.lineTo(this.margin+5*this.space, this.margin+2*this.space); 74     ctx.moveTo(this.margin+3*this.space, this.margin+7*this.space); 75     ctx.lineTo(this.margin+5*this.space, this.margin+9*this.space); 76     ctx.moveTo(this.margin+5*this.space, this.margin); 77     ctx.lineTo(this.margin+3*this.space, this.margin+2*this.space); 78     ctx.moveTo(this.margin+5*this.space, this.margin+7*this.space); 79     ctx.lineTo(this.margin+3*this.space, this.margin+9*this.space); 80     ctx.stroke(); 81     var drawMark = function(x, y, left, right){ 82         if(right){ 83             //bottom right 84             ctx.moveTo(x+this.space/9+this.space/3, y+this.space/9); 85             ctx.lineTo(x+this.space/9, y+this.space/9); 86             ctx.lineTo(x+this.space/9, y+this.space/9+this.space/3); 87             //top right 88             ctx.moveTo(x+this.space/9+this.space/3, y-this.space/9); 89             ctx.lineTo(x+this.space/9, y-this.space/9); 90             ctx.lineTo(x+this.space/9, y-this.space/9-this.space/3); 91         } 92         if(left){ 93             //top left 94             ctx.moveTo(x-this.space/9-this.space/3, y-this.space/9); 95             ctx.lineTo(x-this.space/9, y-this.space/9); 96             ctx.lineTo(x-this.space/9, y-this.space/9-this.space/3); 97             //bottom left 98             ctx.moveTo(x-this.space/9-this.space/3, y+this.space/9); 99             ctx.lineTo(x-this.space/9, y+this.space/9);100             ctx.lineTo(x-this.space/9, y+this.space/9+this.space/3);101         }102     }103     //paws mark104     for(i=0; i<10; i++){105         var y = this.margin + 3*(i%2)*this.space + 3*this.space;106         var x = this.margin + Math.floor(i/2)*2*this.space;107         drawMark(x, y, i>1, i<8);108     }109     //cannons mark110     for(i=0; i<2; i++){111         var x = this.margin + this.space  +(i%2)*6*this.space;112         var y = this.margin + 2*this.space;113         drawMark(x, y, true, true);114         y = this.margin + 7*this.space;115         drawMark(x, y, true, true);116     }117     ctx.stroke();118     ctx.save();119     //楚河 汉界120     ctx.font = this.space*0.9+"px 宋体";121     ctx.fillStyle = "#000";122     var wordspace = (8*this.space - this.space*0.9*4)/5;123     ctx.fillText("楚", this.margin+wordspace, this.margin+this.space*5-0.2*this.space);124     ctx.fillText("河", this.margin+2*wordspace+this.space*0.9, this.margin+this.space*5-0.2*this.space);125     ctx.rotate(Math.PI);126     ctx.fillText("界", -this.margin-3*wordspace-3*+this.space*0.9, -this.margin-this.space*4-0.2*this.space);127     ctx.fillText("汉", -this.margin-4*wordspace-4*+this.space*0.9, -this.margin-this.space*4-0.2*this.space);128     ctx.restore();129     ctx.closePath();130 }
View Code

编写完成后把代发汇总到队长手里。

转载于:https://www.cnblogs.com/zixiao520/p/7773727.html

你可能感兴趣的文章
Exchange 2013部署系列之(四)DAG配置
查看>>
解决Win8.1系统LYNC共享PPT提示“演示文稿遇到问题”
查看>>
谈谈我第一次如何为 Laravel 贡献源码
查看>>
磁盘空间不足
查看>>
资深编曲人常用的软件是哪款呢?
查看>>
vim插件——rainbow
查看>>
Web开发中 前端路由 实现的几种方式和适用场景
查看>>
python3实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
查看>>
梳理一份机器学习的学习目录
查看>>
Java并发编程:深入剖析ThreadLocal
查看>>
Mac OSX 中java7 java8环境的配置
查看>>
我所理解的JDK自动装箱和拆箱
查看>>
30分钟入门Java
查看>>
elasticsearch学习——环境搭建2
查看>>
Ruby中 局部变量(local variable) 块变量(block variable) 与块局部变量(block local variable)...
查看>>
Android零基础入门第55节:ImageSwitcher和TextSwitcher使用
查看>>
网页被篡改怎么防护?
查看>>
数据科学求职过程中总结的四点经验
查看>>
git代码首次提交
查看>>
mysql安装,远程连接,以及修改密码
查看>>