最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS實現(xiàn)簡單打磚塊彈球小游戲

 更新時間:2021年09月12日 10:16:28   作者:我是真的不會前端  
這篇文章主要為大家詳細(xì)介紹了JS實現(xiàn)簡單打磚塊彈球小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

使用原生JS寫的,還有一點瑕疵。代碼直接復(fù)制到html就能使用

速度隨機的 因為設(shè)涉及橫向和縱向速度,所以顯示的小球速度值是他們的和速度(立方和開根號)。
按回車或者在滑塊上單機左鍵開始游戲。鼠標(biāo)滑動或者鍵盤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>
<div class="container">
    <div class="brickBox"></div>
    <div class="ball"></div>
    <div class="slider"></div>
</div>
<div style="margin-left: 40%;font-size: 25px;">當(dāng)前速度: <span id="speed"></span> </div>
<div style="margin-left: 40% ;font-size: 25px;">當(dāng)前打掉的方塊數(shù): <span id="count"></span> </div>

</body>
<script>
// 獲取當(dāng)前所有標(biāo)簽
var container = document.querySelector('.container')
var brickBox = container.querySelector('.brickBox')
var ball = container.querySelector('.ball')
var slider = container.querySelector('.slider')
// 動態(tài)創(chuàng)建磚塊
// 定義磚塊大小
var brickWidth = 50;
var brickHeight = 15;
// 計算磚塊數(shù)量
var brickNum = brickBox.clientWidth * brickBox.clientHeight / (brickWidth * brickHeight)
// console.log(brickNum);
var brickColNum = brickBox.clientWidth / brickWidth
// 根據(jù)數(shù)量去創(chuàng)建
for(var i=0;i<brickNum;i++){
    var div = document.createElement('div')
    setStyle(div,{
        width:brickWidth + "px",
        height:brickHeight + "px",
        backgroundColor:getColor(true),
        position:'absolute',
        top:parseInt(i/brickColNum)*brickHeight + 'px',
        left:(i%brickColNum)*brickWidth + 'px'
    })
    brickBox.appendChild(div)
}


// 點擊滑塊讓小球開始運動
// 定義橫向移動的值和縱向移動的值
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('您最終分?jǐn)?shù)'+count)}
        }
        left += speedX
        top += speedY
        // 設(shè)置給小球的left和top
        ball.style.left = left + "px"
        ball.style.top = top + "px"
    },20)
}

// 讓滑塊跟著鼠標(biāo)移動
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)
        
        
    }
    
}
// 封裝檢測相撞的函數(shù)
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;
    }
}
// 封裝獲取隨機顏色的函數(shù)
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)})`
}
// 封裝設(shè)置樣式的函數(shù)
function setStyle(ele,styleObj){
    for(var attr in styleObj){
            ele.style[attr] = styleObj[attr]
    }
}
// 封裝獲取隨機數(shù)的函數(shù)
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是左右方向鍵沒設(shè)置終止值。偶爾會出現(xiàn)位置精度丟失導(dǎo)致小球在滑塊上抽搐。

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

相關(guān)文章

  • 在IE下獲取object(ActiveX)的Param的代碼

    在IE下獲取object(ActiveX)的Param的代碼

    在IE下,獲取Param的時候有個詭異現(xiàn)象(不知道算不算bug)。
    2009-09-09
  • javascript實現(xiàn)起伏的水波背景效果

    javascript實現(xiàn)起伏的水波背景效果

    這篇文章主要為大家詳細(xì)介紹了javascript實現(xiàn)起伏的水波背景效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Js圖片點擊切換輪播實現(xiàn)代碼

    Js圖片點擊切換輪播實現(xiàn)代碼

    這篇文章主要介紹了Js圖片點擊切換輪播實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • JS模式之單例模式基本用法

    JS模式之單例模式基本用法

    這篇文章主要介紹了JS模式之單例模式基本用法,實例分析了javascript單例模式的基本實現(xiàn)方法,需要的朋友可以參考下
    2015-06-06
  • ajax如何實現(xiàn)頁面局部跳轉(zhuǎn)與結(jié)果返回

    ajax如何實現(xiàn)頁面局部跳轉(zhuǎn)與結(jié)果返回

    AJAX即“Asynchronous Javascript And XML”(異步JavaScript和XML),是指一種創(chuàng)建交互式網(wǎng)頁應(yīng)用的網(wǎng)頁開發(fā)技術(shù),通過在后臺與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,AJAX 可以使網(wǎng)頁實現(xiàn)異步更新,本篇文章給大家介紹ajax如何實現(xiàn)頁面局部跳轉(zhuǎn)與結(jié)果返回,感興趣的朋友一起來學(xué)習(xí)
    2015-08-08
  • JavaScript iframe數(shù)據(jù)共享接口實現(xiàn)方法

    JavaScript iframe數(shù)據(jù)共享接口實現(xiàn)方法

    在iframe與父窗口或者與子窗口傳遞數(shù)據(jù)是一個麻煩的事情,如果我們能夠?qū)懸粋€一勞永逸的接口那就再方便不過了,下面就來簡答介紹一下如何實現(xiàn)此功能,對js iframe相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • JavaScript數(shù)組及常見操作方法小結(jié)

    JavaScript數(shù)組及常見操作方法小結(jié)

    這篇文章主要介紹了JavaScript數(shù)組及常見操作方法,結(jié)合實例形式總結(jié)分析了JavaScript數(shù)組的基本獲取、添加、刪除、排序、翻轉(zhuǎn)等相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11
  • js實現(xiàn)div閃爍原理及實現(xiàn)代碼

    js實現(xiàn)div閃爍原理及實現(xiàn)代碼

    閃爍的原理是什么呢:其實就一個,display在none與block之間頻繁的交替,這樣說你明白了么,示例代碼如下,希望對大家有所幫助
    2014-06-06
  • js中頁面的重新加載(當(dāng)前頁面/上級頁面)及frame或iframe元素引用介紹

    js中頁面的重新加載(當(dāng)前頁面/上級頁面)及frame或iframe元素引用介紹

    用JavaScript刷新上級頁面和當(dāng)前頁面在某些情況下還是比較實用的,感興趣的朋友可以了解下另外介紹一下frame或iframe元素的引用方法,希望本文對你有所幫助
    2013-01-01
  • Ajax 文件上傳進(jìn)度監(jiān)聽之upload.onprogress案例詳解

    Ajax 文件上傳進(jìn)度監(jiān)聽之upload.onprogress案例詳解

    這篇文章主要介紹了Ajax 文件上傳進(jìn)度監(jiān)聽之upload.onprogress案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評論

临邑县| 兴义市| 依兰县| 石嘴山市| 荣成市| 玉环县| 武清区| 彰化县| 黎平县| 嘉善县| 大竹县| 清流县| 博罗县| 万载县| 商丘市| 墨玉县| 延长县| 北宁市| 泰顺县| 全南县| 齐河县| 明溪县| 罗江县| 榕江县| 巴中市| 枣阳市| 惠州市| 峡江县| 西丰县| 大埔区| 喀什市| 宁远县| 津市市| 沅陵县| 芜湖县| 方正县| 镇原县| 琼海市| 全椒县| 托克托县| 固原市|