用javascript做一個(gè)小游戲平臺(tái) (二) 游戲選擇器
更新時(shí)間:2010年01月01日 01:17:51 作者:
昨天晚上“設(shè)計(jì)”了n久,那些代碼都還沒有運(yùn)行起來,有點(diǎn)心急、有點(diǎn)郁悶。
今天先預(yù)覽一下今晚的成果,如下(挫了點(diǎn),別扔磚頭):

今天主要設(shè)計(jì)了下選擇器,就是進(jìn)入游戲時(shí)展現(xiàn)游戲列表,然后用來選擇游戲的一個(gè)白癡的功能。
選擇器建立在昨天的游戲類基礎(chǔ)上,針對(duì)昨天的代碼我作了部分修改:
//5.游戲類:名稱,邏輯方法,鍵盤方法,開始方法,開始關(guān)卡方法,結(jié)束方法
var Game = function(name, logicalFn, keyFn, startFn, loadFn, endFn) {
//游戲名
this._name = name || "未命名";
//5.a.1:邏輯控制
this._LControl = logicalFn || function(date) {
//簡單游戲邏輯控制
if (this._FrameMain) {
var innHTML = "游戲時(shí)間:" + date.getSeconds() + "秒" + date.getMilliseconds();
innHTML += "<br/>當(dāng)前游戲沒有編寫,您可以按Esc退出該游戲;";
this._FrameMain.innerHTML = innHTML;
}
};
//5.a.2:鍵盤控制
this._KControl = keyFn || function(event) {
//簡單鍵盤邏輯
alert("您敲擊了" + event.keyCode + "鍵");
};
//5.b.1:標(biāo)題區(qū)域
this._FrameTitle = null;
//5.b.2:游戲區(qū)域
this._FrameMain = null;
//5.b.3:狀態(tài)顯示區(qū)
this._FrameState = null;
//5.b.4:控制區(qū)
this._FrameControl = null;
//5.c.1:開場動(dòng)畫
this._AnmLoad = new Animation("進(jìn)入游戲",null);
//5.c.2:過關(guān)動(dòng)畫
this._AnmNext = new Animation("加載中",null);
//5.c.3:結(jié)束動(dòng)畫
this._AnmEnd = new Animation("結(jié)束",null);
//5.d.1:開始:調(diào)用開始動(dòng)畫、開始處理方法、加載游戲
this._Start = function() {
this._AnmLoad = this._AnmLoad || new Animation(null);
var temp = this; //得到當(dāng)前對(duì)象
this._AnmLoad._palyEnd = this._AnmLoad._palyEnd || function() {
startFn && startFn();
temp._Load();
};
this._AnmLoad._play();
};
//5.d.2:結(jié)束
this._End = endFn || function() {
alert("游戲結(jié)束");
};
//5.d.3:加載:每次開始游戲時(shí)(關(guān)卡開始)
this._Load = function() {
this._AnmNext = this._AnmNext || new Animation(null);
var temp = this; //得到當(dāng)前對(duì)象
this._AnmNext._palyEnd = this._AnmNext._palyEnd || function() {
if (!loadFn) {
temp._FrameTitle = _HtmlControl._newPanel(0, 0, 400, 30);
temp._FrameTitle.innerHTML = temp._name || "未命名游戲";
temp._FrameMain = _HtmlControl._newPanel(0, 30, 350, 370);
temp._FrameState = _HtmlControl._newPanel(350, 30, 50, 180);
temp._FrameControl = _HtmlControl._newPanel(350, 210, 50, 190);
_HtmlControl._ClearArea();
_HtmlControl._AddControl(temp._FrameTitle);
_HtmlControl._AddControl(temp._FrameMain);
_HtmlControl._AddControl(temp._FrameState);
_HtmlControl._AddControl(temp._FrameControl);
} else {
loadFn();
}
}
this._AnmNext && this._AnmNext._play();
}
//5.e地圖
this._Map = [];
mapIndex = -1;
};
就是說選擇器它也是游戲類的一個(gè)對(duì)象,有加載動(dòng)畫,也有鍵盤處理等:
//創(chuàng)建游戲
var game1 = new Game("貪吃蛇", null, null);
var game2 = new Game("俄羅斯方塊", null, null);
var game3 = new Game("推箱子", null, null);
var game4 = new Game("賽車", null, null);
var game5 = new Game("坦克大戰(zhàn)", null, null);
//----------------------------------------------------
//6.游戲列表
var _GameList = [game1, game2, game3, game4, game5];
//----------------------------------------------------
//7.游戲選擇器
var _GameChoose = new Game("選擇器", null, null);
{
_GameChoose._Map = _GameList;
_GameChoose._Load = function() {
this._FrameTitle = _HtmlControl._newPanel(0, 0, 400, 30);
this._FrameTitle.innerHTML = "請(qǐng)選擇游戲";
this._FrameMain = _HtmlControl._newPanel(0, 30, 240, 370);
this._FrameState = _HtmlControl._newPanel(240, 30, 160, 180);
this._FrameState.innerHTML = "你可以<br/>使用上下鍵<br/>選擇游戲";
this._FrameControl = _HtmlControl._newPanel(240, 210, 160, 190);
this._FrameControl.innerHTML = "按下Enter<br/>進(jìn)入游戲";
this._tempButtons = [];
this._tempButtonsIndex = 0;
//this._FrameMain.style.滾動(dòng)條//
if (this._Map.length > 0) {
for (var i = 0; i < this._Map.length; i++) {
var button = _HtmlControl._newButton(this._Map[i]._name, 200, 30);
this._FrameMain.appendChild(button);
this._tempButtons.push(button);
}
this._tempButtons[0].select();
}
_HtmlControl._AddControl(this._FrameTitle);
_HtmlControl._AddControl(this._FrameMain);
_HtmlControl._AddControl(this._FrameState);
_HtmlControl._AddControl(this._FrameControl);
};
_GameChoose._LControl = function(date) {
if (mapIndex != -1) {
this._Map && this._Map[mapIndex]._LControl(date);
}
};
_GameChoose._KControl = function(event) {
if (mapIndex == -1) {
switch (event.keyCode) {
case _KeyParameters.KeyUp:
{
//alert("上t");
if (this._tempButtonsIndex > 0) {
this._tempButtonsIndex--;
for (var i = 0; i < this._tempButtons.length; i++) {
this._tempButtons[i].unselect();
}
this._tempButtons[this._tempButtonsIndex].select();
}
}
break;
case _KeyParameters.KeyDown:
{
//alert("下");
if (this._tempButtonsIndex < this._tempButtons.length - 1) {
this._tempButtonsIndex++;
for (var i = 0; i < this._tempButtons.length; i++) {
this._tempButtons[i].unselect();
}
this._tempButtons[this._tempButtonsIndex].select();
}
}
break;
case _KeyParameters.KeyEnter:
{
mapIndex = this._tempButtonsIndex;
this._Map && this._Map[mapIndex]._Start();
}
break;
default:
{
} break;
}
} else {
if (event.keyCode == _KeyParameters.KeyESC) {
mapIndex = -1;
this._Start();
return;
}
this._Map && this._Map[mapIndex]._KControl(event);
}
}
}
就這么寫內(nèi)容,由于時(shí)間關(guān)系,貪吃蛇仍然沒有做,昨天最后一句口號(hào)被公司的人說了,說我把公司分配的任務(wù)扔了。
今天要改一句口號(hào),以促進(jìn)第一個(gè)游戲的完成:白天權(quán)限,晚上貪吃蛇,我要把貪吃蛇做到極致,嘿嘿...
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
寫的明顯很挫,但是為了提升自己的各方面能力,還是貼上來了,歡迎各位批評(píng)。

今天主要設(shè)計(jì)了下選擇器,就是進(jìn)入游戲時(shí)展現(xiàn)游戲列表,然后用來選擇游戲的一個(gè)白癡的功能。
選擇器建立在昨天的游戲類基礎(chǔ)上,針對(duì)昨天的代碼我作了部分修改:
復(fù)制代碼 代碼如下:
//5.游戲類:名稱,邏輯方法,鍵盤方法,開始方法,開始關(guān)卡方法,結(jié)束方法
var Game = function(name, logicalFn, keyFn, startFn, loadFn, endFn) {
//游戲名
this._name = name || "未命名";
//5.a.1:邏輯控制
this._LControl = logicalFn || function(date) {
//簡單游戲邏輯控制
if (this._FrameMain) {
var innHTML = "游戲時(shí)間:" + date.getSeconds() + "秒" + date.getMilliseconds();
innHTML += "<br/>當(dāng)前游戲沒有編寫,您可以按Esc退出該游戲;";
this._FrameMain.innerHTML = innHTML;
}
};
//5.a.2:鍵盤控制
this._KControl = keyFn || function(event) {
//簡單鍵盤邏輯
alert("您敲擊了" + event.keyCode + "鍵");
};
//5.b.1:標(biāo)題區(qū)域
this._FrameTitle = null;
//5.b.2:游戲區(qū)域
this._FrameMain = null;
//5.b.3:狀態(tài)顯示區(qū)
this._FrameState = null;
//5.b.4:控制區(qū)
this._FrameControl = null;
//5.c.1:開場動(dòng)畫
this._AnmLoad = new Animation("進(jìn)入游戲",null);
//5.c.2:過關(guān)動(dòng)畫
this._AnmNext = new Animation("加載中",null);
//5.c.3:結(jié)束動(dòng)畫
this._AnmEnd = new Animation("結(jié)束",null);
//5.d.1:開始:調(diào)用開始動(dòng)畫、開始處理方法、加載游戲
this._Start = function() {
this._AnmLoad = this._AnmLoad || new Animation(null);
var temp = this; //得到當(dāng)前對(duì)象
this._AnmLoad._palyEnd = this._AnmLoad._palyEnd || function() {
startFn && startFn();
temp._Load();
};
this._AnmLoad._play();
};
//5.d.2:結(jié)束
this._End = endFn || function() {
alert("游戲結(jié)束");
};
//5.d.3:加載:每次開始游戲時(shí)(關(guān)卡開始)
this._Load = function() {
this._AnmNext = this._AnmNext || new Animation(null);
var temp = this; //得到當(dāng)前對(duì)象
this._AnmNext._palyEnd = this._AnmNext._palyEnd || function() {
if (!loadFn) {
temp._FrameTitle = _HtmlControl._newPanel(0, 0, 400, 30);
temp._FrameTitle.innerHTML = temp._name || "未命名游戲";
temp._FrameMain = _HtmlControl._newPanel(0, 30, 350, 370);
temp._FrameState = _HtmlControl._newPanel(350, 30, 50, 180);
temp._FrameControl = _HtmlControl._newPanel(350, 210, 50, 190);
_HtmlControl._ClearArea();
_HtmlControl._AddControl(temp._FrameTitle);
_HtmlControl._AddControl(temp._FrameMain);
_HtmlControl._AddControl(temp._FrameState);
_HtmlControl._AddControl(temp._FrameControl);
} else {
loadFn();
}
}
this._AnmNext && this._AnmNext._play();
}
//5.e地圖
this._Map = [];
mapIndex = -1;
};
就是說選擇器它也是游戲類的一個(gè)對(duì)象,有加載動(dòng)畫,也有鍵盤處理等:
復(fù)制代碼 代碼如下:
//創(chuàng)建游戲
var game1 = new Game("貪吃蛇", null, null);
var game2 = new Game("俄羅斯方塊", null, null);
var game3 = new Game("推箱子", null, null);
var game4 = new Game("賽車", null, null);
var game5 = new Game("坦克大戰(zhàn)", null, null);
//----------------------------------------------------
//6.游戲列表
var _GameList = [game1, game2, game3, game4, game5];
//----------------------------------------------------
//7.游戲選擇器
var _GameChoose = new Game("選擇器", null, null);
{
_GameChoose._Map = _GameList;
_GameChoose._Load = function() {
this._FrameTitle = _HtmlControl._newPanel(0, 0, 400, 30);
this._FrameTitle.innerHTML = "請(qǐng)選擇游戲";
this._FrameMain = _HtmlControl._newPanel(0, 30, 240, 370);
this._FrameState = _HtmlControl._newPanel(240, 30, 160, 180);
this._FrameState.innerHTML = "你可以<br/>使用上下鍵<br/>選擇游戲";
this._FrameControl = _HtmlControl._newPanel(240, 210, 160, 190);
this._FrameControl.innerHTML = "按下Enter<br/>進(jìn)入游戲";
this._tempButtons = [];
this._tempButtonsIndex = 0;
//this._FrameMain.style.滾動(dòng)條//
if (this._Map.length > 0) {
for (var i = 0; i < this._Map.length; i++) {
var button = _HtmlControl._newButton(this._Map[i]._name, 200, 30);
this._FrameMain.appendChild(button);
this._tempButtons.push(button);
}
this._tempButtons[0].select();
}
_HtmlControl._AddControl(this._FrameTitle);
_HtmlControl._AddControl(this._FrameMain);
_HtmlControl._AddControl(this._FrameState);
_HtmlControl._AddControl(this._FrameControl);
};
_GameChoose._LControl = function(date) {
if (mapIndex != -1) {
this._Map && this._Map[mapIndex]._LControl(date);
}
};
_GameChoose._KControl = function(event) {
if (mapIndex == -1) {
switch (event.keyCode) {
case _KeyParameters.KeyUp:
{
//alert("上t");
if (this._tempButtonsIndex > 0) {
this._tempButtonsIndex--;
for (var i = 0; i < this._tempButtons.length; i++) {
this._tempButtons[i].unselect();
}
this._tempButtons[this._tempButtonsIndex].select();
}
}
break;
case _KeyParameters.KeyDown:
{
//alert("下");
if (this._tempButtonsIndex < this._tempButtons.length - 1) {
this._tempButtonsIndex++;
for (var i = 0; i < this._tempButtons.length; i++) {
this._tempButtons[i].unselect();
}
this._tempButtons[this._tempButtonsIndex].select();
}
}
break;
case _KeyParameters.KeyEnter:
{
mapIndex = this._tempButtonsIndex;
this._Map && this._Map[mapIndex]._Start();
}
break;
default:
{
} break;
}
} else {
if (event.keyCode == _KeyParameters.KeyESC) {
mapIndex = -1;
this._Start();
return;
}
this._Map && this._Map[mapIndex]._KControl(event);
}
}
}
就這么寫內(nèi)容,由于時(shí)間關(guān)系,貪吃蛇仍然沒有做,昨天最后一句口號(hào)被公司的人說了,說我把公司分配的任務(wù)扔了。
今天要改一句口號(hào),以促進(jìn)第一個(gè)游戲的完成:白天權(quán)限,晚上貪吃蛇,我要把貪吃蛇做到極致,嘿嘿...
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
寫的明顯很挫,但是為了提升自己的各方面能力,還是貼上來了,歡迎各位批評(píng)。
您可能感興趣的文章:
- 用js做一個(gè)小游戲平臺(tái) (一)
- 12個(gè)非常有創(chuàng)意的JavaScript小游戲
- 由JavaScript技術(shù)實(shí)現(xiàn)的web小游戲(不含網(wǎng)游)
- 原創(chuàng)javascript小游戲?qū)崿F(xiàn)代碼
- JavaScript打字小游戲代碼
- 打豆豆小游戲 用javascript編寫的[打豆豆]小游戲
- js猜數(shù)字小游戲的簡單實(shí)現(xiàn)代碼
- JavaScript制作windows經(jīng)典掃雷小游戲
- Javascript編寫2048小游戲
- 純javascript模仿微信打飛機(jī)小游戲
- javascript實(shí)現(xiàn)別踩白塊兒小游戲程序
相關(guān)文章
JS獲得多個(gè)同name 的input輸入框的值的實(shí)現(xiàn)方法
這篇文章主要介紹了基于JS代碼實(shí)現(xiàn)input密碼輸入框輸入密碼變黑點(diǎn)密文以及JS獲得多個(gè)同name 的input輸入框的值的實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
webpack中使用Eslint的實(shí)現(xiàn)
本文主要介紹了webpack中使用Eslint的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
微信小程序?qū)崿F(xiàn)Session功能及無法獲取session問題的解決方法
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)Session功能及無法獲取session問題的解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
關(guān)于ES6尾調(diào)用優(yōu)化的使用
這篇文章主要介紹了關(guān)于ES6尾調(diào)用優(yōu)化的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Bootstrap Table 雙擊、單擊行獲取該行及全表內(nèi)容
這篇文章主要介紹了Bootstrap Table 雙擊、單擊行獲取該行內(nèi)容及獲取全表的內(nèi)容,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
javascript實(shí)現(xiàn)避免頁面按鈕重復(fù)提交
很多時(shí)候我們都需要防止重復(fù)提交,這方面的文章也比較多,實(shí)現(xiàn)的途徑差別也很大.因?yàn)橛行r(shí)候即使服務(wù)器能夠識(shí)別重復(fù)的提交,也會(huì)造成問題.比如需要很長等待時(shí)間的操作,在首次提交后,不斷重復(fù)提交,頁面可能會(huì)死掉.用腳本來控制的話可以防止這種問題.2015-01-01
在JavaScript中使用Promise.allSettled()的方法
Promise.allSettled()是一個(gè)游戲規(guī)則改變者,讓您等待所有承諾得到解決(解決或拒絕),然后根據(jù)結(jié)果采取行動(dòng),這篇文章主要介紹了如何在JavaScript中使用Promise.allSettled(),需要的朋友可以參考下2023-07-07
JS window.opener返回父頁面的應(yīng)用
網(wǎng)上支付開發(fā)分為支付平臺(tái)和客戶端兩部分。當(dāng)客戶端進(jìn)入支付平臺(tái)時(shí),需要在新窗體打開支付平臺(tái)頁面。2009-10-10
JavaScript增加數(shù)組中指定元素的5種方法總結(jié)
在JS中數(shù)組方法是非常重要且常用的的方法,在此整理總結(jié)一番,下面這篇文章主要給大家介紹了關(guān)于JavaScript增加數(shù)組中指定元素的5種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02

