您好,登錄后才能下訂單哦!
HTML5 利用canvas構(gòu)建 Web五子棋游戲程序設(shè)計
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- <style type="text/css">
- body {
- margin: 10px;
- }
- </style>
- <script type="text/javascript">
- var canvas;
- var context;
- var isWhite = true;//設(shè)置是否該輪到白棋
- var isWell = false;//設(shè)置該局棋盤是否贏了,如果贏了就不能再走了
- var img_b = new Image();
- img_b.src = "p_w_picpaths/b.png";//白棋圖片
- var img_w = new Image();
- img_w.src = "p_w_picpaths/w.png";//黑棋圖片
- var chessData = new Array(15);//這個為棋盤的二維數(shù)組用來保存棋盤信息,初始化0為沒有走過的,1為白棋走的,2為黑棋走的
- for (var x = 0; x < 15; x++) {
- chessData[x] = new Array(15);
- for (var y = 0; y < 15; y++) {
- chessData[x][y] = 0;
- }
- }
- function drawRect() {//頁面加載完畢調(diào)用函數(shù),初始化棋盤
- canvas = document.getElementById("canvas");
- context = canvas.getContext("2d");
- for (var i = 0; i <= 640; i += 40) {//繪制棋盤的線
- context.beginPath();
- context.moveTo(0, i);
- context.lineTo(640, i);
- context.closePath();
- context.stroke();
- context.beginPath();
- context.moveTo(i, 0);
- context.lineTo(i, 640);
- context.closePath();
- context.stroke();
- }
- }
- function play(e) {//鼠標(biāo)點(diǎn)擊時發(fā)生
- var x = parseInt((e.clientX - 20) / 40);//計算鼠標(biāo)點(diǎn)擊的區(qū)域,如果點(diǎn)擊了(65,65),那么就是點(diǎn)擊了(1,1)的位置
- var y = parseInt((e.clientY - 20) / 40);
- if (chessData[x][y] != 0) {//判斷該位置是否被下過了
- alert("你不能在這個位置下棋");
- return;
- }
- if (isWhite) {
- isWhite = false;
- drawChess(1, x, y);
- }
- else {
- isWhite = true;
- drawChess(2, x, y);
- }
- }
- function drawChess(chess, x, y) {//參數(shù)為,棋(1為白棋,2為黑棋),數(shù)組位置
- if (isWell == true) {
- alert("已經(jīng)結(jié)束了,如果需要重新玩,請刷新");
- return;
- }
- if (x >= 0 && x < 15 && y >= 0 && y < 15) {
- if (chess == 1) {
- context.drawImage(img_w, x * 40 + 20, y * 40 + 20);//繪制白棋
- chessData[x][y] = 1;
- }
- else {
- context.drawImage(img_b, x * 40 + 20, y * 40 + 20);
- chessData[x][y] = 2;
- }
- judge(x, y, chess);
- }
- }
- function judge(x, y, chess) {//判斷該局棋盤是否贏了
- var count1 = 0;
- var count2 = 0;
- var count3 = 0;
- var count4 = 0;
- //左右判斷
- for (var i = x; i >= 0; i--) {
- if (chessData[i][y] != chess) {
- break;
- }
- count1++;
- }
- for (var i = x + 1; i < 15; i++) {
- if (chessData[i][y] != chess) {
- break;
- }
- count1++;
- }
- //上下判斷
- for (var i = y; i >= 0; i--) {
- if (chessData[x][i] != chess) {
- break;
- }
- count2++;
- }
- for (var i = y + 1; i < 15; i++) {
- if (chessData[x][i] != chess) {
- break;
- }
- count2++;
- }
- //左上右下判斷
- for (var i = x, j = y; i >= 0, j >= 0; i--, j--) {
- if (chessData[i][j] != chess) {
- break;
- }
- count3++;
- }
- for (var i = x + 1, j = y + 1; i < 15, j < 15; i++, j++) {
- if (chessData[i][j] != chess) {
- break;
- }
- count3++;
- }
- //右上左下判斷
- for (var i = x, j = y; i >= 0, j < 15; i--, j++) {
- if (chessData[i][j] != chess) {
- break;
- }
- count4++;
- }
- for (var i = x + 1, j = y - 1; i < 15, j >= 0; i++, j--) {
- if (chessData[i][j] != chess) {
- break;
- }
- count4++;
- }
- if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5) {
- if (chess == 1) {
- alert("白棋贏了");
- }
- else {
- alert("黑棋贏了");
- }
- isWell = true;//設(shè)置該局棋盤已經(jīng)贏了,不可以再走了
- }
- }
- </script>
- </head>
- <body onload="drawRect()">
- <div>
- <canvas width="640" id="canvas" onmousedown="play(event)" height="640">你的瀏覽器不支持HTML5 canvas ,請使用 google chrome 瀏覽器 打開.
- </canvas>
- </div>
- </body>
- </html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。