自己做的弹珠小游戏(JS实现简单打砖块弹球小游戏)
类别:编程学习 浏览量:2059
时间:2022-01-26 01:26:01 自己做的弹珠小游戏
JS实现简单打砖块弹球小游戏本文实例为大家分享了JS实现打砖块弹球小游戏的具体代码,供大家参考,具体内容如下
使用原生JS写的,还有一点瑕疵。代码直接复制到html就能使用
速度随机的 因为设涉及横向和纵向速度,所以显示的小球速度值是他们的和速度(立方和开根号)。
按回车或者在滑块上单机左键开始游戏。鼠标滑动或者键盘A(左)或者D(右)控制滑块方向接小球。
这个小demo的意义主要为了锻炼逻辑能力:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>document</title> <style> .container{ width: 500px; height: 500px; border:1px solid #000; margin:auto; position:relative; } .brickBox{ width: 500px; height: 300px; /* background-color: yellowgreen; */ position:absolute; left: 0; top: 0; } .ball{ width: 15px; height: 15px; background-color:purple; border-radius:50%; position:absolute; bottom:30px; left:235px; /* margin-left:-15px; */ } .slider{ width: 150px; height: 30px; background-color: #00f; position:absolute; /* left:50%; */ left:175px; /* margin-left:-75px; */ bottom:0; } </style> </head> <body> <li class="container"> <li class="brickBox"></li> <li class="ball"></li> <li class="slider"></li> </li> <li style="margin-left: 40%;font-size: 25px;">当前速度: <span id="speed"></span> </li> <li style="margin-left: 40% ;font-size: 25px;">当前打掉的方块数: <span id="count"></span> </li> </body> <script> // 获取当前所有标签 var container = document.querySelector('.container') var brickBox = container.querySelector('.brickBox') var ball = container.querySelector('.ball') var slider = container.querySelector('.slider') // 动态创建砖块 // 定义砖块大小 var brickWidth = 50; var brickHeight = 15; // 计算砖块数量 var brickNum = brickBox.clientWidth * brickBox.clientHeight / (brickWidth * brickHeight) // console.log(brickNum); var brickColNum = brickBox.clientWidth / brickWidth // 根据数量去创建 for(var i=0;i<brickNum;i++){ var li = document.createElement('li') setStyle(li,{ width:brickWidth + "px", height:brickHeight + "px", backgroundColor:getColor(true), position:'absolute', top:parseInt(i/brickColNum)*brickHeight + 'px', left:(i%brickColNum)*brickWidth + 'px' }) brickBox.appendChild(li) } // 点击滑块让小球开始运动 // 定义横向移动的值和纵向移动的值 var speedX = getRandom(1,8); var speedY = getRandom(1,8); document.querySelector("#speed").innerHTML= Math.sqrt(Math.pow(speedX,2)+Math.pow(speedY,2)) var timer; //点击移动 slider.onclick = move; //回车键开始弹 function move(){ var count=0; clearInterval(timer) timer = setInterval(function(){ // 开始移动 // 获取小球的left和top let left = ball.offsetLeft; let top = ball.offsetTop; // 让left和top增加速度 // 小球和滑块相撞 if(boom(slider,ball)){ speedY = -speedY } // 小球和大盒子相撞 if(left<=0 || left>=container.clientWidth - ball.offsetWidth){ speedX = -speedX } if(top<=0){ speedY = -speedY } // 检测所有砖块和小球是否相撞 for(let i=0;i<brickBox.children.length;i++){ if(boom(brickBox.children[i],ball)){ speedY = -speedY brickBox.removeChild(brickBox.children[i]); count++; } } console.log(count) document.querySelector("#count").innerHTML=count // GAME OVER if(top>=container.clientHeight-ball.offsetHeight){ clearInterval(timer) if(confirm("GAME OVER,是否重玩")){ location.reload(); }else{alert('您最终分数'+count)} } left += speedX top += speedY // 设置给小球的left和top ball.style.left = left + "px" ball.style.top = top + "px" },20) } // 让滑块跟着鼠标移动 slider.onmouseover = function(){ document.onmousemove = function(e){ var e = e || window.event; var x = e.pageX; var l = x - container.offsetLeft - 1 - slider.offsetWidth/2 if(l<0){ l = 0 } if(l > container.clientWidth - slider.offsetWidth){ l = container.clientWidth - slider.offsetWidth } slider.style.left = l + "px" } } //让滑块跟着左右键盘移动 window.onload= function(){ document.onkeydown = e=>{ var e = e || window.event; var keycode = e.keyCode || e.which; var keyword = String.fromCharCode(keycode).toLowerCase(); if(keycode==13){ move(); } if(keyword=='a'){ console.log("1111") slider.style.left= slider.offsetLeft-15+"px" }else if(keyword=='d'){ console.log("222") slider.style.left=slider.offsetLeft+15+"px" } console.log(slider.offsetLeft) } } // 封装检测相撞的函数 function boom(node1,node2){ // 不撞在一起的只有4中可能 if(node1.offsetLeft+node1.offsetWidth<node2.offsetLeft || node1.offsetTop+node1.offsetHeight<node2.offsetTop || node2.offsetLeft+node2.offsetWidth<node1.offsetLeft || node2.offsetTop+node2.offsetHeight<node1.offsetTop){ return false; }else{ return true; } } // 封装获取随机颜色的函数 function getColor(hex=true){ if(hex){ var color = '#' for(var i=0;i<3;i++){ var rgb = getRandom(256).toString(16); rgb = rgb.length===1?'0'+rgb:rgb; color += rgb } return color; } return `rgb(${getRandom(256)},${getRandom(256)},${getRandom(256)})` } // 封装设置样式的函数 function setStyle(ele,styleObj){ for(var attr in styleObj){ ele.style[attr] = styleObj[attr] } } // 封装获取随机数的函数 function getRandom(a,b=0){ var max = Math.max(a,b); var min = Math.min(a,b) return Math.floor(Math.random() * (max-min)) + min } </script> </html>
效果图如图所示
没用插件 略微样式丑了点。
然后还存在的BUG是左右方向键没设置终止值。偶尔会出现位置精度丢失导致小球在滑块上抽搐。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
您可能感兴趣
- js淘宝购物车效果代码(JavaScript实现电商平台商品细节图)
- js防抖用法(JavaScript的防抖和节流案例)
- js事件冒泡与事件捕获(基于事件冒泡、事件捕获和事件委托详解)
- json和xml比较与区别
- 使用vue-cli构建electron项目(MAC+PyCharm+Flask+Vue.js搭建系统)
- js实现div(javascript拖曳互换div的位置实现示例)
- 微信js开发教程(微信JSSDK分享功能图文实例详解)
- js运算符使用教程(js中不常见的运算符与操作符总结)
- js的遍历数组方法(JS中数组常用的循环遍历你会几种)
- sqlserver字段说明(详解SQL Server 中 JSON_MODIFY 的使用)
- js脚本语言原理(实例说明js脚本语言和php脚本语言的区别)
- nodejs数据采集(nodejs获取表单数据的三种方法实例)
- js中substring的用法、与substr的区别
- js中的内存机制(详解JS内存空间)
- Extjs中FieldSet的收缩和展开
- js怎么防抖(JS防抖节流函数的实现与使用场景)
- CellPress旗下的6 期刊,国人友刊来了解一下吧(CellPress旗下的6期刊国人友刊来了解一下吧)
- ()
- SCI检索 SSCI检索 EI检索 ISTP检索 CSCD检索简介(SCI检索SSCI检索EI检索)
- 参考文献里期刊名称的写法,你知道吗(参考文献里期刊名称的写法)
- 硕博期刊 SCI SSCI CSSCI分不清 一文带你看懂主流期刊分类(硕博期刊SCISSCI)
- 辱华品牌新百伦官宣新代言人IU,个别粉丝希望get爱豆同款(辱华品牌新百伦官宣新代言人IU)
热门推荐
- yii框架的常用组件使用(Yii框架的布局文件实例分析)
- python环境怎么安装pip(python2.7 安装pip的方法步骤管用)
- python排序方法简单(快速排序的四种python实现推荐)
- sql服务器停止后无法启动(SQL服务器无法启动的解决方法)
- vue手动清除keepalive缓存(vue中keep-alive组件的用法示例)
- css制作旋转小球(css实现毛毛虫爬行动作)
- tomcat作用及原理(详解Tomcat常用的过滤器)
- css42个实用技巧(你必须要知道的几个CSS技巧)
- Request、Request.Form、Request.QueryString的区别
- vue过滤器filters怎么用(如何使用vue过滤器filter)