用 canvas 做一個坦克大戰的最小可玩版本,其實比想像中簡單。這個範例先只求兩件事:坦克能聽玩家的按鍵移動,按空白鍵(這裡用 J)能發出一顆會飛的子彈,同時場上有幾台敵車待著。把這個骨架跑通,再往上加碰撞、地圖與敵人 AI 就很順了。
整個遊戲畫面就是一個「重繪迴圈」。用 setInterval 每隔一段時間清空畫布,然後依序畫出我方坦克、子彈、敵方坦克。子彈之所以看起來在飛,只是因為每次重繪時它的座標都變了——這是最關鍵的心智模型。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>HTML5 經典坦克大戰</title>
</head>
<body onkeydown="getCommand();">
<h1>HTML5-經典的坦克大戰</h1>
<!-- 戰場 -->
<canvas id="tankMap" width="400" height="300" style="background-color:black"></canvas>
<span id="info">資料輸出</span>
<script type="text/javascript" src="tank.js"></script>
<script type="text/javascript">
// 取得畫布與繪圖內容(可以理解成畫筆)
var canvas = document.getElementById("tankMap");
var cxt = canvas.getContext("2d");
// 我方坦克,參數:x, y, 方向, 顏色
var hero = new Hero(140, 140, 0, heroColor);
// 我方子彈,初始為空
var heroBullet = null;
// 敵方坦克用陣列管理,方便日後擴充數量
var enemyTanks = new Array();
for (var i = 0; i < 3; i++) {
// 0 上、1 右、2 下、3 左
enemyTanks[i] = new EnemyTank((i + 1) * 50, 0, 2, enemyColor);
}
FlashTankMap();
// 專門負責刷新戰場:我方坦克、子彈、敵方坦克都靠它畫出來
function FlashTankMap() {
cxt.clearRect(0, 0, 400, 300);
drawTank(hero);
drawHeroBullet();
for (var i = 0; i < enemyTanks.length; i++) {
drawTank(enemyTanks[i]);
}
}
// 接收玩家按鍵
function getCommand() {
var code = event.keyCode; // W A S D 對應的 ASCII 碼
switch (code) {
case 87: hero.moveUp(); break; // W
case 68: hero.moveRight(); break; // D
case 83: hero.moveDown(); break; // S
case 65: hero.moveLeft(); break; // A
case 74: hero.shotEnemy(); break; // J 發射
}
FlashTankMap();
}
// 每 100 毫秒刷新一次戰場
window.setInterval("FlashTankMap()", 100);
</script>
</body>
</html>
// 顏色以陣列管理:0 為車身,1 為砲塔與砲管
var heroColor = new Array("#BA9658", "#FEF26E");
var enemyColor = new Array("#00A2B5", "#00FEFE");
// ---------- 子彈類 ----------
function Bullet(x, y, direct, speed) {
this.x = x;
this.y = y;
this.direct = direct;
this.speed = speed;
this.timer = null;
this.isLive = true;
this.run = function () {
// 撞到邊界就銷毀
if (this.x <= 0 || this.x >= 400 || this.y <= 0 || this.y >= 300) {
window.clearInterval(this.timer);
this.isLive = false;
return;
}
switch (this.direct) {
case 0: this.y -= this.speed; break; // 上
case 1: this.x += this.speed; break; // 右
case 2: this.y += this.speed; break; // 下
case 3: this.x -= this.speed; break; // 左
}
document.getElementById("info").innerText =
"子彈 x = " + this.x + " 子彈 y = " + this.y;
};
}
// ---------- 坦克基礎類 ----------
function Tank(x, y, direct, color) {
this.x = x;
this.y = y;
this.speed = 1;
this.direct = direct;
this.color = color;
this.moveUp = function () { this.y -= this.speed; this.direct = 0; };
this.moveRight = function () { this.x += this.speed; this.direct = 1; };
this.moveDown = function () { this.y += this.speed; this.direct = 2; };
this.moveLeft = function () { this.x -= this.speed; this.direct = 3; };
}
// ---------- 我方坦克 ----------
function Hero(x, y, direct, color) {
// 以「對象冒充」達到繼承效果
this.tank = Tank;
this.tank(x, y, direct, color);
// 子彈的出生點要依坦克方向做偏移
this.shotEnemy = function () {
switch (this.direct) {
case 0: heroBullet = new Bullet(this.x + 9, this.y, this.direct, 1); break;
case 1: heroBullet = new Bullet(this.x + 30, this.y + 9, this.direct, 1); break;
case 2: heroBullet = new Bullet(this.x + 9, this.y + 30, this.direct, 1); break;
case 3: heroBullet = new Bullet(this.x, this.y + 9, this.direct, 1); break;
}
// 50 毫秒推進一次,是實測後較順的數值
heroBullet.timer = window.setInterval("heroBullet.run()", 50);
};
}
// ---------- 敵方坦克 ----------
function EnemyTank(x, y, direct, color) {
this.tank = Tank;
this.tank(x, y, direct, color);
}
// ---------- 繪製子彈 ----------
function drawHeroBullet() {
if (heroBullet != null && heroBullet.isLive) {
cxt.fillStyle = "#FEF26E";
cxt.fillRect(heroBullet.x, heroBullet.y, 2, 2);
}
}
// ---------- 繪製坦克 ----------
function drawTank(tank) {
switch (tank.direct) {
case 0: // 朝上
case 2: // 朝下
cxt.fillStyle = tank.color[0];
cxt.fillRect(tank.x, tank.y, 5, 30); // 左履帶
cxt.fillRect(tank.x + 15, tank.y, 5, 30); // 右履帶
cxt.fillRect(tank.x + 6, tank.y + 5, 8, 20); // 車身
cxt.fillStyle = tank.color[1];
cxt.arc(tank.x + 10, tank.y + 15, 4, 0, 360, true); // 砲塔
cxt.fill();
cxt.strokeStyle = tank.color[1]; // 砲管
cxt.lineWidth = 1.5;
cxt.beginPath();
cxt.moveTo(tank.x + 10, tank.y + 15);
if (tank.direct === 0) cxt.lineTo(tank.x + 10, tank.y);
else if (tank.direct === 2) cxt.lineTo(tank.x + 10, tank.y + 30);
cxt.closePath();
cxt.stroke();
break;
case 1: // 朝右
case 3: // 朝左
cxt.fillStyle = tank.color[0];
cxt.fillRect(tank.x, tank.y, 30, 5);
cxt.fillRect(tank.x, tank.y + 15, 30, 5);
cxt.fillRect(tank.x + 5, tank.y + 6, 20, 8);
cxt.fillStyle = tank.color[1];
cxt.arc(tank.x + 15, tank.y + 10, 4, 0, 360, true);
cxt.fill();
cxt.strokeStyle = tank.color[1];
cxt.lineWidth = 1.5;
cxt.beginPath();
cxt.moveTo(tank.x + 15, tank.y + 10);
if (tank.direct === 1) cxt.lineTo(tank.x + 30, tank.y + 10);
else if (tank.direct === 3) cxt.lineTo(tank.x, tank.y + 10);
cxt.closePath();
cxt.stroke();
break;
}
}
for (var i = 0; i < 3; i++) 的 3 換成一個設定值,就能調整難度。這份程式碼的價值不在畫面,而在它把「重繪迴圈 + 座標狀態 + 定時器」這個遊戲開發的基本骨架攤開來給你看。理解之後,換成飛機、方塊或任何 2D 場景,套路都一樣。