首页 > 其他分享 >JS + Canvas 碰撞检测

JS + Canvas 碰撞检测

时间:2022-10-07 14:44:06浏览次数:50  
标签:Canvas hero collision wall direction height 碰撞检测 var JS

 



<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="chart"> <canvas id="canvas" width="540px" height="300px"></canvas> </div> </body> <script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var direction = ' '; function render() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawHero(); drawWall(); drawTxt(direction); } var then = Date.now(); var hero = { speed: 90, x: 0, y: 0, width: 30, height: 30, } var wall = { x: 100, y: 100, width: 100, height: 50, } var collision = { top: false, left: false, bottom: false, right: false, }; function drawHero() { ctx.fillStyle = 'skyblue'; ctx.fillRect(hero.x, hero.y, hero.width, hero.height) } function drawWall() { ctx.fillStyle = '#000' ctx.fillRect(wall.x, wall.y, wall.width, wall.height) } function drawTxt(direction){ var text = '不能再往' + direction + '边走了'; ctx.font = "20px serif"; ctx.fillText(text, 380, 250); } render(); var keyDown = {}; addEventListener('keydown', function (e) { keyDown[e.keyCode] = true; }, false) addEventListener('keyup', function (e) { delete keyDown[e.keyCode] }, false) function move(time) { if (87 in keyDown) { //W if (collision.top == true) { hero.y -= 0 } else { hero.y -= hero.speed * time } } if (65 in keyDown) { //A if (collision.left == true) { hero.x -= 0 } else { hero.x -= hero.speed * time } } if (83 in keyDown) { //S if (collision.bottom == true) { hero.y += 0 } else { hero.y += hero.speed * time } } if (68 in keyDown) { //D if (collision.right == true) { hero.y += 0 } else { hero.x += hero.speed * time; } } // 右 if (hero.x <= wall.x) { if (hero.x + hero.width + 2 >= wall.x && hero.y + hero.height > wall.y && hero.y < wall.y + wall.height) { collision.right = true; direction = '右'; } else { collision.right = false; direction = ' '; } } if (hero.x >= wall.x + wall.width) { if (hero.x - 2 <= wall.x + wall.width && hero.y + hero.height > wall.y && hero.y < wall.y + wall.height) { collision.left = true; direction = '左'; } else { collision.left = false; direction = ' '; } } if(hero.y + hero.height < wall.y){ if(hero.x - 2 <= wall.x + wall.width && hero.x + hero.width + 2 >= wall.x && hero.y + hero.height + 2 >= wall.y){ collision.bottom = true; direction = '下'; }else{ collision.bottom = false; direction = ' '; } } if(hero.y > wall.y + wall.height){ if(hero.x - 2 <= wall.x + wall.width && hero.x + hero.width + 2 >= wall.x && hero.y <= wall.y + wall.height + 2){ collision.top = true; direction = '上'; }else{ collision.top = false; direction = ' '; } } // if ( // hero.x < wall.x + wall.width && // hero.x + hero.width > wall.x && // hero.y < wall.y + wall.height && // hero.height + hero.y > wall.y // ) { // console.log('触碰到了') // } } function moveTime() { var now = Date.now(); var deleta = now - then; let time = deleta / 1000; move(time); render(); then = now; requestAnimationFrame(moveTime); } moveTime(); </script> </html> <style> * { margin: 0; padding: 0; } #chart { display: block; margin: 200px auto; width: 540px; } #canvas { border: 1px solid #000; } </style>

 

标签:Canvas,hero,collision,wall,direction,height,碰撞检测,var,JS
From: https://www.cnblogs.com/zgjg/p/16759700.html

相关文章

  • 直播带货源码,通过js实现轮播图的效果
    直播带货源码,通过js实现轮播图的效果<!DOCTYPEhtml><html> <head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <m......
  • autojs 企业微信 一键上传
    app.startActivity({action:"android.intent.action.VIEW",data:"alipays://platformapi/startapp?appId=2018062060350751&page=%2Fpages%2Fweb%2Fweb%3Furl%3Dht......
  • HttpClient发送Post请求传递json、普通参数
    importcom.alibaba.fastjson.JSONObject;importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncod......
  • Springboot 之 Filter 实现 Gzip 压缩超大 json 对象
    简介在项目中,存在传递超大json数据的场景。直接传输超大json数据的话,有以下两个弊端占用网络带宽,而有些云产品就是按照带宽来计费的,间接浪费了钱传输数据大导致......
  • autojs 企业微信 一键上传 健康码 行程码
    auto();//打开autojs权限//https://pro.autojs.org/docs/#/zh-cn/coordinatesBasedAutomation?id=setscreenmetricswidth-height//设置屏幕分辨率的坐标setScreenMe......
  • autojs 小红书scheme
    https://blog.csdn.net/u013508278/article/details/117128016https://blog.csdn.net/weixin_38927522/article/details/125540259?utm_medium=distribute.pc_relevant.no......
  • JSON
    json在线解析:https://www.sojson.com/json基础入门:https://blog.csdn.net/Rao_Limon/article/details/80011601学习网站:http://c.biancheng.net/json/what-is-json.html......
  • js 关键词高亮显示
    <!DOCTYPEhtml><html><head><metacharset="utf-8"/><title>js关键字高亮显示</title></head><body><inputtype......
  • JS基础 -- if分别使用return、break、continue的区别
    /**if分别使用return、break、continue的区别**break:使用break可以退出当前的循环**continue:用于跳过当次循环**return:使用return可以结束整个函数**下面用......
  • Node.js原生开发基础入门
     1.NodeJS编程基础概要2.本地环境搭建与基础入门3.文件操作与模块化概念4.JavaScript模块化开发5.npm包管理       1.NodeJS编程基础概要node.js与J......