国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

JS實現簡單打磚塊彈球小游戲

瀏覽:57日期:2024-03-24 10:46:23

本文實例為大家分享了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><div class='container'> <div class='brickBox'></div> <div class='ball'></div> <div class='slider'></div></div><div style='margin-left: 40%;font-size: 25px;'>當前速度: <span id='speed'></span> </div><div style='margin-left: 40% ;font-size: 25px;'>當前打掉的方塊數: <span id='count'></span> </div></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 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和toplet 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 = -speedYbrickBox.removeChild(brickBox.children[i]);count++; } }console.log(count)document.querySelector('#count').innerHTML=count// GAME OVERif(top>=container.clientHeight-ball.offsetHeight){ clearInterval(timer) if(confirm('GAME OVER,是否重玩')){ location.reload(); }else{alert(’您最終分數’+count)}}left += speedXtop += speedY// 設置給小球的left和topball.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/2if(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>

效果圖如圖所示

JS實現簡單打磚塊彈球小游戲

沒用插件 略微樣式丑了點。然后還存在的BUG是左右方向鍵沒設置終止值。偶爾會出現位置精度丟失導致小球在滑塊上抽搐。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 男性吸女下身的视频 | 九色视频在线观看免费 | 久青草免费视频手机在线观看 | 一级特一级特色生活片 | 亚洲一区欧洲一区 | 日本在线亚洲 | 国内欧美一区二区三区 | 美国三级在线 | 毛片免费在线视频 | 国产免费播放一区二区 | 一区二区播放 | 亚洲黄色片网站 | 国产在线综合一区二区三区 | 久久综合久久88 | 亚洲在线影院 | 俺来也欧美亚洲a∨在线 | 国产成人久久精品推最新 | 久久综合精品不卡一区二区 | 国产欧美日韩综合精品一区二区 | 国内精品一区二区2021在线 | aaaa欧美高清免费 | 99视频在线精品 | 日韩亚洲天堂 | 午夜免费片在线观看不卡 | 男女视频在线免费观看 | 中国一级毛片录像 | 特色一级片 | 日本不卡一二三区 | 美女张开腿让男人捅爽 | 一级一级 a爱片免费视频 | 深夜福利国产福利视频 | 欧美一级高清片在线 | 性色欧美xo影院 | 欧美俄罗斯一级毛片激情 | 日本尹人综合香蕉在线观看 | 国产性生活视频 | 国产香蕉久久 | 国产a级特黄的片子视频免费 | 91手机看片国产福利精品 | 久久国产片 | 国产三级日产三级日本三级 |