
在开发基于JavaScript Canvas的游戏时,一个常见的挑战是如何有效地管理多个游戏实体,例如多个敌人。如果每个敌人的位置、速度等属性都依赖于全局变量,那么当游戏中出现多个敌人时,它们将无法独立行动。
考虑以下初始代码片段,它尝试绘制并移动一个敌人:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var x = 0; // 全局X坐标
var y = 0; // 全局Y坐标
var x_add = 2; // 全局X方向速度
var y_add = 2; // 全局Y方向速度
function animate(){
draw();
setTimeout(animate, 10);
};
function draw(){
ctx.clearRect(0,0,1500, 500);
draw_enemy(900, 100, "red", 40, 50); // 绘制一个敌人
};
function draw_enemy(start_x, start_y, fill, w, h){
// 边界检测和速度反转逻辑
if(x + w + start_x >= 1000){ // 注意:这里使用了硬编码的画布宽度
x_add = -2;
}
if(y + h + start_y >= 500){ // 注意:这里使用了硬编码的画布高度
y_add = -2;
}
if(y + start_y <= 0){
y_add = 2;
}
if(x + start_x <= 0){
x_add = 2;
}
x += x_add; // 更新全局X坐标
y += y_add; // 更新全局Y坐标
ctx.fillStyle = fill;
ctx.fillRect(x + start_x, y + start_y, w, h);
};
animate();以及对应的HTML结构:
<html>
<head>
<title>local storage test</title>
<style>
</style>
</head>
<body>
<div style="text-align: center">
<canvas id="canvas" width="1000" height="500" style="border: 1px solid black; padding: 5 px">
</canvas>
</div>
<script src="script.js">
</script>
</body>
</html>这段代码的问题在于,x、y、x_add 和 y_add 都是全局变量。当只绘制一个敌人时,一切正常。然而,如果尝试绘制两个或更多敌人,例如在 draw 函数中调用 draw_enemy 两次,所有敌人都会共享这些全局变量。这意味着:
立即学习“Java免费学习笔记(深入)”;
这种共享全局状态的方式使得每个敌人都无法拥有独立的运动轨迹和行为逻辑,这显然不符合游戏设计的需求。
为了解决上述问题,最佳实践是采用面向对象编程的思想,使用JavaScript的 class 语法来定义敌人的蓝图。通过类,我们可以创建多个独立的敌人实例,每个实例都拥有自己的属性(如位置、速度、颜色、大小等)和方法(如绘制、更新)。
首先,我们创建一个 Enemy 类,它将封装每个敌人的所有相关属性和行为。
class Enemy {
constructor(color, initialX, initialY, width, height, initialVx, initialVy) {
// 为每个敌人实例初始化独立的属性
this.x = initialX !== undefined ? initialX : 50 + Math.random() * (canvas.width - 100);
this.y = initialY !== undefined ? initialY : 50 + Math.random() * (canvas.height - 100);
this.w = width !== undefined ? width : 40;
this.h = height !== undefined ? height : 50;
this.c = color !== undefined ? color : 'red'; // 颜色
this.vx = initialVx !== undefined ? initialVx : 2; // X方向速度
this.vy = initialVy !== undefined ? initialVy : 2; // Y方向速度
}
// 绘制敌人的方法
draw() {
ctx.fillStyle = this.c;
ctx.fillRect(this.x, this.y, this.w, this.h);
}
// 更新敌人状态(位置和边界检测)的方法
update() {
// 边界检测:使用this.x, this.y, this.w, this.h确保是当前实例的属性
if (this.x + this.w >= canvas.width || this.x <= 0) {
this.vx *= -1; // 碰到左右边界反转X方向速度
// 修正位置,防止卡在边界
this.x = this.x + this.w >= canvas.width ? canvas.width - this.w : 0;
}
if (this.y + this.h >= canvas.height || this.y <= 0) {
this.vy *= -1; // 碰到上下边界反转Y方向速度
// 修正位置,防止卡在边界
this.y = this.y + this.h >= canvas.height ? canvas.height - this.h : 0;
}
// 更新位置:使用this.vx, this.vy确保是当前实例的速度
this.x += this.vx;
this.y += this.vy;
this.draw(); // 更新后立即绘制
}
}在 Enemy 类中:
为了管理多个敌人,我们需要一个数组来存储 Enemy 类的实例。
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
let enemies = []; // 存储所有敌人实例的数组
// 创建多个敌人实例并添加到数组中
function createEnemies(count = 5) {
for (let i = 0; i < count; i++) {
// 可以传入不同的参数来创建具有不同初始状态的敌人
enemies.push(new Enemy('red'));
}
}
createEnemies(3); // 创建3个红色敌人
enemies.push(new Enemy('green', 100, 200, 50, 50, 3, -3)); // 创建一个绿色敌人,并指定其初始状态
enemies.push(new Enemy('blue', 500, 150, 60, 40, -2, 4)); // 创建一个蓝色敌人现在,游戏的主循环(animate 和 draw 函数)需要进行修改,以便遍历 enemies 数组中的所有敌人实例,并分别调用它们的 update 方法。
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除整个画布
// 遍历敌人数组,对每个敌人实例调用其update方法
enemies.forEach(enemy => enemy.update());
}
function animate() {
draw();
// 推荐使用 requestAnimationFrame() 替代 setTimeout(),以获得更流畅的动画
// requestAnimationFrame(animate);
setTimeout(animate, 10);
}
animate(); // 启动动画HTML (index.html):
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Canvas 游戏:多敌人管理</title>
<style>
body { margin: 0; overflow: hidden; display: flex; justify-content: center; align-items: center; min-height: 100vh; background-color: #f0f0f0; }
canvas { border: 1px solid black; background-color: #eee; }
</style>
</head>
<body>
<canvas id="canvas" width="1000" height="500"></canvas>
<script src="script.js"></script>
</body>
</html>JavaScript (script.js):
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
let enemies = []; // 存储所有敌人实例的数组
class Enemy {
constructor(color, initialX, initialY, width, height, initialVx, initialVy) {
this.w = width !== undefined ? width : 40;
this.h = height !== undefined ? height : 50;
this.x = initialX !== undefined ? initialX : Math.random() * (canvas.width - this.w);
this.y = initialY !== undefined ? initialY : Math.random() * (canvas.height - this.h);
this.c = color !== undefined ? color : 'red';
this.vx = initialVx !== undefined ? initialVx : 2 * (Math.random() > 0.5 ? 1 : -1); // 随机初始速度方向
this.vy = initialVy !== undefined ? initialVy : 2 * (Math.random() > 0.5 ? 1 : -1);
}
draw() {
ctx.fillStyle = this.c;
ctx.fillRect(this.x, this.y, this.w, this.h);
}
update() {
// 边界检测和速度反转
if (this.x + this.w >= canvas.width) {
this.x = canvas.width - this.w; // 修正位置
this.vx *= -1;
}
if (this.x <= 0) {
this.x = 0; // 修正位置
this.vx *= -1;
}
if (this.y + this.h >= canvas.height) {
this.y = canvas.height - this.h; // 修正位置
this.vy *= -1;
}
if (this.y <= 0) {
this.y = 0; // 修正位置
this.vy *= -1;
}
this.x += this.vx;
this.y += this.vy;
this.draw();
}
}
// 创建多个敌人实例
function createEnemies(count = 5) {
for (let i = 0; i < count; i++) {
enemies.push(new Enemy('red')); // 默认创建红色敌人
}
}
createEnemies(3); // 创建3个红色敌人
enemies.push(new Enemy('green', 100, 200, 50, 50, 3, -3)); // 创建一个绿色敌人,并指定其初始状态
enemies.push(new Enemy('blue', 500, 150, 60, 40, -2, 4)); // 创建一个蓝色敌人
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
enemies.forEach(enemy => enemy.update()); // 遍历并更新所有敌人
}
function animate() {
draw();
// 推荐使用 requestAnimationFrame() 替代 setTimeout() 以获得更流畅的动画
requestAnimationFrame(animate);
// setTimeout(animate, 10);
}
animate();通过采用面向对象的设计方法,利用JavaScript的 class 语法,我们可以为游戏中的每个实体创建独立的实例。每个实例都拥有自己的状态和行为,并通过一个数组进行统一管理。这种方法不仅解决了多实体共享全局状态的问题,使得每个敌人都能独立运动,还大大提高了代码的模块化、可读性和可维护性,为构建更复杂、更动态的Canvas游戏奠定了坚实的基础。
以上就是JavaScript Canvas 游戏:使用类管理多个敌人实例的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号