欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)打磚塊游戲

 更新時(shí)間:2020年02月14日 11:01:35   作者:風(fēng)華正茂呀  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JS實(shí)現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下

面向?qū)ο笏枷?/p>

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title></title>
 <style type="text/css">
 #show{
 width: 200px;
 height: 600px;
 position: absolute;
 left:1122px ;
 top:50px;
 background-color: gray;
 color: blue;
 line-height: 100px;
 font-size: larger;
 align-self: center;
 }
 #lose{
 position: absolute;
 top: 300px;
 left: 300px;
 font-size: xx-large;
 display: none;
 }
 li{
 position: absolute;
 list-style-type: none;
 background-color: #000000;
 width: 90px;
 height: 30px;
 border: 1px solid white;
 }
 #box{
 position: absolute;
 width: 920px;
 height: 600px;
 left: 200px;
 top: 50px;
 border: 2px solid red;
 }
 #board{
 position: absolute;
 top:590px;
 left: 300px;
 width: 200px;
 height: 10px;
 background-color: black;
 }
 #bubble{
 position: absolute;
 top: 565px;
 left: 360px;
 width: 25px;
 height: 25px;
 background-color: #FF0000;
 border-radius: 50%;
 }
 </style>
 
 <script type="text/javascript">
 window.onload=function(){
 function $(id){
 return document.getElementById(id);
 }
 function rand(min,max){
 return Math.floor(Math.random()*(max-min+1))+min
 }
 
 //打磚塊構(gòu)造函數(shù)
 function BlockBreak(){
 this.box=$('box');//容器
 this.list=document.getElementsByTagName('li');//磚塊
 this.board=$('board');//擋板
 this.ball=$('bubble');//小球
 this.fx=3;//橫向
 
 this.fy=-3;//縱向
 this.leftInit=0;//磚塊left初始值
 this.topInit=0;//磚塊top初始值
 }
 //初始化功能 擺放每一個(gè)磚塊的位置
 BlockBreak.prototype.init=function(){
 for(var i=0;i<this.list.length;i++){
 this.list[i].style.backgroundColor="rgb("+rand(0,255)+","+rand(0,255)+","+rand(0,255)+")";
 var x=this.leftInit*this.list[0].offsetWidth;
 var y=this.topInit;
 this.list[i].style.left=x+'px';
 this.list[i].style.top=y+'px';
 this.leftInit++

 //換行
 if((i+1)%10==0){
 this.leftInit=0;
 this.topInit+=this.list[0].offsetHeight;
 }
 
 }
 }
 //擋板運(yùn)動
 
 BlockBreak.prototype.keydown=function(){
 var that=this;
 var mleft=false;
 var mright=false;
 
 //因?yàn)榘存I之后第一次跟第之后的移動之間會有延遲,這是操作系統(tǒng)的問題防止連按兩次
 //所以就不把移動放在這里了,把治理放一個(gè)標(biāo)志,移動放在定時(shí)器里
 document.onkeydown=function(e){
 var e=e||event;
 if(e.keyCode==37){
 mleft=true;
 }
 if(e.keyCode==39){
 mright=true;
 
 }
 }
 document.onkeyup=function(){
 mleft=false;
 mright=false;
 }
 setInterval(function(){
 
 console.log(mleft);
 if (mleft){
 that.board.style.left=that.board.offsetLeft-15+'px';
 if(that.board.offsetLeft<=0){
 that.board.style.left=0;
 }
 }else if(mright){
 that.board.style.left=that.board.offsetLeft+15+'px';
 if(that.board.offsetLeft>=720){
 that.board.style.left=720+'px';
 }
 } 
 
 },50) 
 
 
 }
 var times=0;
 var score=0;
 //小球運(yùn)動
 BlockBreak.prototype.move=function(){
 var timer=null;
 var that=this;
 timer=setInterval(function(){
 that.ball.style.left=that.ball.offsetLeft+that.fx+'px';
 that.ball.style.top=that.ball.offsetTop+that.fy+'px';
 //小球四個(gè)方向反彈
 if(that.ball.offsetTop<=0){
 that.fy*=-1;
 }
 if(that.ball.offsetLeft<=0){
 that.fx*=-1;
 }
 if(that.ball.offsetLeft>=(that.box.offsetWidth-that.ball.offsetWidth)){
 that.fx*=-1;
 
 }
 if(that.ball.offsetTop>=(that.box.offsetHeight-that.ball.offsetHeight)){
 //游戲結(jié)束
 
 $('lose').style.display='block';
 clearInterval(timer);
 $('res').innerHTML='游戲結(jié)束'+"<br>";
 }
 //小球擋板碰撞反彈
 if(that.ball.offsetTop+that.ball.offsetHeight>=that.board.offsetTop){
 if(that.ball.offsetLeft+that.ball.offsetWidth>=that.board.offsetLeft){
 if(that.ball.offsetLeft<=that.board.offsetLeft+that.board.offsetWidth){
 that.fy*=-1;
 times++;
 $('times').innerHTML=times+'次'+'<br>';
 }
 }
 }
 //小球磚塊反彈
 //以一個(gè)小球?yàn)榛鶞?zhǔn) 遍歷所有磚塊,找到滿足條件的磚塊
 for(var i=0;i<that.list.length;i++)
 {
 if(that.ball.offsetTop<=that.list[i].offsetTop+that.list[i].offsetHeight){
 if(that.ball.offsetLeft>=that.list[i].offsetLeft){
 if(that.ball.offsetLeft<=that.list[i].offsetLeft+that.list[i].offsetWidth){
 that.fy*=-1;
 that.list[i].style.display='none';
 score++;
 $('score').innerHTML=score+'分'+'<br>';
 }
 }
 }
 
 }
 
 },10)
 }
 var bb=new BlockBreak();
 bb.init();
 $('start').onclick=function(){
 $('times').innerHTML=0+'次'+'<br>';
 $('score').innerHTML=0+'分'+"<br>";
 $('res').innerHTML= "正在游戲"+"<br>";
 bb.keydown();
 bb.move();
 
 }
 
 }
 </script>
 </head>
 <body>
 <div id="container">
 <div id="show">
 <span id="info">游戲重要信息<br></span>
 <span>當(dāng)前時(shí)間:</span>
 <span id="time">加載中...<br></span>
 <span>游戲狀態(tài):</span>
 <span id="res">加載中...<br></span>
 <span>擋板打球次數(shù):</span>
 <span id="times">加載中...<br></span>
 <span>游戲得分:</span>
 <span id="score">加載中...</span>
 </div>
 
 
 
 <!----游戲區(qū)域--->
 <div id="box">
 <ul>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 </ul>
 <div id="bubble"></div>
 <div id="board"></div>
 <div id="lose"><h1>Game Over!</h1></div>
 </div>
 
 <button type="button" id="start">開始游戲</button>
 
 </div>
 </body>
</html>

更多關(guān)于Js游戲的精彩文章,請查看專題: 《JavaScript經(jīng)典游戲 玩不?!?/a>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 微信小程序 wx.getUserInfo引導(dǎo)用戶授權(quán)問題實(shí)例分析

    微信小程序 wx.getUserInfo引導(dǎo)用戶授權(quán)問題實(shí)例分析

    這篇文章主要介紹了微信小程序 wx.getUserInfo引導(dǎo)用戶授權(quán)問題,結(jié)合實(shí)例形式分析了微信小程序使用wx.getUserInfo引導(dǎo)用戶授權(quán)問題的具體操作步驟與實(shí)現(xiàn)方法,需要的朋友可以參考下
    2020-03-03
  • 微信小程序請求前置的方法詳解

    微信小程序請求前置的方法詳解

    這篇文章主要給大家介紹了關(guān)于微信小程序請求前置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 瀏覽器兼容的JS寫法總結(jié)

    瀏覽器兼容的JS寫法總結(jié)

    瀏覽器兼容的JS寫法總結(jié),涵蓋的內(nèi)容很全面,感興趣的小伙伴們可以參考一下
    2016-04-04
  • 你不知道的JS?ES6字符串標(biāo)簽函數(shù)分享

    你不知道的JS?ES6字符串標(biāo)簽函數(shù)分享

    字符串標(biāo)簽函數(shù)是一種特殊的函數(shù)調(diào)用語法,本文將深入探討ES6中字符串標(biāo)簽函數(shù)的工作原理,并結(jié)合具體的代碼展示它的威力,快跟隨小編一起學(xué)習(xí)起來吧
    2023-06-06
  • Javascript實(shí)現(xiàn)跨域后臺設(shè)置攔截的方法詳解

    Javascript實(shí)現(xiàn)跨域后臺設(shè)置攔截的方法詳解

    這篇文章主要給大家介紹了關(guān)于Javascript實(shí)現(xiàn)跨域后臺設(shè)置攔截的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 一道常被人輕視的web前端常見面試題(JS)

    一道常被人輕視的web前端常見面試題(JS)

    面試題是招聘公司和開發(fā)者都非常關(guān)心的話題,公司希望通過它了解開發(fā)者的真實(shí)水平和細(xì)節(jié)處理能力,而開發(fā)者希望能夠最大程度地展示自己的水平(甚至超常發(fā)揮)。本文提供了眾多前端開發(fā)面試題,無論是招聘方還是應(yīng)聘方都值得一看
    2016-02-02
  • js中null與空字符串

    js中null與空字符串""的區(qū)別講解

    今天小編就為大家分享一篇關(guān)于js中null與空字符串""的區(qū)別講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Javascript怎樣使用SessionStorage和LocalStorage

    Javascript怎樣使用SessionStorage和LocalStorage

    這篇文章主要介紹了Javascript怎樣使用SessionStorage和LocalStorage,對web存儲數(shù)據(jù)感興趣的同學(xué),可以參考下
    2021-04-04
  • JavaScript?WebSocket實(shí)現(xiàn)實(shí)時(shí)雙向聊天

    JavaScript?WebSocket實(shí)現(xiàn)實(shí)時(shí)雙向聊天

    這篇文章主要為大家詳細(xì)介紹了如何基于JavaScript?WebSocket實(shí)現(xiàn)實(shí)時(shí)雙向聊天,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下
    2024-04-04
  • 淺析JavaScript中回調(diào)地獄與asyn函數(shù)和await函數(shù)原理

    淺析JavaScript中回調(diào)地獄與asyn函數(shù)和await函數(shù)原理

    這篇文章主要介紹了JavaScript中回調(diào)地獄與asyn函數(shù)和await函數(shù)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01

最新評論