欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2013年09月22日 15:05:44   作者:  
彈球游戲,一般都是使用html5來實(shí)現(xiàn)的,其實(shí)不然,使用js也可以實(shí)現(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(擋板類)

球類代碼實(shí)現(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;

// 實(shí)際的橫向移動速度,左或者右
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;
// 隨機(jī)數(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);

// 將魔法棒添加進(jìn)磚塊中
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源文件實(shí)現(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() {

// 飛機(jī)對應(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) {

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

// 設(shè)置飛機(jī)的初始位置
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() {}

}

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

通過鍵盤左右方向鍵移動擋板的代碼實(shí)現(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)文章

  • 淺析JavaScript中作用域和作用域鏈

    淺析JavaScript中作用域和作用域鏈

    本文主要介紹了JavaScript中作用域和作用域鏈解析,條理分明,方便理解,這里推薦給小伙伴們,有需要的朋友可以參考下
    2016-12-12
  • 如何在JavaScript中使用localStorage詳情

    如何在JavaScript中使用localStorage詳情

    這篇文章主要介紹了如何在JavaScript中使用localStorage,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • uniapp實(shí)現(xiàn)點(diǎn)擊出現(xiàn)彈窗功能實(shí)例

    uniapp實(shí)現(xiàn)點(diǎn)擊出現(xiàn)彈窗功能實(shí)例

    這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)點(diǎn)擊出現(xiàn)彈窗功能的相關(guān)資料,UniApp框架中提供了兩種不同類型的彈出框,以幫助我們滿足不同的需求,需要的朋友可以參考下
    2023-08-08
  • js禁止頁面復(fù)制功能禁用頁面右鍵菜單示例代碼

    js禁止頁面復(fù)制功能禁用頁面右鍵菜單示例代碼

    禁止頁面復(fù)制功能、禁用頁面右鍵菜單等等在瀏覽網(wǎng)頁時想必大家都有遇到過吧,下面為大家詳細(xì)介紹下使用js是如何實(shí)現(xiàn)的,感興趣的朋友可以參考下
    2013-08-08
  • JS實(shí)現(xiàn)類似百葉窗下拉菜單效果

    JS實(shí)現(xiàn)類似百葉窗下拉菜單效果

    百葉窗下拉菜單效果非常棒,今天小編給大家分享一段js代碼實(shí)現(xiàn)類似百葉窗下拉菜單效果,需要的朋友參考下
    2016-12-12
  • 微信小程序?qū)崿F(xiàn)用戶登錄模塊服務(wù)器搭建

    微信小程序?qū)崿F(xiàn)用戶登錄模塊服務(wù)器搭建

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)用戶登錄模塊服務(wù)器搭建,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 微信小程序?qū)崿F(xiàn)側(cè)邊導(dǎo)航欄

    微信小程序?qū)崿F(xiàn)側(cè)邊導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)側(cè)邊導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 8個開發(fā)者必須知道的JavaScript深層概念(推薦)

    8個開發(fā)者必須知道的JavaScript深層概念(推薦)

    JavaScript有一個名為“調(diào)用堆棧”(Call Stack)的簡單列表,它逐一管理任務(wù)(堆棧算法),但是當(dāng)異步任務(wù)被傳遞時,JavaScript會把它彈出到web API,瀏覽器就會處理它,這篇文章主要介紹了8個開發(fā)者必須知道的JavaScript深層概念,需要的朋友可以參考下
    2022-10-10
  • Javascript快速實(shí)現(xiàn)瀏覽器系統(tǒng)通知

    Javascript快速實(shí)現(xiàn)瀏覽器系統(tǒng)通知

    這篇文章給大家介紹了Javascript快速實(shí)現(xiàn)瀏覽器系統(tǒng)通知的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-08-08
  • 基于JavaScript開發(fā)一個有趣的分組抽簽小程序

    基于JavaScript開發(fā)一個有趣的分組抽簽小程序

    在團(tuán)隊合作開發(fā)中,經(jīng)常需要將團(tuán)隊成員分組,來完成各自的任務(wù),而抽簽的方式自然是最公平、最簡單的方法之一,所以本文就來開發(fā)一個有趣的分組抽簽小程序吧
    2023-05-05

最新評論