Create a game platform using javascript (2) Game Selector

Source: Internet
Author: User

Today, let's preview the results of tonight, as shown in the following figure ):

Today, I mainly designed the selector to display the game list during the game and then use it to select an idiot function of the game.

The selector is based on yesterday's game class and I made some modifications to yesterday's code:Copy codeThe Code is as follows: // 5. Game class: name, logical method, keyboard method, start method, start level method, and end Method
Var Game = function (name, logicalFn, keyFn, startFn, loadFn, endFn ){
// Game name
This. _ name = name | "not named ";
// 5. a.1: Logical Control
This. _ LControl = logicalFn | function (date ){
// Simple game logic control
If (this. _ FrameMain ){
Var innHTML = "game time:" + date. getSeconds () + "second" + date. getMilliseconds ();
InnHTML + = "<br/> the current game is not written. You can press Esc to exit the game ;";
This. _ FrameMain. innerHTML = innHTML;
}
};
// 5.a.2: keyboard control
This. _ KControl = keyFn | function (event ){
// Simple keyboard Logic
Alert ("You knocked" + event. keyCode + "key ");
};
// 5. B .1: Title Area
This. _ FrameTitle = null;
// 5. B .2: Game Region
This. _ FrameMain = null;
// 5. B .3: Status display area
This. _ FrameState = null;
// 5. B .4: Control Area
This. _ FrameControl = null;
// 5. c.1: Opening Animation
This. _ AnmLoad = new Animation ("enter the game", null );
// 5. c.2: Pass Animation
This. _ AnmNext = new Animation ("loading", null );
// 5. c.3: End Animation
This. _ AnmEnd = new Animation ("end", null );
// 5. d.1: Start: Call the start animation, start the processing method, and load the game.
This. _ Start = function (){
This. _ AnmLoad = this. _ AnmLoad | new Animation (null );
Var temp = this; // get the current object
This. _ AnmLoad. _ palyEnd = this. _ AnmLoad. _ palyEnd | function (){
StartFn & startFn ();
Temp. _ Load ();
};
This. _ AnmLoad. _ play ();
};
// 5. d.2: End
This. _ End = endFn | function (){
Alert ("game ended ");
};
// 5. d.3: loading: each time the game starts (the level starts)
This. _ Load = function (){
This. _ AnmNext = this. _ AnmNext | new Animation (null );
Var temp = this; // get the current object
This. _ AnmNext. _ palyEnd = this. _ AnmNext. _ palyEnd | function (){
If (! LoadFn ){
Temp. _ FrameTitle = _ HtmlControl. _ newPanel (0, 0,400, 30 );
Temp. _ FrameTitle. innerHTML = temp. _ name | "Unnamed game ";
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 Map
This. _ Map = [];
MapIndex =-1;
};

That is to say, the selector is also an object of the game class, including loading animations and keyboard processing:Copy codeThe Code is as follows: // create a game
Var game1 = new Game ("snake", null, null );
Var game2 = new Game ("Tetris", null, null );
Var game3 = new Game ("Push box", null, null );
Var game4 = new Game ("racing car", null, null );
Var game5 = new Game ("tank wars", null, null );
//----------------------------------------------------
// 6. Game list
Var _ GameList = [game1, game2, game3, game4, game5];
//----------------------------------------------------
// 7. Game Selector
Var _ GameChoose = new Game ("selector", null, null );
{
_ GameChoose. _ Map = _ GameList;
_ GameChoose. _ Load = function (){
This. _ FrameTitle = _ HtmlControl. _ newPanel (0, 0,400, 30 );
This. _ FrameTitle. innerHTML = "select a game ";
This. _ FrameMain = _ HtmlControl. _ newPanel (0, 30,240,370 );
This. _ FrameState = _ HtmlControl. _ newPanel (240, 30,160,180 );
This. _ FrameState. innerHTML = "you can <br/> use the up/down key <br/> select a game ";
This. _ FrameControl = _ HtmlControl. _ newPanel (240,210,160,190 );
This. _ FrameControl. innerHTML = "Press Enter <br/> to Enter the game ";
This. _ tempButtons = [];
This. _ tempButtonsIndex = 0;
// This. _ FrameMain. style. scroll bar //
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 ("up to 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 ("bottom ");
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 );
}
}
}

So I wrote the content. Due to the time relationship, the snake was still not done. Yesterday, the last slogan was said by the company, saying that I threw away the task assigned by the company.

Today, I want to change a slogan to promote the completion of the first game: daytime permission, evening greedy snake, I want to make the ultimate greedy snake, hey...<Title> </title> <br/> <style type = "text/css">. button {</p> <p >}</style> <p>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.