JavaScript實(shí)現(xiàn)打磚塊游戲
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
html+css部分
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>打磚塊</title>
<link rel="stylesheet" type="text/css" href="css/break.css" rel="external nofollow" />
<script type="text/javascript" src="js/break.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.content{
position: relative;
width: 800px;
height: 600px;
background-color: #999;
margin: 0 auto;
overflow: hidden;
}
.game{
position: relative;
width: 550px;
height: 500px;
background-color: pink;
margin: 20px auto 0;
}
.brick{
position: absolute;
width: 50px;
height: 20px;
background-color: blueviolet;
}
.flap{
position: absolute;
width: 120px;
height: 30px;
bottom: 0;
left: 0;
background-color: blue;
}
.ball{
position: absolute;
width: 30px;
height: 30px;
bottom: 30px;
left: 0;
border-radius: 50%;
background-color: greenyellow;
}
.btn{
position: absolute;
width: 550px;
height: 50px;
bottom: 0;
left: 125px;
}
.btn button{
width: 120px;
height: 40px;
}
#score{
position: absolute;
width: 80px;
height: 30px;
right: 0;
top: 10%;
background-color: #fff;
/*border: 1px solid red;*/
}
</style>
</head>
<body>
<div class="content">
<div class="game">
<!--<div class="brick"></div>-->
<!--<div class="flap"></div>
<div class="ball"></div>-->
</div>
<div class="btn">
<button id="start">開始</button>
<button id="reset">重置</button>
</div>
<div id="score">
</div>
</div>
</body>
</html>
js部分
window.onload = init;
function init(){
var gameArea = document.getElementsByClassName("game")[0];
var rows = 5;
var cols = 11;
var b_width = 50;
var b_height = 20;
var bricks = [];
var speedX = 5;
var speedY = -5;
var interId = null;
var lf = 0;
var tp = 0;
var flap
var ball;
var n = 0;
var st = document.getElementById("start");
var rt = document.getElementById("reset");
var score = document.getElementById("score");
score.innerHTML = "得分:" + n;
renderDom();
bindDom();
function renderDom(){
getBrick();
//得到五彩磚塊
function getBrick(){
for (var i = 0; i < rows; i++) {
var tp = i * b_height;
var brick = null;
for (var j = 0; j < cols; j++) {
var 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);
}
}
}
//添加擋板
var flap = document.createElement("div");
flap.className = "flap";
gameArea.appendChild(flap);
//添加擋板小球
var ball = document.createElement("div");
ball.className = "ball";
gameArea.appendChild(ball);
}
function bindDom(){
flap = document.getElementsByClassName("flap")[0];
window.onkeydown = function(e){
var ev = e || window.event;
var lf = null;
if (e.keyCode == 37) { //左鍵往左走
lf = flap.offsetLeft - 10;
if (lf < 0) {
lf = 0;
}
flap.style.left = lf + "px";
}else if (e.keyCode == 39) { //右鍵往右走
lf = flap.offsetLeft + 10;
if (lf >= gameArea.offsetWidth - flap.offsetWidth) {
lf = gameArea.offsetWidth - flap.offsetWidth
}
flap.style.left = lf + "px";
}
}
st.onclick = function(){
ballMove();
st.onclick = null;
}
rt.onclick = function(){
window.location.reload();
}
}
//得到磚塊的隨即顏色
function getColor(){
var r = Math.floor(Math.random()*256);
var g = Math.floor(Math.random()*256);
var b = Math.floor(Math.random()*256);
return "rgb(" + r + "," + g + "," + b +")";
}
//實(shí)現(xiàn)小球上下左右來回運(yùn)動
function ballMove(){
ball = document.getElementsByClassName("ball")[0];
interId = setInterval(function(){
lf = ball.offsetLeft + speedX;
tp = ball.offsetTop + speedY;
//實(shí)現(xiàn)磚塊消失的效果
for (var i = 0; i < bricks.length; i++) {
var bk = bricks[i];
if ((lf + ball.offsetWidth/2) >= bk.offsetLeft
&& (lf + ball.offsetWidth/2) <= (bk.offsetLeft + bk.offsetWidth)
&& (bk.offsetTop + bk.offsetHeight) >= ball.offsetTop
) {
bk.style.display = "none";
speedY = 5;
n++;
score.innerHTML = "得分:"+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){
gameOver();
}
ball.style.left = lf + 'px';
ball.style.top = tp + "px";
},20)
}
//判斷游戲是否結(jié)束
function gameOver(){
alert("game over" + "\n" + "您的得分是" + score.innerHTML);
clearInterval(interId);
}
}
更多關(guān)于Js游戲的精彩文章,請查看專題: 《JavaScript經(jīng)典游戲 玩不?!?/a>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個仿Windows UI的html table,兼容IE和firefox
兼容IE和firefox的仿Windows UI的html table2008-11-11
JS實(shí)現(xiàn)深度優(yōu)先搜索求解兩點(diǎn)間最短路徑
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)深度優(yōu)先搜索求解兩點(diǎn)間最短路徑,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
JS 新增Cookie 取cookie值 刪除cookie 舉例詳解
cookie很實(shí)用的一個功能,可以判斷某個狀態(tài),下面與大家分享下JS 如何新增Cookie 取cookie值 刪除cookie,感興趣的朋友可以參考下2014-10-10
js將table的每個td的內(nèi)容自動賦值給其title屬性的方法
下面小編就為大家?guī)硪黄猨s將table的每個td的內(nèi)容自動賦值給其title屬性的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
整理Javascript事件響應(yīng)學(xué)習(xí)筆記
整理Javascript事件響應(yīng)學(xué)習(xí)筆記,之前一系列的文章是跟我學(xué)習(xí)Javascript,本文就是進(jìn)一步學(xué)習(xí)javascript流程控制語句,希望大家繼續(xù)關(guān)注2015-12-12
淺談ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別
箭頭函數(shù)是ES6中一種新的函數(shù)的表達(dá)式,本文就來介紹一下ES6中箭頭函數(shù)與普通函數(shù)的區(qū)別,非常具有實(shí)用價值,需要的朋友可以參考下2023-05-05
JS和css實(shí)現(xiàn)檢測移動設(shè)備方向的變化并判斷橫豎屏幕
這篇文章主要介紹了JS和css實(shí)現(xiàn)檢測移動設(shè)備方向的變化并判斷橫豎屏幕,本文分別給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05

