亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > JavaScript > 正文

非html5實現js版彈球游戲示例代碼

2019-11-20 22:16:36
字體:
來源:轉載
供稿:網友
開始前的html頁面
 
開始后的html游戲界面
 
html頁面布局,即index.html文件源碼如下:
復制代碼 代碼如下:

<!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">分數:
<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文件源碼如下:
復制代碼 代碼如下:

#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(擋板類)

球類代碼實現如下:
復制代碼 代碼如下:

// 球類
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";

},

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

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

},

// 彈球動畫,就是移動,傳入參數為游戲背景的寬與高
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;

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

// 彈球的x,y坐標
var left = _this.dom.offsetLeft;
var top = _this.dom.offsetTop;

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

_movepx *= -1;

}

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

// 判斷是否想上調轉方向
if (top < 0 || isCrashStick || isCrashBall) {

_movepy *= -1;

}

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

// 設置彈球位置
_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() {},

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

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

}

磚類代碼如下brick.js源文件:
復制代碼 代碼如下:

// 磚類
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;
// 隨機數
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源文件實現如下:
復制代碼 代碼如下:

// 魔法棒類
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;

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

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

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

setTimeout(downMove, _this.movespeed);

} else {

// 動畫結束觸發事件
_this.onEnd();

}

};

downMove();

},

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

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

}

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

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

// 飛機對應的dom元素
this.dom = null;

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

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

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

// 變大標記
this.bigCount = 0;

// 變小標記
this.smallCount = 0;

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

this.init();

}

Stick.prototype = {

// 游戲背景Dom
gamePanel : null,

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

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

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

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

// 方向鍵值對應
keyCodeAndDirection : {

37 : "left",
39 : "right"

},

// 初始化
init : function() {

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

},

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

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

// 設置飛機的初始位置
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) {

// 設置為非發彈中
this.isSend = false;

}

},

// 移動
move : function(keyCode) {

// 設置為移動中
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);

},

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


// 改分數外部接口
onChangeScore : function() {}

}

部分難點技術實現

通過鍵盤左右方向鍵移動擋板的代碼實現:
復制代碼 代碼如下:

// 鍵盤按下事件
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) {

// 設置為非發彈中
this.isSend = false;

}

},

// 移動
move : function(keyCode) {

// 設置為移動中
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();

}

},
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲欧美中文字幕| 欧美日韩一区二区免费在线观看| 精品欧美aⅴ在线网站| 成年无码av片在线| 国产美女精品视频免费观看| 成人高清视频观看www| 538国产精品一区二区在线| 国内精品免费午夜毛片| 欧美大片欧美激情性色a∨久久| 欧美激情国产日韩精品一区18| 亚洲国产精品小视频| 欧美视频在线观看免费| 亚洲福利在线播放| 久久久久国产精品一区| 国产精品99久久久久久www| 成人黄色短视频在线观看| 国产精品情侣自拍| 亚洲人成网站在线播| 欧美成人精品h版在线观看| 亚洲品质视频自拍网| 日韩美女在线观看一区| 日韩欧美国产免费播放| 国产精品久久久久久久久免费| 97欧美精品一区二区三区| 亚洲免费一在线| 91免费精品国偷自产在线| 亚洲石原莉奈一区二区在线观看| 日韩av在线导航| 成人在线播放av| 亚洲国产中文字幕久久网| 萌白酱国产一区二区| 亚洲欧美另类人妖| 国产精品偷伦一区二区| 狠狠躁夜夜躁人人爽天天天天97| 俺去啦;欧美日韩| 日韩av中文字幕在线| 国产裸体写真av一区二区| 视频在线观看一区二区| 国产精品视频一区国模私拍| 色在人av网站天堂精品| 91tv亚洲精品香蕉国产一区7ujn| 红桃av永久久久| 中文字幕一精品亚洲无线一区| 韩日精品中文字幕| 中文字幕在线观看日韩| 欧美老少做受xxxx高潮| 国产视频久久久久| 国产精品亚洲激情| 亚洲激情小视频| 欧美日韩在线第一页| 国产日本欧美一区二区三区在线| 亚洲欧美日本伦理| 亚洲天堂男人天堂| 亚洲国产精品99久久| 亚洲天堂2020| 欧美在线欧美在线| 国产女人18毛片水18精品| 色偷偷88888欧美精品久久久| 精品国产成人在线| 国产成人在线精品| 黄色一区二区在线| 欧美日韩另类视频| 2019中文字幕全在线观看| 亚洲一区av在线播放| 色噜噜狠狠色综合网图区| 国产欧美一区二区三区在线| 日本免费在线精品| 亚洲人免费视频| 亚洲护士老师的毛茸茸最新章节| 九色精品免费永久在线| 中文字幕亚洲欧美日韩在线不卡| 777精品视频| 成人激情在线观看| 欧美裸身视频免费观看| 亚洲精品永久免费精品| 中文字幕亚洲在线| 高清欧美性猛交xxxx| 欧美裸体视频网站| 搡老女人一区二区三区视频tv| 热久久免费视频精品| 国产脚交av在线一区二区| 中文字幕免费国产精品| 日韩在线资源网| 欧美与黑人午夜性猛交久久久| 国产精品va在线播放我和闺蜜| 91tv亚洲精品香蕉国产一区7ujn| 亚洲美女免费精品视频在线观看| 久久免费国产精品1| 136fldh精品导航福利| 久久精品夜夜夜夜夜久久| 欧美精品久久久久久久| 亚洲小视频在线| 亚洲xxx视频| 欧美成人午夜影院| 国产亚洲美女精品久久久| 久久久久久久国产精品视频| 国产精品1区2区在线观看| 日韩电影免费在线观看中文字幕| 国内精品久久久久久中文字幕| 亚洲成年人在线| 日韩av资源在线播放| 成人有码视频在线播放| 色中色综合影院手机版在线观看| 国产中文日韩欧美| 精品国产区一区二区三区在线观看| 性欧美xxxx| 欧美插天视频在线播放| 色香阁99久久精品久久久| 中文字幕精品一区二区精品| 亚洲高清久久网| 91探花福利精品国产自产在线| 色爱av美腿丝袜综合粉嫩av| 韩曰欧美视频免费观看| 色婷婷综合久久久久| 欧美另类69精品久久久久9999| 久久久久久999| 日韩电影大全免费观看2023年上| 韩国一区二区电影| 欧美日韩国产区| 91精品国产综合久久香蕉最新版| 久久久久亚洲精品国产| 欧美日韩福利电影| 中文字幕日韩在线视频| 国产精品久久久久久久久久久不卡| 日韩国产欧美区| 国产97人人超碰caoprom| 亚洲精品久久久久久下一站| 亚洲国产古装精品网站| 国产精品久久久久久久久免费| 欧美性猛交xxx| 亚洲国产日韩一区| 欧美国产日韩在线| 中文字幕亚洲情99在线| 国产日韩欧美在线视频观看| 久久影院资源站| 久久91精品国产91久久跳| 国产在线a不卡| 日韩精品视频在线播放| 国产免费成人av| 日韩美女视频中文字幕| 国产精品久久久久久av福利| 九九久久综合网站| 久久琪琪电影院| 成人午夜在线视频一区| 亚洲老头同性xxxxx| 久久久日本电影| 海角国产乱辈乱精品视频| 黑人巨大精品欧美一区免费视频| 欧美性xxxx在线播放| 最近2019年中文视频免费在线观看| 精品久久久久久国产| 欧美精品一二区| 久久久天堂国产精品女人| 欧美超级乱淫片喷水| 中文字幕欧美日韩| 亚洲成人精品视频在线观看| 日韩国产欧美精品在线| 高清欧美性猛交xxxx| 精品久久久久久久久久久| 美女精品久久久| 自拍视频国产精品| 色久欧美在线视频观看| 欧美专区日韩视频| 国产裸体写真av一区二区|