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

非html5實現(xiàn)js版彈球游戲示例代碼

 更新時間:2013年09月22日 15:05:44   作者:  
彈球游戲,一般都是使用html5來實現(xiàn)的,其實不然,使用js也可以實現(xiàn)類似的效果,下面有個不錯的示例,感興趣的朋友可以參考下,希望對大家有所幫助
開始前的html頁面
 
開始后的html游戲界面
 
html頁面布局,即index.html文件源碼如下:
復(fù)制代碼 代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>彈球游戲</title>
<link rel="stylesheet" type="text/css" href="css/index.css"/>

</head>

<body>
<center>
<div id="gamePanel" tabindex="0">
<div class="score">分?jǐn)?shù):
<span id="score">0</span>
</div>
<div id="startBtn" onclick="Start()"></div>
</div>
</center>
<script type="text/javascript" src="js/magic.js"></script>
<script type="text/javascript" src="js/brick.js"></script>
<script type="text/javascript" src="js/ball.js"></script>
<script type="text/javascript" src="js/stick.js"></script>
<script type="text/javascript" src="js/game.js"></script>
</body>
</html>

index.css文件源碼如下:
復(fù)制代碼 代碼如下:

#gamePanel{

width:504px;
height:504px;
background:Black;
position:relative;

}

#gamePanel .score{

font-size:20px;
color:White;
position:absolute;
left:0;
top:0;
z-index:9999;

}

#gamePanel .bullet{

width:5px;
height:15px;
position:absolute;
background:url(../img/bullet.png);
overflow:hidden;

}

#gamePanel .stick{

width:80px;
height:18px;
position:absolute;
background:blue;

}

#gamePanel .ball{

width:15px;
height:15px;
position:absolute;
background:url(../img/ball.gif);

}

#gamePanel .brick {

width : 28px;
height : 28px;
position : relative;
background : url(../img/brick.gif);
float : left;

}

#gamePanel .hideBrick {

width : 28px;
height : 28px;
position : relative;
background : black;
float : left;

}

#gamePanel .magic {

width : 27px;
height : 11px;
position : absolute;
background : green;

}

#gamePanel .shortMagic {

width : 28px;
height : 12px;
position : absolute;
background : yellow;

}

#gamePanel .bingo{

width:18px;
height:18px;
position:absolute;
background:url(../img/bingo2.png);

}

#startBtn{

border-width:20px;
border-style:solid;
border-color:Black Black Black Green;
position:absolute;
left:240px;
top:240px;
cursor:pointer;
width:0px;
height:0px;
overflow:hidden;

}

JavaScript部分分為5個源文件,即ball.js(球類)、brick.js(磚類)、game.js(游戲類)、magic.js(魔法棒類)、stick.js(擋板類)

球類代碼實現(xiàn)如下:
復(fù)制代碼 代碼如下:

// 球類
var Ball = function() {

// 彈球dom元素
this.dom = null;

// 是否激活
this.isFirst = true;

// 彈球移動方向
this.direction = null;

this.init();

}

Ball.prototype = {

// 彈球橫向移動速度
movepx : 3,

// 彈球縱向移動速度
movepy : 2,

// 彈球移動頻率
movesp : 20,

// 彈球移動頻率映射
movespMap : {

1 : 75,
2 : 65,
3 : 50,
4 : 40

},

// 初始化
init : function() {

this.dom = document.createElement("div");
this.dom.className = "ball";

},

// 設(shè)置彈球的初始化位置,x與y坐標(biāo)
setPosition : function(x, y) {

this.dom.style.left = x + "px";
this.dom.style.top = y + "px";

},

// 彈球動畫,就是移動,傳入?yún)?shù)為游戲背景的寬與高
animation : function(gameWidth, gameHeight, stick) {

var _this = this;

// 實際的橫向移動速度,左或者右
var _movepx = this.dom.offsetLeft > gameWidth/2 ? -1*this.movepx : this.movepx;
var _movepy = this.dom.offsetTop > gameHeight/2 ? this.movepy : -1*this.movepy;

// 處理移動函數(shù)
var process = function() {

// 彈球的x,y坐標(biāo)
var left = _this.dom.offsetLeft;
var top = _this.dom.offsetTop;

// 是否要調(diào)轉(zhuǎn)方向
if (left <= 0 || left >= gameWidth - _this.dom.clientWidth) {

_movepx *= -1;

}

var isCrashStick = _this.OnCheckCrashStick();
var isCrashBall = _this.OnCheckCrashBrick();

// 判斷是否想上調(diào)轉(zhuǎn)方向
if (top < 0 || isCrashStick || isCrashBall) {

_movepy *= -1;

}

// 向下移動
top = top + _movepy;
left = left + _movepx;

// 設(shè)置彈球位置
_this.dom.style.top = top + "px";
_this.dom.style.left = left + "px";

if(top > gameHeight) {

_this.onend();
alert("You Lose");

} else {

setTimeout(process, _this.movesp);

}

// 判斷彈球移動方向
if (_movepx > 0 && _movepy < 0) {

_this.direction = "RightUp";

return;

}

if (_movepx > 0 && _movepy > 0) {

_this.direction = "RightDown";

return;

}

if (_movepx < 0 && _movepy < 0) {

_this.direction = "LeftUp";

return;

}

if (_movepx < 0 && _movepy > 0) {

_this.direction = "LeftDown";

return;

}

};

// 開始移動
process();

},

// 外部接口,檢測是否撞到魔法棒
OnCheckCrashStick : function() {},

// 外部接口,檢測是否撞到磚塊
OnCheckCrashBrick : function() {},

// 彈球結(jié)束事件
onend : function() {},

// 游戲結(jié)束
gameover : function() {}

}

磚類代碼如下brick.js源文件:
復(fù)制代碼 代碼如下:

// 磚類
var Brick = function(gamePanel) {

// 磚的dom元素
this.dom = null;

// 磚塊所在的畫布
this.gamePanel = gamePanel;

// 是否激活
this.isLive = true;

// 是否帶有魔法棒
this.magic = null;

this.width = 28;
this.height = 28;

this.left = 0;
this.top = 0;

this.init();

}

Brick.prototype = {

// 初始化
init : function() {

this.dom = document.createElement("div");
this.dom.className = "brick";

},

// 為position: relative的Brick初始化位置
setPosition : function(x, y) {

this.left = x;
this.top = y;

},

// 為positon : relative的Brick初始化尺寸
setSize : function(width, height) {

this.width = width;
this.height = height;

},

// 初始化生成魔法棒
initMagic : function() {

var _this = this;
// 隨機數(shù)
var random = parseInt(Math.random()*1000 + 1, 10);

var type = random % 5 == 0 ? "good" : random % 4 == 0 ? "bad" : "none";

// 新建一個魔法棒對象
var magic = new Magic(type);

this.magic = magic;

magic.initPosition(this);

// 將魔法棒添加進磚塊中
this.gamePanel.appendChild(magic.dom);

magic.onEnd = function() {

_this.gamePanel.removeChild(magic.dom);

};

magic.animation(this.gamePanel.clientHeight);

},

// 擊中后的動作
onEnd : function() {

this.isLive = false;
this.dom.className = "hideBrick";
this.initMagic();

}

}

魔法棒類代碼即magic.js源文件實現(xiàn)如下:
復(fù)制代碼 代碼如下:

// 魔法棒類
var Magic = function(type) {

// Magic的dom元素
this.dom = null;

// Magic的dom信息
this.left = 0;
this.top = 0;
this.width = 0;
this.height = 0;

this.type = type;

this.init();

}

Magic.prototype = {

// 魔法棒類型
magicType : {

"good" : "magic",
"bad" : "shortMagic",
"none" : ""

},

// 每次移動位移
movepy : 3,

// 移動速度
movespeed : 20,

// 初始化魔法棒
init : function() {

this.dom = document.createElement("div");

this.dom.className = this.magicType[this.type];
//this.dom.style.display = "none";

this.width = parseInt(this.dom.style.width, 10);
this.height = parseInt(this.dom.style.height, 10);

},

// 魔法棒初始化位置
initPosition : function(brick) {

this.left = brick.left;
this.top = brick.top;

this.dom.style.left = this.left + "px";
this.dom.style.top = this.top + "px";

},

// 更新位置
update : function() {

this.dom.style.left = this.left + "px";
this.dom.style.top = this.top + "px";

},

// 魔法棒動畫,height為游戲背景高度
animation : function(height) {

if (this.type == "none") {

return;

}

var _this = this;

// 向下移動函數(shù)
var downMove = function() {

_this.top = _this.top + _this.movepy;
_this.update();

// 判斷魔法棒下移是否越界,是否擊中stick
if (_this.top < height && !_this.isBeatStick()) {

setTimeout(downMove, _this.movespeed);

} else {

// 動畫結(jié)束觸發(fā)事件
_this.onEnd();

}

};

downMove();

},

// 動畫結(jié)束觸發(fā)事件,外部覆蓋
onEnd : function() {},

// 魔法棒是否擊中擋板以及擊中后處理事件,外部覆蓋
isBeatStick : function() {}

}

擋板類代碼即stick.js源文件如下:
復(fù)制代碼 代碼如下:

// 新建棒類
var Stick = function() {

// 飛機對應(yīng)的dom元素
this.dom = null;

// 是否移動中
this.isMove = false;

// 移動的ID
this.moveId = null;

// 是否彈球中
this.isSend = false;

// 變大標(biāo)記
this.bigCount = 0;

// 變小標(biāo)記
this.smallCount = 0;

// 接棒的寬度變大變小時做存儲
this.width = 0;

this.init();

}

Stick.prototype = {

// 游戲背景Dom
gamePanel : null,

// 游戲背景寬度
gameWidth : 0,

// 游戲背景高度
gameHeight : 0,

// 魔法棒移動速度
movepx : 10,

// 魔法棒移動頻率
movesp : 30,

// 方向鍵值對應(yīng)
keyCodeAndDirection : {

37 : "left",
39 : "right"

},

// 初始化
init : function() {

this.dom = document.createElement("div");
this.dom.className = "stick";

},

// 設(shè)置位置
setPosition : function(gamePanel, width, height) {

// 將魔法棒添加進游戲背景中
this.gamePanel = gamePanel;
this.gamePanel.appendChild(this.dom);

// 設(shè)置飛機的初始位置
this.dom.style.left = (width - this.dom.clientWidth)/2 + "px";
this.dom.style.top = height - this.dom.clientHeight + "px";

// 獲取到游戲背景的寬和高
this.gameWidth = width;
this.gameHeight = height;

},

// 鍵盤按下事件
keydown : function(e) {

var keyCode = e.keyCode;

if (!this.isMove) {

this.move(keyCode);

}

},

// 鍵盤釋放事件
keyup : function(e) {

// 判斷是否為鍵盤釋放
if (this.keyCodeAndDirection[e.keyCode]) {

// 停止移動
this.stopMove();

} else if (e.keyCode == 32) {

// 設(shè)置為非發(fā)彈中
this.isSend = false;

}

},

// 移動
move : function(keyCode) {

// 設(shè)置為移動中
this.isMove = true;
var _this = this;

// 判斷移動方向
switch(this.keyCodeAndDirection[keyCode]) {

case "left" : {

this.moveId = setInterval(function() {_this.moveLeft();}, _this.movesp);
break;

}

case "right" : {

this.moveId = setInterval(function() {_this.moveRight();}, _this.movesp);
break;

}

default : break;

}

},

// 向左移動
moveLeft : function() {

var left = this.dom["offsetLeft"];
left = left - this.movepx >= 0 ? left - this.movepx : 0;
this.dom.style["left"] = left + "px";

if (left == 0) {

this.stopMove();

}

},

// 向右移動
moveRight : function() {

var left = this.dom["offsetLeft"];
var maxDistance = this.gameWidth - this.dom.clientWidth;
left = left + this.movepx <= maxDistance ? left + this.movepx: maxDistance;
this.dom.style["left"] = left + "px";

if (left == maxDistance) {

this.stopMove();

}

},

// 變小
changeSmall : function() {

if (this.smallCount >= 1) {

return;

} else {

this.dom.style.width = 80 + "px";
this.smallCount ++;

this.bigCount >= 1 ? this.bigCount -- : this.bigCount + 0;

}

this.dom.style.left = parseInt(this.dom.style.left, 10) + 20 + "px";
this.dom.style.width = 40 + "px";

},

// 變大
changeBig : function() {

if (this.bigCount >= 1) {

return;

} else {

this.dom.style.width = 80 + "px";
this.bigCount ++;

this.smallCount >= 1 ? this.smallCount -- : this.smallCount + 0;

}

if (parseInt(this.dom.style.left, 10) <= 75 ) {

this.dom.style.width = parseInt(this.dom.style.width, 10) + 75 + parseInt(this.dom.style.left, 10)+ "px";
this.dom.style.left = 0 + "px";

return;

} else if (this.dom.style.width + 150 + parseInt(this.dom.style.left, 10) >= this.gamePanel.clientWidth) {

this.dom.style.left = parseInt(this.dom.style.left, 10) - 150 + "px";
this.dom.style.width = this.dom.style.width + 150 + "px";

return;

} else {

this.dom.style.left = parseInt(this.dom.style.left, 10) - 75 + "px";
this.dom.style.width = 150 + "px";

}

},

// 停止移動
stopMove : function() {

this.isMove = false;
clearInterval(this.moveId);

},

// 發(fā)射彈球,外部接口,
onSendBall : function() {},


// 改分?jǐn)?shù)外部接口
onChangeScore : function() {}

}

部分難點技術(shù)實現(xiàn)

通過鍵盤左右方向鍵移動擋板的代碼實現(xiàn):
復(fù)制代碼 代碼如下:

// 鍵盤按下事件
keydown : function(e) {

var keyCode = e.keyCode;

if (!this.isMove) {

this.move(keyCode);

}

},

// 鍵盤釋放事件
keyup : function(e) {

// 判斷是否為鍵盤釋放
if (this.keyCodeAndDirection[e.keyCode]) {

// 停止移動
this.stopMove();

} else if (e.keyCode == 32) {

// 設(shè)置為非發(fā)彈中
this.isSend = false;

}

},

// 移動
move : function(keyCode) {

// 設(shè)置為移動中
this.isMove = true;
var _this = this;

// 判斷移動方向
switch(this.keyCodeAndDirection[keyCode]) {

case "left" : {

this.moveId = setInterval(function() {_this.moveLeft();}, _this.movesp);
break;

}

case "right" : {

this.moveId = setInterval(function() {_this.moveRight();}, _this.movesp);
break;

}

default : break;

}

},

// 向左移動
moveLeft : function() {

var left = this.dom["offsetLeft"];
left = left - this.movepx >= 0 ? left - this.movepx : 0;
this.dom.style["left"] = left + "px";

if (left == 0) {

this.stopMove();

}

},

// 向右移動
moveRight : function() {

var left = this.dom["offsetLeft"];
var maxDistance = this.gameWidth - this.dom.clientWidth;
left = left + this.movepx <= maxDistance ? left + this.movepx: maxDistance;
this.dom.style["left"] = left + "px";

if (left == maxDistance) {

this.stopMove();

}

},

相關(guān)文章

  • 在JS數(shù)組特定索引處指定位置插入元素的技巧

    在JS數(shù)組特定索引處指定位置插入元素的技巧

    這篇文章主要介紹了如何在JS數(shù)組特定索引處指定位置插入元素?將一個元素插入到現(xiàn)有數(shù)組的特定索引處,需要的朋友可以參考下
    2014-08-08
  • 解決使用attachEvent函數(shù)時,this指向被綁定的元素的問題的方法

    解決使用attachEvent函數(shù)時,this指向被綁定的元素的問題的方法

    解決使用attachEvent函數(shù)時,this指向被綁定的元素的問題的方法...
    2007-08-08
  • javascript局部自定義鼠標(biāo)右鍵菜單

    javascript局部自定義鼠標(biāo)右鍵菜單

    這篇文章主要為大家詳細介紹了javascript局部自定義鼠標(biāo)右鍵菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • JavaScript基于querySelector?/?querySelectorAll對元素的操作(DOM?API掃盲)

    JavaScript基于querySelector?/?querySelectorAll對元素的操作(DOM?AP

    這篇文章主要介紹了JavaScript基于querySelector?/?querySelectorAll對元素的操作(DOM?API掃盲),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • js的各種數(shù)據(jù)類型判斷的介紹

    js的各種數(shù)據(jù)類型判斷的介紹

    今天小編就為大家分享一篇關(guān)于js的各種數(shù)據(jù)類型判斷的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • setTimeout與setInterval的區(qū)別淺析

    setTimeout與setInterval的區(qū)別淺析

    這篇文章主要給大家介紹了關(guān)于setTimeout與setInterval區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用js具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • vite添加環(huán)境變量import.meta.env的方法

    vite添加環(huán)境變量import.meta.env的方法

    在不同的文件里面配置不同的環(huán)境變量,可以讓我們的配置更加容易維護和使用,這里我們說下vite配置環(huán)境變量和模式是怎么配置的,對vite環(huán)境變量相關(guān)知識感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • Echarts中l(wèi)egend屬性使用的方法詳解

    Echarts中l(wèi)egend屬性使用的方法詳解

    Echarts可以幫助我們快速構(gòu)建柱狀圖、餅圖、條形圖,這對于多圖形化展示數(shù)據(jù)來說尤其方便,可幫助我們快速開發(fā),下面這篇文章主要給大家介紹了關(guān)于Echarts中l(wèi)egend屬性使用的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • js閉包的9個使用場景

    js閉包的9個使用場景

    這篇文章主要介紹了js 閉包的9個使用場景,幫助大家更好的理解和學(xué)習(xí)JavaScript 閉包的使用,感興趣的朋友可以了解下
    2020-12-12
  • JavaScript實現(xiàn)經(jīng)典排序算法之冒泡排序

    JavaScript實現(xiàn)經(jīng)典排序算法之冒泡排序

    這篇文章主要介紹了JavaScript實現(xiàn)經(jīng)典排序算法之冒泡排序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評論

中卫市| 若羌县| 南召县| 六枝特区| 英吉沙县| 汶上县| 贡觉县| 绵阳市| 大埔县| 临潭县| 江门市| 乌兰察布市| 溆浦县| 兴安盟| 大姚县| 保德县| 新邵县| 扶沟县| 阜宁县| 苍山县| 齐齐哈尔市| 温州市| 慈溪市| 云和县| 阳泉市| 电白县| 临高县| 新晃| 孟津县| 宜宾县| 华坪县| 海城市| 兴业县| 祁门县| 三穗县| 阿克苏市| 汉中市| 建始县| 宾阳县| 高碑店市| 南陵县|