Javascript typing game implementation code _ javascript skills

Source: Internet
Author: User
Javascript typing game implementation code, very good results, the function is not very complete, like a friend can refer. Effect:

Below is the core code

The 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
PCreate: function (width, height, left, top, value ){
This. width = width;
This. height = height;
This. p = document. createElement ("p ");
This. p. style. width = width;
This. p. style. height = height;
This. p. style. left = left;
This. p. style. top = top;
This. p. innerText = value;
This. p. style. color = this. randLetterColor ();
This. p. style. fontSize = this. randLetterSize ();
This. p. style. lineHeight = this. p. style. height;
This. p. style. textAlign = "center ";
This. p. style. fontWeight = "bold ";
// This. p. style. border = "solid red 1px ";
This. p. style. position = "relative ";
Document. getElementById ("map"). appendChild (this. p );
Return this. p;
},
// DIV whereabouts
PDown: function (){
Var pTop = parseInt (this. p. style. top. slice (0,-2); // Top of the letter box
Var mapHeight = parseInt (document. getElementById ("map"). style. height. slice (0,-2 ));
// Disappear
If (pTop <mapHeight-parseInt (this. height) + 20 ){
This. p. style. top = pTop + 30;
// Obtain the key value
Document. onkeydown = function (){
// Whether the key letter is equal to the value of p
If (String. fromCharCode (event. keyCode) = GAME. p. innerText ){
Document. getElementById ("TextRecord"). value = "correct ";
GAME. p. style. display = "none ";
ClearInterval (GAME. timeCreateID );
GAME. pCreate (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. p. style. display = "none ";
GAME. pCreate (100,100, Math. floor (Math. random () * 300),-30, this. randLetter ());
}
},
TimeCreateID: null,
TimeDownID: null,
START: function (){
This. pCreate (100,100,200,-40, this. randLetter ());
This. timeDownID = setInterval ("GAME. pDown ();", 1000 );
Document. getElementById ('buttonstart'). disabled = 'Disabled ';
Document. getElementById ('buttonstop'). disabled = '';
},
STOP: function (){
If (this. timeDownID! = Null ){
ClearInterval (this. timeDownID );
This. p. style. display = "none ";
}
Document. getElementById ('buttonstart'). disabled = '';
Document. getElementById ('buttonstop'). disabled = 'Disabled ';
}

}


Effect demonstration

<Script type = "text/javascript"> GAME = {// randomly generate the 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"); // a random 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 DIV pCreate: function (width, height, left, top, value) {this. width = width; this. height = Height; this. p = document. createElement ("p"); this. p. style. width = width; this. p. style. height = height; this. p. style. left = left; this. p. style. top = top; this. p. innerText = value; this. p. style. color = this. randLetterColor (); this. p. style. fontSize = this. randLetterSize (); this. p. style. lineHeight = this. p. style. height; this. p. style. textAlign = "center"; this. p. style. fontWeight = "bold"; // this. p. Style. border = "solid red 1px"; this. p. style. position = "relative"; document. getElementById ("map "). appendChild (this. p); return this. p ;}, // DIV whereabouts pDown: function () {var pTop = parseInt (this. p. style. top. slice (0,-2); // Top var mapHeight = parseInt (document. getElementById ("map "). style. height. slice (0,-2); // It disappears if (pTop <mapHeight-parseInt (this. height) + 20) {this. p. style. top = pTop + 30; // Obtain the key value document. onkeydown = function () {// whether the letter of the key is equal to the value of p if (String. fromCharCode (event. keyCode) = GAME. p. innerText) {document. getElementById ("TextRecord "). value = "correct"; GAME. p. style. display = "none"; clearInterval (GAME. timeCreateID); GAME. pCreate (100,100, Math. floor (Math. random () * 300),-30, GAME. randLetter ();} else {document. getElementById ("TextRecord "). value = "error" ;}}// it disappears when it reaches the bottom line, Then create DIV else {this. p. style. display = "none"; GAME. pCreate (100,100, Math. floor (Math. random () * 300),-30, this. randLetter () ;}}, timeCreateID: null, timeDownID: null, START: function () {this. pCreate (100,100,200,-40, this. randLetter (); this. timeDownID = setInterval ("GAME. pDown (); ", 1000); document. getElementById ('buttonstart '). disabled = 'Disabled '; document. getElementById ('buttonstop '). di Sabled = '';}, STOP: function () {if (this. timeDownID! = Null) {clearInterval (this. timeDownID); this. p. style. display = "none";} document. getElementById ('buttonstart '). disabled = ''; document. getElementById ('buttonstop '). disabled = 'Disabled ';} "script" 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> <p style = "border-bottom: blue 1px solid; border-left: blue 1px solid; width: 400px; height: 300px; border-top: blue 1px solid; border-right: blue 1px solid "id =" map "> </p> <p id =" foot "> score: <input id = "TextRecord" value = "0" type = "text"/> </p> <input id = "ButtonStart" onclick = "GAME. START (); "value =" game start "type =" button "> <input id =" ButtonStop "onclick =" GAME. STOP (); "value =" game stop "type =" button "> <p> </p>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.