20251109-2

news/2025/11/9 10:51:55/文章来源:https://www.cnblogs.com/Leesz/p/19203892
我才发现我这碰撞检测写的简直是一坨屎,稍微优化了一下,虽然依旧一坨屎。
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<style>body {margin: 0;overflow: hidden;}
</style><body><canvas id="Canvas" style="border:1px solid #000000;"></canvas>
</body>
<script>//初始化画布
  const canvas = document.getElementById('Canvas');canvas.width = window.innerWidth;canvas.height = window.innerHeight;canvas.style.backgroundColor = '#000000';const ctx = canvas.getContext('2d');//定义游戏对象数组
  const grounds = [];const monsters = [];//定义玩家类
  class Player {//基础属性
    maxHp = 10;hp = 10;x = Math.round(canvas.width / 2);y = Math.round(canvas.height / 2);width = 30;height = 30;color = '#FF0000';invincibleColor = 'white';speedX = 0;speedY = 0;a = 0.05;g = 0.1;maxSpeedX = 3;maxSpeedY = 3;lastAttackedTime = Date.now();status = {up: false,down: false,left: false,right: false,landing: false,toward: 'right',attacking: false,invincible: false,}damage = {at: 1,width: 80,height: 40,}//方法//跳跃方法
    jump() {this.speedY = -5;this.status.landing = false;}//玩家运动
    move() {this.x += this.speedX;this.y += this.speedY;}//碰撞监测
    checkCrash() {//陆地检测
      grounds.forEach(Ground => {//下落碰撞检测if (Ground.y - (this.y + this.height) <= 0 &&Ground.y - (this.y + this.height) >= -this.speedY &&this.x + this.width > Ground.x &&this.x < Ground.x + Ground.width) {this.y = Ground.y - this.height;this.status.landing = true;}//左侧if (this.x < Ground.x + Ground.width &&this.x > Ground.x + Ground.width + this.speedX &&this.y + this.height > Ground.y &&this.y < Ground.y + Ground.height) {this.x = Ground.x + Ground.width;this.speedX = 0;this.status.left = false;}//右侧if (this.x + this.width > Ground.x &&this.x + this.width < Ground.x + this.speedX &&this.y + this.height > Ground.y &&this.y < Ground.y + Ground.height) {this.x = Ground.x - this.width;this.speedX = 0;this.status.right = false;}});//边界检测if (this.x < 0) {this.x = 0;this.speedX = 0;}if (this.x + this.width > canvas.width) {this.x = canvas.width - this.width;this.speedX = 0;}if (this.y < 0) {this.y = 0;this.speedY = 0;}if (this.y + this.height > canvas.height) {this.y = canvas.height - this.height;this.status.landing = true;}}//重力作用
    applyGravity() {if (this.status.landing == false) {this.speedY += this.g;if (this.speedY > this.maxSpeedY)this.speedY = this.maxSpeedY;} else {this.speedY = 0;}}//水平无操作时水平减速
    velocityDecay() {if ((this.status.left == false && this.status.right == false) || (this.status.left == true && this.status.right == true)) {if (this.speedX > 0) {this.speedX -= this.a;if (this.speedX < 0) this.speedX = 0;} else if (this.speedX < 0) {this.speedX += this.a;if (this.speedX > 0) this.speedX = 0;}}}//水平加速度操作速度
    controlSpeed() {if (this.status.left) {this.speedX -= this.a;if (this.speedX < -this.maxSpeedX) this.speedX = -this.maxSpeedX;}if (this.status.right) {this.speedX += this.a;if (this.speedX > this.maxSpeedX) this.speedX = this.maxSpeedX;}}//绘制玩家
    draw() {if (this.status.invincible)ctx.fillStyle = this.invincibleColor;elsectx.fillStyle = this.color;ctx.fillRect(this.x, this.y, this.width, this.height);}//展示血量数字
    showHp() {ctx.fillStyle = 'white';ctx.font = '12px Arial';ctx.fillText(this.hp, this.x + this.width / 2 - 6, this.y - 2);}//攻击方法
    attack(m) {m.hp -= this.damage.at;console.log("攻击命中!怪物剩余血量:" + m.hp);if (m.hp <= 0) {const index = monsters.indexOf(m);if (index > -1) {monsters.splice(index, 1);console.log("怪物已被击败!");}}}//绘制攻击范围与攻击判定
    drawAttackRange() {//绘制范围if (this.status.attacking) {ctx.fillStyle = '#FFFF00';if (this.status.toward == 'right') {ctx.fillRect(this.x + this.width, this.y + (this.height - this.damage.height) / 2, this.damage.width, this.damage.height);} else if (this.status.toward == 'left') {ctx.fillRect(this.x - this.damage.width, this.y + (this.height - this.damage.height) / 2, this.damage.width, this.damage.height);}//攻击判定
        monsters.forEach(m => {if (this.status.toward == 'right' &&m.x < this.x + this.width + this.damage.width &&m.x + m.width > this.x + this.width &&m.y < this.y + (this.height + this.damage.height) / 2 &&m.y + m.height > this.y + (this.height - this.damage.height) / 2) {this.attack(m);}else if (this.status.toward == 'left' &&m.x + m.width > this.x - this.damage.width &&m.x < this.x &&m.y < this.y + (this.height + this.damage.height) / 2 &&m.y + m.height > this.y + (this.height - this.damage.height) / 2) {this.attack(m);}})this.status.attacking = false;}}//受击方法
    attacked() {monsters.forEach(m => {if (m.x < this.x + this.width &&m.x + m.width > this.x &&m.y < this.y + this.height &&m.y + m.height > this.y) {this.reduceHp(m.at);}});}//常规血量减少
    reduceHp(at) {const currentTime = Date.now();if (currentTime - this.lastAttackedTime > 1000) {this.hp -= at;this.status.invincible = true;this.lastAttackedTime = currentTime;//异步延迟
        setTimeout(() => {this.status.invincible = false;}, 1000);}}}//定义地面类
  class Ground {x = 0;y = 0;width = 0;height = 0;color = '#654321';constructor(x, y, width, height) {this.x = x;this.y = y;this.width = width;this.height = height;}}//定义怪物类
  class Monster {hp = 5;at = 1;x = 0;y = 0;width = 30;height = 30;invincibleColor = 'white';speedX = 0;speedY = 0;a = 0.03;g = 0.1;maxSpeedX = 2;maxSpeedY = 3;color = '#00FF00';status = {up: false,down: false,left: false,right: false,landing: false,toward: 'right',attacking: false,invincible: false,}constructor(x, y, width, height) {this.x = x;this.y = y;this.width = width;this.height = height;}//绘制怪物
    draw() {ctx.fillStyle = this.color;ctx.fillRect(this.x, this.y, this.width, this.height);}//展示血量数字
    showHp() {ctx.fillStyle = 'white';ctx.font = '12px Arial';ctx.fillText(this.hp, this.x + this.width / 2 - 6, this.y - 2);}//怪物运动
    move() {this.x += this.speedX;this.y += this.speedY;}//水平加速度操作速度
    controlSpeed() {if (this.status.left) {this.speedX -= this.a;if (this.speedX < -this.maxSpeedX) this.speedX = -this.maxSpeedX;}if (this.status.right) {this.speedX += this.a;if (this.speedX > this.maxSpeedX) this.speedX = this.maxSpeedX;}}//判断玩家位置并移动
    pursuePlayer(p) {if (p.x < this.x) {this.status.left = true;this.status.right = false;} else if (p.x > this.x) {this.status.right = true;this.status.left = false;} else {this.status.left = false;this.status.right = false;}}//重力作用
    applyGravity() {if (this.status.landing == false) {this.speedY += this.g;if (this.speedY > this.maxSpeedY)this.speedY = this.maxSpeedY;} else {this.speedY = 0;}}//碰撞监测
    checkCrash() {grounds.forEach(Ground => {//下落碰撞检测if (Ground.y - (this.y + this.height) <= 0 &&Ground.y - (this.y + this.height) >= -this.speedY &&this.x + this.width > Ground.x &&this.x < Ground.x + Ground.width) {this.y = Ground.y - this.height;this.status.landing = true;}//左侧if (this.x < Ground.x + Ground.width &&this.x > Ground.x + Ground.width + this.speedX &&this.y + this.height > Ground.y &&this.y < Ground.y + Ground.height) {this.x = Ground.x + Ground.width;this.speedX = 0;}//右侧if (this.x + this.width > Ground.x &&this.x + this.width < Ground.x + this.speedX &&this.y + this.height > Ground.y &&this.y < Ground.y + Ground.height) {this.x = Ground.x - this.width;this.speedX = 0;}});//边界检测if (this.x < 0) {this.x = 0;this.speedX = 0;}if (this.x + this.width > canvas.width) {this.x = canvas.width - this.width;this.speedX = 0;}if (this.y < 0) {this.y = 0;this.speedY = 0;}if (this.y + this.height > canvas.height) {this.y = canvas.height - this.height;this.status.landing = true;}}}//创建初始测试 玩家对象 地面对象 怪物对象
  const p = new Player();const ground1 = new Ground(0, Math.round(canvas.height - 100), Math.round(canvas.width), 100);grounds.push(ground1);const monster1 = new Monster(200, ground1.y - 30, 30, 30);monsters.push(monster1);//键盘事件监听//1.按下按键
  document.addEventListener('keydown', function (event) {switch (event.key) {case 'ArrowUp':if (p.status.landing == true)p.jump();break;case 'ArrowDown':p.status.down = true;break;case 'ArrowLeft':p.status.left = true;p.status.toward = 'left';break;case 'ArrowRight':p.status.right = true;p.status.toward = 'right';break;case 'z':p.status.attacking = true;break;case 'Z':p.status.attacking = true;break;}});//2.松开按键
  document.addEventListener('keyup', function (event) {switch (event.key) {case 'ArrowUp':break;case 'ArrowDown':p.status.down = false;break;case 'ArrowLeft':p.status.left = false;break;case 'ArrowRight':p.status.right = false;break;}});//3.c键查看玩家状态
  document.addEventListener('keydown', function (event) {if (event.key === 'c' || event.key === 'C') {console.log("玩家状态:", p);}});//动画循环function animate() {ctx.clearRect(0, 0, canvas.width, canvas.height);//绘制陆地
    grounds.forEach(Ground => {ctx.fillStyle = Ground.color;ctx.fillRect(Ground.x, Ground.y, Ground.width, Ground.height);});//玩家
    {//玩家运动
      p.move();//碰撞监测
      p.checkCrash();//重力作用
      p.applyGravity();//水平无操作时水平减速
      p.velocityDecay();//水平加速度操作速度
      p.controlSpeed();//受到伤害方法
      p.attacked()//绘制玩家
      p.draw();//展示血量
      p.showHp();//绘制攻击范围
      p.drawAttackRange();}//怪物
    {monsters.forEach(m => {//下落碰撞检测
        m.checkCrash();//重力作用
        m.applyGravity();//绘制怪物
        m.draw();//展示血量
        m.showHp();//怪物运动
        m.pursuePlayer(p)//水平加速度操作速度
        m.controlSpeed();//怪物移动
        m.move();});}requestAnimationFrame(animate);}//启动!!
  animate();
</script></html>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/960313.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入解析:让AI说“人话“:TypeChat.NET如何用强类型驯服大语言模型的“野性“

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025年评价高的专利评估综合口碑榜

2025年评价高的专利评估综合口碑榜行业背景与市场趋势随着全球科技创新步伐加快,知识产权已成为企业核心竞争力的重要组成部分。根据世界知识产权组织(WIPO)最新数据显示,2024年全球专利申请量达到380万件,同比增…

2025年口碑好的耐高温劳保鞋厂家推荐及选择指南

2025年口碑好的耐高温劳保鞋厂家推荐及选择指南行业背景与市场趋势随着我国工业安全意识的不断提升,劳保用品市场规模持续扩大。据中国劳动防护用品行业协会统计,2024年我国劳保鞋市场规模已达85亿元,预计2025年将突…

基于高光谱成像和偏最小二乘法(PLS)的苹果糖度检测MATLAB实现

一、程序框架设计二、实现 1. 图像加载与预处理 %% 参数设置 clear; clc; imgPath = apple_hsi.mat; % 高光谱图像路径 roiSize = [50,50]; % ROI区域尺寸 wavelength = 400:10:1000; % 波长范围(nm)%% 加载数据…

2025年优质的青年鸡高评价榜

2025年优质的青年鸡高评价榜行业背景与市场趋势近年来,随着我国禽蛋产业规模化、标准化程度不断提升,青年鸡专业化养殖已成为行业重要发展方向。据中国畜牧业协会统计数据显示,2024年全国青年鸡市场规模已达185亿元…

day07-一键生成儿歌视频工作流

今日内容 1 一键生成儿歌背单词视频 1.1 目标 # 1 用户输入一个主题---》生成中英文儿歌,方便小朋友记忆不同主题的单词-动物--》10个动物单词儿歌-蔬菜--》10种蔬菜单词儿歌# 2 用到python代码--》稍微有些难度1.2 工…

实用指南:手机群控软件在游戏运营中的风险管控技术实现

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Zabbix服务告警: Zabbix server: Utilization of icmp pinger processes over 75%

在 Zabbix 中,icmp pinger 主要用于执行主机 ping 检测。当告警提示“Utilization over 75%”,意味着所有 icmp pinger 进程在大部分时间都在忙碌中,系统没有足够的空闲进程来及时处理新的 Ping 请求。该值可以通过…

Process Monitor 学习笔记(5.2):事件模型与五大类操作(文档/注册表/进程/网络/Profiling

Process Monitor 学习笔记(5.2):事件模型与五大类操作(文档/注册表/进程/网络/Profiling2025-11-09 10:38 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !import…

flask: 用gunicorn部署flask项目

一,安装第三方库 $ pip3 install gunicorn gevent 二,使用 -D 后台运行(守护进程) -b 指定绑定地址和端口 -w 设置 4 个工作进程 $ gunicorn -D -b 127.0.0.1:8000 -w 4 app:app 测试 效果: 用ss查看端口是否已使…

2025年评价高的盐城短视频剪辑用户好评榜

2025年评价高的盐城短视频剪辑用户好评榜行业背景与市场趋势随着短视频行业的蓬勃发展,2025年中国短视频市场规模预计将达到1.5万亿元人民币,年复合增长率保持在25%以上。据《2025中国短视频行业白皮书》显示,短视频…

使用Math库执行数值计算

在程序设计和软件开发中,数值计算是核心组成之一,尤其是涉及到工程、科学研究和数据分析等领域。在Python语言中,数值计算常常依赖于内建的 math库,该库提供了一系列的数学函数以支持复杂的数学运算。 首先,math库…

实用指南:Guava Cache 高性能本地缓存库详解与使用案例

实用指南:Guava Cache 高性能本地缓存库详解与使用案例pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&…

深度学习进阶(一)——从 LeNet 到 Transformer:卷积的荣光与注意力的崛起 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025年热门的剧院舞台灯光厂家最新推荐榜

2025年热门的剧院舞台灯光厂家最新推荐榜行业背景与市场趋势随着文化娱乐产业的蓬勃发展,全球舞台灯光市场规模持续扩大。据最新行业报告显示,2024年全球舞台灯光市场规模已达到85亿美元,预计到2025年将突破90亿美元…

2025年知名的火车宠物托运用户好评榜

2025年知名的火车宠物托运用户好评榜 行业背景与市场趋势 随着宠物经济的蓬勃发展,宠物托运行业近年来呈现快速增长态势。据《2024年中国宠物行业白皮书》数据显示,中国宠物市场规模已突破5000亿元,其中宠物托运服…

2025年专业的短视频运营本地优质榜

2025年专业的短视频运营本地优质榜:行业趋势与优质服务商推荐行业背景与市场趋势短视频行业在2025年已进入深度整合与专业化发展阶段。根据《2025中国短视频行业发展白皮书》显示,中国短视频用户规模已达9.8亿,占网…

中文机器阅读理解数据集:7000条高质量问答数据,涵盖搜索与知道双场景,支持DESCRIPTION:YES_NO:ENTITY多类型问题,适用于BERT:GPT等模型训练与评估

参考数据:机器阅读理解数据集引言与背景 在人工智能快速发展的今天,机器阅读理解(Machine Reading Comprehension, MRC)作为自然语言处理领域的核心任务之一,正受到学术界和工业界的广泛关注。机器阅读理解要求模…

2025年11月货架厂家推荐榜:五强对比评测与选购全解析

在“双11”后的补货高峰与年末库存盘点双重压力下,2025年11月成为仓储升级的关键窗口。中小企业主、第三方物流仓、连锁零售配销中心三类人群最常面临“货架承重不足、通道浪费、交付延期”三大痛点:一方面,临时加租…

2025年优秀的涂装喷砂房最新TOP排名厂家

2025年优秀的涂装喷砂房最新TOP排名厂家随着中国制造业的持续升级和环保要求的不断提高,涂装喷砂房作为表面处理的关键设备,其市场需求呈现出稳定增长态势。根据中国表面工程协会最新数据显示,2024年我国涂装喷砂设…