首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在HTML5画布中创建带有棋子的棋盘格

在HTML5画布中创建带有棋子的棋盘格
EN

Stack Overflow用户
提问于 2018-07-27 08:27:26
回答 2查看 574关注 0票数 2

我正在尝试在HTML和JS中使用canvas,并尝试绘制一个棋盘的画布,每个画布的两侧各有16个棋子。我可以创建国际象棋棋盘,但我被困在如何绘制每一面的16个棋子(这些棋子可以是圆圈,所以只有一边有16个红色圆圈,一边有16个蓝色圆圈)。

我不知道为什么这让我如此困惑,我知道你可能只需要一个for循环在特定的坐标处停止,但是在每一边得到不同颜色的块以及在特定的部分停止会给我带来麻烦。

我只是想得到帮助,在我的代码中,我会把棋子放在哪里。如果你能修改我当前的代码,并在你做了修改的地方放置注释,这样我就可以看到了,那将是非常感谢的。

这是我到目前为止用来做跳棋棋盘的东西:

代码语言:javascript
复制
<canvas id="canvas" width="300" height="300"></canvas>

function drawCheckeredBackground(can, nRow, nCol) {
    var ctx = can.getContext("2d");
    var w = can.width;
    var h = can.height;

    nRow = nRow || 8;    
    nCol = nCol || 8;   

    w /= nCol;            
    h /= nRow;            

    for (var i = 0; i < nRow; ++i) {
        for (var j = 0, col = nCol / 2; j < col; ++j) {
            ctx.rect(2 * j * w + (i % 2 ? 0 : w), i * h, w, h);
        }
    }

    ctx.fill();
}

var canvas = document.getElementById("canvas");

drawCheckeredBackground(canvas);

这是我想要的棋盘的样子,每边都有16个棋子,就像这样。我很快就用画图做了这个例子:https://i.imgur.com/BvbxzSZ.png

EN

回答 2

Stack Overflow用户

发布于 2018-07-27 08:58:11

这可能不是最好的解决方案,但它应该提供一些基本的想法,并且可以使用step变量的想法进行调整。很有可能,你需要在获取实际的组件时进行重构。

代码语言:javascript
复制
const drawBoard = (ctx, step) => {
  for (let i = 0; i < 8; i++) {
    for (let j = 0; j < 8; j++) {
      ctx.fillStyle = (i + j) & 1 ? "black" : "white";
      ctx.fillRect(j * step, i * step, step, step);
    }
  }
};

const drawPieces = (ctx, y, color, step) => {
  ctx.fillStyle = color;
  
  for (let i = y; i < 2 * step + y; i += step) {
    for (let j = step / 2; j < 8 * step; j += step) {
      ctx.beginPath();
      ctx.arc(j, i - step / 2, step / 3, 0, Math.PI * 2);
      ctx.fill();
    }
  }
};

const step = 60;
const c = document.createElement("canvas");
c.height = c.width = step * 8;
document.body.appendChild(c);
const ctx = c.getContext("2d");

drawBoard(ctx, step);
drawPieces(ctx, step, "red", step);
drawPieces(ctx, step * 7, "blue", step);

Play with it at JSFiddle

票数 1
EN

Stack Overflow用户

发布于 2018-07-27 23:35:19

代码语言:javascript
复制
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<style>
			body {
				background-color: black;
			}
			
			canvas {
				display: block;
				margin: auto;
				border: solid 1px white;
				border-radius: 10px;
			}
		</style>
	</head>
	
	<body>
		<canvas id="canvas"></canvas>
		<script type="application/javascript">
		
		// Self executing function
		void function() {
		
			// Turn on strict js rules for this scope
			"use strict";
			
			// Classes
			
			function ChessPeice(x,y,radius) {
				this.x = x || 0.0;
				this.y = y || 0.0;
				this.radius = radius || 1.0;
			}
			
			ChessPeice.prototype = {
				tick: function() {
				
				},
				
				render: function(ctx) {
					ctx.moveTo(
						this.x + this.radius,
						this.y
					);
				
					ctx.arc(
						this.x,
						this.y,
						this.radius,
						0.0,
						2.0 * Math.PI,
						false
					);
				}
			};
			
			// Constructor, when called with 'new' creates an object and puts it
			// in the 'this' variable, new properties can then be added to it.
			function Chessboard(width,height) {
				this.boardWidth = width || 1;
				this.boardHeight = height || 1;
				this.tileWidth = this.boardWidth / this.H_TILE_COUNT;
				this.tileHeight = this.boardHeight / this.V_TILE_COUNT;
				this.whitePeices = [];
				this.blackPeices = [];
				
				for (var y = 0; y < 2; ++y) {
					for (var x = 0; x < this.V_TILE_COUNT; ++x) {
						this.whitePeices.push(
							new ChessPeice(
								x * this.tileWidth + (this.tileWidth >> 1),
								y * this.tileHeight + (this.tileHeight >> 1),
								this.CHESS_PIECE_RADIUS
							)
						);
						
						this.blackPeices.push(
							new ChessPeice(
								x * this.tileWidth + (this.tileWidth >> 1),
								(this.V_TILE_COUNT - 1 - y) * this.tileHeight + (this.tileHeight >> 1),
								this.CHESS_PIECE_RADIUS
							)
						);
					}
				}
			}
			
			// Prototype object, all objects created with 'new Chessboard()'
			// will share the properties in the prototype, use it for constant values
			// & class functions
			Chessboard.prototype = {
				H_TILE_COUNT: 8, // How many white & black tiles per axis?
				V_TILE_COUNT: 8,
				
				EDGE_THICKNESS: 10.0,
				EDGE_COLOUR: "#603E11FF",
				WHITE_TILE_COLOUR: "#BBBBBBFF",
				BLACK_TILE_COLOUR: "#555555FF",
				
				CHESS_PIECE_RADIUS: 5.0,
				WHITE_PIECE_COLOUR: "#EEEEEEFF",
				BLACK_PIECE_COLOUR: "#333333FF",
				
				tick: function() {
					// You can add game logic here
				},
				
				render: function(ctx) {
					// Draw white tiles
					var x = 0;
					var y = 0;
					var totalTiles = this.H_TILE_COUNT * this.V_TILE_COUNT;
					
					ctx.fillStyle = this.WHITE_TILE_COLOUR;
					ctx.beginPath();
					
					for (var i = 0; i < totalTiles; ++i) {
						ctx.rect(
							x * this.tileWidth,
							y * this.tileHeight,
							this.tileWidth,
							this.tileHeight
						);
						
						x += 2;
						
						if (x >= this.H_TILE_COUNT) {
							x = this.H_TILE_COUNT - x + 1;
							++y;
						}
					}
					
					ctx.fill();
					
					// Draw black tiles
					x = 1;
					y = 0;
					
					ctx.fillStyle = this.BLACK_TILE_COLOUR;
					ctx.beginPath();
					
					for (var i = 0; i < totalTiles; ++i) {
						ctx.rect(
							x * this.tileWidth,
							y * this.tileHeight,
							this.tileWidth,
							this.tileHeight
						);
						
						x += 2;
						
						if (x >= this.H_TILE_COUNT) {
							x = this.H_TILE_COUNT - x + 1;
							++y;
						}
					}
					
					ctx.fill();
					
					// Draw edge
					ctx.lineWidth = this.EDGE_THICKNESS >> 1;
					ctx.strokeStyle = this.EDGE_COLOUR;
					ctx.beginPath();
					ctx.rect(0,0,this.boardWidth,this.boardHeight);
					ctx.stroke();
					
					// Draw white pieces
					ctx.lineWidth = 2;
					ctx.strokeStyle = "#000000FF";
					ctx.fillStyle = this.WHITE_PIECE_COLOUR;
					ctx.beginPath();
					
					for (var i = 0; i < this.whitePeices.length; ++i) {
						this.whitePeices[i].render(ctx);
					}
					
					ctx.fill();
					ctx.stroke();
					
					// Draw black pieces
					ctx.lineWidth = 2;
					ctx.strokeStyle = "#000000FF";
					ctx.fillStyle = this.BLACK_PIECE_COLOUR;
					ctx.beginPath();
					
					for (var i = 0; i < this.blackPeices.length; ++i) {
						this.blackPeices[i].render(ctx);
					}
					
					ctx.fill();
					ctx.stroke();
				}
			};
			
			// Variables
			var canvasWidth = 160;
			var canvasHeight = 160;
			var canvas = null;
			var ctx = null;
			var board = null;
			
			// Game Loop
			function loop() {
				// Tick (Update game logic)
				board.tick();
				
				// Render
				ctx.fillStyle = "gray";
				ctx.fillRect(0,0,canvasWidth,canvasHeight);
				
				board.render(ctx);
				
				//
				requestAnimationFrame(loop);
			}
			
			// Entry Point (Runs when the page loads)
			onload = function() {
				canvas = document.getElementById("canvas");
				canvas.width = canvasWidth;
				canvas.height = canvasHeight;
				
				ctx = canvas.getContext("2d");
				board = new Chessboard(canvasWidth,canvasHeight);
				
				loop();
			}
		
		}();
		
		</script>
	</body>
</html>

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51549174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档