新闻中心
J*aScript Canvas 游戏:使用类管理多个敌人实例的教程

问题描述:全局状态与多实体管理
在开发基于J*aScript 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 两次,所有敌人都会共享这些全局变量。这意味着:
- 它们将使用相同的 x 和 y 坐标,导致它们在屏幕上重叠或表现出相同的位移。
- 当任何一个敌人触碰到边界时,它会修改全局的 x_add 或 y_add,从而影响所有其他敌人的移动方向,使得它们的行为变得同步且随机。
这种共享全局状态的方式使得每个敌人都无法拥有独立的运动轨迹和行为逻辑,这显然不符合游戏设计的需求。
解决方案:使用J*aScript类进行面向对象管理
为了解决上述问题,最佳实践是采用面向对象编程的思想,使用J*aScript的 class 语法来定义敌人的蓝图。通过类,我们可以创建多个独立的敌人实例,每个实例都拥有自己的属性(如位置、速度、颜色、大小等)和方法(如绘制、更新)。
1. 定义 Enemy 类
首先,我们创建一个 Enemy 类,它将封装每个敌人的所有相关属性和行为。
青泥AI
青泥学术AI写作辅助平台
360
查看详情
class Enemy {
constructor(color, initialX, ini
tialY, 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 类中:
- constructor 方法用于初始化每个敌人实例的属性,如 x、y(位置)、w、h(宽度、高度)、c(颜色)以及 vx、vy(X、Y方向的速度)。通过 this 关键字,确保这些属性是每个实例独有的。
- draw() 方法负责根据当前实例的 x、y、w、h 和 c 属性在 Canvas 上绘制敌人。
- update() 方法负责更新敌人实例的位置,并执行边界检测。同样,它使用 this 关键字来访问和修改当前实例的属性。
2. 管理多个敌人实例
为了管理多个敌人,我们需要一个数组来存储 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)); // 创建一个蓝色敌人3. 改进游戏循环
现在,游戏的主循环(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>J*aScript 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>J*aScript (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();注意事项与最佳实践
- 使用 canvas.width 和 canvas.height: 在进行边界检测时,始终使用 canvas.width 和 canvas.height 而不是硬编码的数值。这使得你的游戏能够更好地适应不同大小的 Canvas,增加代码的灵活性和可维护性。
- requestAnimationFrame() vs. setTimeout(): 对于游戏循环,强烈推荐使用 requestAnimationFrame() 而非 setTimeout()。requestAnimationFrame() 会在浏览器下一次重绘之前调用指定的回调函数,它与浏览器的刷新率同步,能够提供更平滑、更高效的动画,并节省CPU资源(当页面在后台时会暂停)。
- 构造函数的灵活性: 在 Enemy 类的 constructor 中,你可以根据需要传入更多参数,例如不同的颜色、初始位置、生命值、防御力等。这使得创建各种具有独特属性的敌人变得非常容易。
- 数组迭代方法: forEach 是一种简洁且常用的数组迭代方法,它比传统的 for 循环更具可读性。在处理游戏实体数组时,它是一个很好的选择。
- 碰撞修正: 在边界检测中,除了反转速度外,最好也修正一下对象的位置,将其精确地放置在边界上,以避免在某些帧率下对象“卡”在边界内或穿透边界的问题。示例代码中已加入了简单的位置修正。
总结
通过采用面向对象的设计方法,利用J*aScript的 class 语法,我们可以为游戏中的每个实体创建独立的实例。每个实例都拥有自己的状态和行为,并通过一个数组进行统一管理。这种方法不仅解决了多实体共享全局状态的问题,使得每个敌人都能独立运动,还大大提高了代码的模块化、可读性和可维护性,为构建更复杂、更动态的Canvas游戏奠定了坚实的基础。
以上就是J*aScript Canvas 游戏:使用类管理多个敌人实例的教程的详细内容,更多请关注其它相关文章!
# 全局变量
# 廊坊网站营销推广招商
# 100分seo
# 清河网站建设资讯公司
# 厦门护肤品网站排名优化
# 行业网站建设的建议方案
# 白城seo服务哪个好用
# 鲤城网站推广营销公司
# 网站seo推广平台排名
# 青岛网站建设低价
# 外部seo收入
# 人时
# 自己的
# 推荐使用
# 遍历
# 置顶
# javascript
# 回调
# 创建一个
# 面向对象
# 多个
# overflow
# 重绘
# 常见问题
# 面向对象编程
# 回调函数
# 浏览器
# 编码
# js
# html
# java
相关栏目:
【
科技资讯46185 】
【
网络学院92790 】
相关推荐:
Python中高效访问嵌套字典与列表中的键值对
文心一言怎样用插件调度API数据_文心一言用插件调度API数据【API调用】
为什么简单的XML文件也会解析失败? 检查隐藏的非打印字符(如BOM)的方法
Fabric Mod开发:在1.19.3+版本中正确添加自定义物品并管理物品组
TypeScript/J*aScript:高效查找数组中首个唯一ID对象
Eclipse怎么运行工程_Eclipse工程运行配置说明
抖音商城签到领现金是真的吗_抖音商城签到奖励与提现说明
Go语言中Map值调用指针接收器方法的限制与应对
马斯克:Optimus 人形机器人复数形式为 Optimi
Mac终端命令大全_Mac常用Terminal指令速查
Flexbox布局实践:实现粘性导航栏与底部固定页脚
word邮件合并后日期格式不对怎么改_Word邮件合并日期格式修改方法
利用5118提升短视频内容效果_5118短视频关键词优化方法
如何在J*a中使用Locale处理多语言环境
绝地鸭卫平a核爆刀流玩法攻略
R星幕后开发视频泄露 包含《GTA6》等多款大作
深入理解字体排版:Adobe光学字偶距与CSS字偶距的差异与实现
如何优雅地解决Livewire文件上传难题?SpatieLivewireFilepond让一切变得简单
React Hooks最佳实践:动态组件状态管理的组件化方案
2026春节假期时间安排 2026春节假日查询
星露谷物语官网入口 星露谷物语游戏官网入口
MAC怎么在地图App里使用“四处看看”_MAC体验部分城市的3D实景街景
qq游戏网页版直接玩_qq游戏免下载快速入口
win11 Snap Layouts怎么用 Win11窗口布局与分屏多任务高效指南【必学】
Shopware订单对象中获取产品自定义字段的正确方法
sublime如何配置Python开发环境_将sublime打造成轻量级Python IDE
Win10系统怎么查看已安装更新_Win10卸载有问题的更新补丁
企业名称高精度匹配:N-gram方法在结构相似性分析中的应用
Word2013如何插入视频和音频媒体_Word2013媒体插入的多媒体支持
京东单号查询入口_京东快递订单追踪入口
Go语言中的*string:深入理解字符串指针
outlook中文官网入口地址 outlook官方中文版直达首页链接
HTML元素状态管理:根据DIV内容动态启用/禁用按钮
美团外卖商家服务中心入口 美团商家版官网入口
Python异步编程实践:使用Binance API构建实时交易数据流
html怎么在cmd下运行php文件_cmd运行html中php文件方法【教程】
漫蛙漫画官方主页入口 漫蛙MANWA网页直达访问链接
漫蛙2(台版)官方入口地址 漫蛙2(台版)正版漫画网页端
在J*a中如何使用Exception包装底层异常_异常包装与信息传递方法说明
正确连接J*aScript到HTML实现可点击图片与自定义事件处理
2025-2030年全球乘用车销量预测:新能源成增长主力
C++如何连接MySQL数据库_C++使用Connector/C++操作MySQL数据库教程
QQ邮箱网页版入口登录 QQ邮箱在线邮箱官方通道
QQ邮箱网页版邮箱入口 QQ邮箱官方登录平台
Golang如何实现微服务鉴权与权限控制_Golang微服务鉴权与权限管理实践
Excel函数批量查找替换超快方法_Excel用REPLACE和FIND函数秒级替换
C++如何解决segmentation fault_C++段错误调试与原因分析
AngularJS $http POST请求数据传递与Go后端接收实践
css滚动动画效果怎么实现_使用Animate.css滚动触发动画类
age动漫网站入口 age动漫官网直接访问入口


2025-10-09
浏览次数:次
返回列表
tialY, 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(); // 更新后立即绘制
}
}