用JS實(shí)現(xiàn)一個(gè)簡(jiǎn)單的打磚塊游戲
話(huà)不多說(shuō),先看看效果:

HTML架構(gòu)部分
<!-- HTML結(jié)構(gòu) -->
<div class="content">
<div class="game"></div>
<div class="container">
<h2>打磚塊小游戲</h2>
<hr />
<center>
<button id="start"
style="width: 120px;height: 22px;margin: 20px auto;border-radius: 10px;border: none;outline: none;color: rgba(145, 146, 146, 0.918);cursor:pointer;">開(kāi)始游戲</button>
</center>
<div style="width: 219px;border: 1px solid rgba(145, 146, 146, 0.918);"></div>
<center>
<button id="reset"
style="width: 120px;height: 22px;margin: 20px auto;border-radius: 10px;border: none;outline: none;color: rgba(145, 146, 146, 0.918);cursor:pointer;">再來(lái)一局</button>
</center>
<center>
<!-- 分?jǐn)?shù) -->
<div id="score"></div>
</center>
</div>
</div>
CSS樣式部分
<!-- CSS樣式 -->
<style>
* {
padding: 0;
margin: 0;
}
/* body>div {
width: 550px;
height: 520px;
display: flex;
margin: 20px auto;
} */
.container {
width: 220px;
height: 500px;
border: 1px solid rgba(145, 146, 146, 0.918);
margin-top: 20px;
margin-right: 120px;
}
h2 {
text-align: center;
font-size: 26px;
color: rgba(145, 146, 146, 0.918);
margin-bottom: 2px;
}
.content {
position: relative;
width: 800px;
height: 600px;
margin: 0 auto;
overflow: hidden;
display: flex;
}
.game {
position: relative;
width: 456px;
height: 500px;
border: 1px solid rgba(145, 146, 146, 0.918);
margin: 20px auto 0;
}
.brick {
position: absolute;
width: 50px;
height: 20px;
background-color: rgb(238, 17, 28);
}
/* 畫(huà)擋板 */
.flap {
position: absolute;
width: 120px;
height: 10px;
bottom: 0;
left: 0;
background-color: royalblue;
}
.ball {
position: absolute;
width: 20px;
height: 20px;
bottom: 10px;
left: 52px;
border-radius: 50%;
background-color: blue;
}
#score {
width: 100px;
height: 30px;
right: 0;
top: 10%;
color: rgba(145, 146, 146, 0.918);
}
</style>
JavaScript腳本語(yǔ)言部分
<!-- JS結(jié)構(gòu) -->
<script>
/*
// 獲取canvas元素
const canvas = document.getElementById('canvas');
// 獲取到上下文,創(chuàng)建context對(duì)象
const ctx = canvas.getContext('2d');
let raf;
// 定義一個(gè)小球
const ball = {
x: 100, // 小球的 x 坐標(biāo)
y: 100, // 小球的 y 坐標(biāo)
raduis: 20, // 小球的半徑
color: 'blue', // 小球的顏色
vx: 3, // 小球在 x 軸移動(dòng)的速度
vy: 2, // 小球在 y 軸移動(dòng)的速度
// 繪制方法
draw: function () {
ctx.beginPath();
ctx.arc(this.x, this.y, this.raduis, Math.PI * 2, false);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
}
// 該函數(shù)為繪制函數(shù):主要邏輯就是清空畫(huà)布,重新繪制小球
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ball.draw();
// 進(jìn)行邊界的判斷
if (ball.y + ball.vy > canvas.height || ball.y + ball.vy < 0) {
ball.vy = -ball.vy;
}
if (ball.x + ball.vx > canvas.width || ball.x + ball.vx < 0) {
ball.vx = -ball.vx;
}
ball.x += ball.vx;
ball.y += ball.vy;
raf = window.requestAnimationFrame(draw);
}
raf = window.requestAnimationFrame(draw);
*/
// 加載窗口 = init
window.onload = init;
function init() {
// 獲取元素
let gameArea = document.getElementsByClassName("game")[0];
// 設(shè)置10列
let rows = 10;
// 設(shè)置8行
let cols = 8;
// 磚塊與磚塊之間的寬度
let b_width = 58;
// 磚塊與磚塊之間的高度
let b_height = 28;
// 用數(shù)組的形式來(lái)裝磚塊
let bricks = [];
// 小球的X軸方向(上下左右來(lái)回的運(yùn)動(dòng))
let speedX = 5;
// 小球Y軸方向(上下左右來(lái)回的運(yùn)動(dòng))
let speedY = -5;
// 在內(nèi)部里,小球上下左右來(lái)回的運(yùn)動(dòng),【小球碰撞到磚塊 = null】
let interId = null;
// 左邊距離為0
let lf = 0;
// 上邊距離為0
let tp = 0;
// 擋板
let flap;
// 擋板上面的小球
let ball;
// 分?jǐn)?shù)記錄(初始值為0)
let n = 0;
// 獲取開(kāi)始游戲按鈕的元素
let st = document.getElementById("start");
// 獲取再來(lái)一局(重新渲染)按鈕的元素
let rt = document.getElementById("reset");
// 獲取分?jǐn)?shù)記錄的元素
let score = document.getElementById("score");
score.innerHTML = "分?jǐn)?shù):" + n;
// 提供(渲染)Dom[渲染磚塊] 方法
renderDom();
// 鍵盤(pán)的操作(A與D;askm查詢(xún):A:65,需-32,D:68,需+32)方法
bindDom();
// 進(jìn)行渲染磚塊
function renderDom() {
getBrick();
// 畫(huà)磚塊
function getBrick() {
for (let i = 0; i < rows; i++) {
let tp = i * b_height;
let brick = null;
for (let j = 0; j < cols; j++) {
let lf = j * b_width;
brick = document.createElement("div");
brick.className = "brick";
brick.setAttribute("style", "top:" + tp + "px;left:" + lf + "px;");
// 獲取背景的顏色
brick.style.backgroundColor = getColor();
bricks.push(brick);
gameArea.appendChild(brick);
}
}
}
//添加擋板
flap = document.createElement("div");
flap.className = "flap";
gameArea.appendChild(flap);
//添加擋板+小球
ball = document.createElement("div");
ball.className = "ball";
gameArea.appendChild(ball);
}
// 鍵盤(pán)的操作
function bindDom() {
flap = document.getElementsByClassName("flap")[0];
window.onkeydown = function (e) {
let ev = e || window.event;
// 左邊移動(dòng)
let lf = null;
// A鍵往左移動(dòng)
if (e.keyCode == 65) {
lf = flap.offsetLeft - 32;
if (lf < 0) {
lf = 0;
}
flap.style.left = lf + "px";
// D鍵往右移動(dòng)
} else if (e.keyCode == 68) {
lf = flap.offsetLeft + 32;
if (lf >= gameArea.offsetWidth - flap.offsetWidth) {
lf = gameArea.offsetWidth - flap.offsetWidth
}
flap.style.left = lf + "px";
}
}
// 為開(kāi)始游戲按鈕添加點(diǎn)擊事件
st.onclick = function () {
// 實(shí)現(xiàn)小球上下左右不斷移動(dòng)
ballMove();
st.onclick = null;
}
// 為再來(lái)一局按鈕添加點(diǎn)擊事件
rt.onclick = function () {
window.location.reload();
}
}
// 獲得磚塊的顏色 rgb ===>>> 隨機(jī)顏色;random() = 隨機(jī)數(shù)方法
function getColor() {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
return "rgb(" + r + "," + g + "," + b + ")";
}
// 實(shí)現(xiàn)小球上下左右不斷移動(dòng)
function ballMove() {
ball = document.getElementsByClassName("ball")[0];
interId = setInterval(function () {
// 左邊(X軸方向)的移動(dòng)速度
lf = ball.offsetLeft + speedX;
// 上邊(Y軸方向)的移動(dòng)速度
tp = ball.offsetTop + speedY;
// 用for(){}循環(huán)實(shí)現(xiàn)小球與磚塊碰撞后從而消失
for (let i = 0; i < bricks.length; i++) {
let bk = bricks[i];
// if進(jìn)行判斷,判斷小球與磚塊接觸 【若:接觸到:面板的寬度:offset ===>>> 抵消的意思,使它/2,簡(jiǎn)單的說(shuō)就是:X軸=寬,Y軸=高,邊距:上邊offsetTop;左邊offsetLeft.從什么地方反回到某一個(gè)地方接觸一次則記為碰撞一次,從而進(jìn)行讓磚塊抵消】
if ((lf + ball.offsetWidth / 2) >= bk.offsetLeft && (lf + ball.offsetWidth / 2) <= (bk.offsetLeft + bk.offsetWidth) && (bk.offsetTop + bk.offsetHeight) >= ball.offsetTop) {
// 執(zhí)行時(shí) = none時(shí) ===>>> 消失
bk.style.display = "none";
// Y軸的移動(dòng)速度
speedY = 5;
// 小球與磚塊碰撞抵消后,分?jǐn)?shù)+1(n++)
n++;
score.innerHTML = "分?jǐn)?shù):" + n;
}
}
if (lf < 0) {
speedX = -speedX;
}
if (lf >= (gameArea.offsetWidth - ball.offsetWidth)) {
speedX = -speedX;
}
if (tp <= 0) {
speedY = 5;
} else if ((ball.offsetTop + ball.offsetHeight) >= flap.offsetTop && (ball.offsetLeft + ball.offsetWidth / 2) >= flap.offsetLeft && (ball.offsetLeft + ball.offsetWidth / 2) <= (flap.offsetLeft + flap.offsetWidth)) {
speedY = -5;
} else if (ball.offsetTop >= flap.offsetTop) {
// 游戲結(jié)束
gameOver();
}
ball.style.left = lf + 'px';
ball.style.top = tp + "px";
// 讓小球移動(dòng)是時(shí)間參數(shù)隨便給
}, 40)
}
//判斷游戲是否結(jié)束
function gameOver() {
// 彈框提示游戲該結(jié)束
alert("游戲結(jié)束!" + "\n" + score.innerHTML);
// 清除間隔
clearInterval(interId);
}
}
</script>
總結(jié)
以上所述是小編給大家介紹的用JS實(shí)現(xiàn)一個(gè)簡(jiǎn)單的打磚塊游戲,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
JavaScript設(shè)計(jì)模式之工廠(chǎng)方法模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之工廠(chǎng)方法模式介紹,本文講解了簡(jiǎn)單工廠(chǎng)模式、多個(gè)工廠(chǎng)方法模式等內(nèi)容,需要的朋友可以參考下2014-12-12
Bootstrap導(dǎo)航條鼠標(biāo)懸停下拉菜單
這篇文章主要為大家詳細(xì)介紹了Bootstrap導(dǎo)航條鼠標(biāo)懸停下拉菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Electron啟動(dòng)出現(xiàn)白屏問(wèn)題的解決方案
對(duì)于 Web 開(kāi)發(fā)者使用 Electron 構(gòu)建桌面應(yīng)用程序時(shí),經(jīng)常會(huì)遇到如上圖所示的一個(gè)問(wèn)題,在應(yīng)用窗口創(chuàng)建完成到頁(yè)面加載出來(lái)的這段時(shí)間里,出現(xiàn)了長(zhǎng)時(shí)間的白屏,本文就來(lái)探索基于 Electron 場(chǎng)景下啟動(dòng)白屏的解決方案,需要的朋友可以參考下2024-05-05
BootStrap初學(xué)者對(duì)彈出框和進(jìn)度條的使用感覺(jué)
這篇文章主要介紹了BootStrap初學(xué)者對(duì)彈出框和進(jìn)度條的使用感覺(jué)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
基于JavaScript FileReader上傳圖片顯示本地鏈接
這篇文章主要為大家詳細(xì)介紹了基于JavaScript FileReader上傳圖片顯示本地鏈接的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
uniapp實(shí)現(xiàn)人臉識(shí)別功能的具體實(shí)現(xiàn)代碼
最近在使用uniapp開(kāi)發(fā)項(xiàng)目,有刷臉實(shí)名認(rèn)證的需求,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)人臉識(shí)別功能的具體實(shí)現(xiàn),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
js動(dòng)態(tài)修改input輸入框的type屬性(實(shí)現(xiàn)方法解析)
本文是對(duì)js動(dòng)態(tài)修改input輸入框的type屬性的實(shí)現(xiàn)方法。進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11

