Javascript typing game implementation code

Source: Internet
Author: User

Effect:

Below is the core code
Copy codeThe Code is as follows:
GAME = {
// Randomly generate letters
RandLetter: function (){
Var arrLetter = new Array ("A", "B", "C", "D", "E", "F", "G", "H ", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R ", "S", "T", "U", "V", "W ",
"X ",
"Y", "Z ");
// Randomly generate letters
Var index = Math. floor (Math. random () * 26 );
Return arrLetter [index];
},
// Random letter color
RandLetterColor: function (){
Var arrLetterColor = new Array ("Red", "Green", "#555", "Blue", "Black ");
Var index = Math. floor (Math. random () * 4 );
Return arrLetterColor [index];
},
// Random letter size
RandLetterSize: function (){
Var arrLetterSize = new Array ("12px", "16px", "20px", "24px", "28px", "32px", "36px", "40px ");
Var index = Math. floor (Math. random () * 7 );
Return arrLetterSize [index];
},
// Create a DIV
DivCreate: function (width, height, left, top, value ){
This. width = width;
This. height = height;
This. div = document. createElement ("div ");
This. div. style. width = width;
This. div. style. height = height;
This. div. style. left = left;
This. div. style. top = top;
This. div. innerText = value;
This. div. style. color = this. randLetterColor ();
This. div. style. fontSize = this. randLetterSize ();
This. div. style. lineHeight = this. div. style. height;
This. div. style. textAlign = "center ";
This. div. style. fontWeight = "bold ";
// This. div. style. border = "solid red 1px ";
This. div. style. position = "relative ";
Document. getElementById ("map"). appendChild (this. div );
Return this. div;
},
// DIV whereabouts
DivDown: function (){
Var divTop = parseInt (this. div. style. top. slice (0,-2); // Top of the letter box
Var mapHeight = parseInt (document. getElementById ("map"). style. height. slice (0,-2 ));
// Disappear
If (divTop <mapHeight-parseInt (this. height) + 20 ){
This. div. style. top = divTop + 30;
// Obtain the key value
Document. onkeydown = function (){
// Whether the key letter is equal to the div Value
If (String. fromCharCode (event. keyCode) = GAME. div. innerText ){
Document. getElementById ("TextRecord"). value = "correct ";
GAME. div. style. display = "none ";
ClearInterval (GAME. timeCreateID );
GAME. divCreate (100,100, Math. floor (Math. random () * 300),-30, GAME. randLetter ());
}
Else {
Document. getElementById ("TextRecord"). value = "error ";
}
}
}
// Disappear when the bottom line is reached, and then create a DIV
Else {
This. div. style. display = "none ";
GAME. divCreate (100,100, Math. floor (Math. random () * 300),-30, this. randLetter ());
}
},
TimeCreateID: null,
TimeDownID: null,
START: function (){
This. divCreate (100,100,200,-40, this. randLetter ());
This. timeDownID = setInterval ("GAME. divDown ();", 1000 );
Document. getElementById ('buttonstart'). disabled = 'Disabled ';
Document. getElementById ('buttonstop'). disabled = '';
},
STOP: function (){
If (this. timeDownID! = Null ){
ClearInterval (this. timeDownID );
This. div. style. display = "none ";
}
Document. getElementById ('buttonstart'). disabled = '';
Document. getElementById ('buttonstop'). disabled = 'Disabled ';
}

}

Effect demonstration
Snmon becomes a positive question--|, the question requirement must be 100 times more complex... <P> currently, only random letters are generated, and the keyboard is listened to. The score is not calculated yet. </P> score: <input id = "TextRecord" value = "0" type = "text"/> <input id = "ButtonStart" onclick = "GAME. START (); "value =" game start "type =" button "> <input id =" ButtonStop "onclick =" GAME. STOP (); "value =" game stop "type =" button ">
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.