search
HomeWeb Front-endJS TutorialJavaScript typing game code_javascript skills

Function module: Program design:
1. You can select the game time and display the countdown 1. Define global variables
2. You can select the number of English letters to appear 2. Control the game time function
3. Statistical scores 3. Animation effect
4. Menu option 4. Set the time when the letter picture appears
5. Judgment function
6. Game menu
7. Game time option
8. Display game time
       9. Game difficulty options
     10. Game score
 
First, the renderings: (PS: The art is flawed)
JavaScript typing game code_javascript skills
Main code design:

Copy code The code is as follows:
 
//-------全局变量------- 
var data={ 
"10":["JavaScript typing game code_javascript skills"],"11":["JavaScript typing game code_javascript skills"],"12":["JavaScript typing game code_javascript skills"],"13":["JavaScript typing game code_javascript skills"], 
"14":["JavaScript typing game code_javascript skills"],"15":["JavaScript typing game code_javascript skills"],"16":["JavaScript typing game code_javascript skills"],"17":["JavaScript typing game code_javascript skills"], 
"18":["JavaScript typing game code_javascript skills"],"19":["JavaScript typing game code_javascript skills"],"20":["JavaScript typing game code_javascript skills"],"21":["JavaScript typing game code_javascript skills"], 
"22":["JavaScript typing game code_javascript skills"],"23":["JavaScript typing game code_javascript skills"],"24":["JavaScript typing game code_javascript skills"],"25":["JavaScript typing game code_javascript skills"], 
"26":["JavaScript typing game code_javascript skills"],"27":["JavaScript typing game code_javascript skills"],"28":["JavaScript typing game code_javascript skills"],"29":["JavaScript typing game code_javascript skills"], 
"30":["JavaScript typing game code_javascript skills"],"31":["JavaScript typing game code_javascript skills"],"32":["JavaScript typing game code_javascript skills"],"33":["JavaScript typing game code_javascript skills"], 
"34":["JavaScript typing game code_javascript skills"],"35":["JavaScript typing game code_javascript skills"]
};
var datas=new Array();//随机出现的 class样式以数组中偶数存储,图片以数组中奇数存储
var now=new Date();
var Image; //随机出现图片
var Divs;//随机出现层
var count=0;//积分系统
var key;//键盘的值
var amounts=1;//出现字母图片的个数
var gametime=30;//控制游戏的时间
var gametimes;//时间为0
var gametimess=30;//显示时钟变量
var time1;//setInterval变量
var time2=5000;//设定setInterval的时间
var time3;
var tab;//用来记录,传递tabindex焦点位置的值
//--------这样写为了兼容FF浏览器-------
var plug = {
addEvent:function(o,e,f){
if(o.addEventListener){
o.addEventListener(e,f,false);
}
else if(o.attachEvent){
o.attachEvent("on"+e,f);
}
}
}
plug.addEvent(window,"load",function(){Focus()});//兼容FF浏览器
//------1.控制游戏时间函数--------
function Gametime(){
for(gametimes=gametime;gametimes>=0;gametimes--) {
window.setTimeout('Show(' + gametimes + ')',(gametime-gametimes+2) * 1000);
}
}
function Show(gametimes){
if(gametimes==0){
clearInterval(time1);//停止游戏
alert("游戏结束!你的得分为:"+count);
$("#main").empty();//清除main中的div
$(".gameapply").empty();
$("#select1").empty();
$("#select2").empty();
count=0;//得分清空为0
Score();//让分数框显示为0
Focus();//重新生成菜单选项
}
}
//---------2.动画效果---------
function fun(){
datas.length=0;
for(var i=0;iImage=parseInt(Math.random() * 26)+10;//随机出现字母图片
datas.push(Image);//图片以数组中偶数存储,从零开始
Divs=parseInt(Math.random() * 8)+1;//随机出现层的class样式即层的不同位置
datas.push(Divs);//样式以数组中奇数存储
var time=parseInt(Math.random() * 2000)+3000;//完成动画的时间
if(timevar $divs=$("
"+data[Image]+"
");
$("#main").append($divs);
//-----JQ animation function----
$(".divPop" Divs). animate(
{"top":$(window).height() - $(".divPop" Divs).height() - $(".divPop" Divs).position().top},time, function(){$("#main").empty()})
}
}
}
//--------3. Set the letter picture to appear Time ------
function sets(){
time1 = setInterval(fun,time2);
}
//---------4. Bind keyboard ---------
//---------Compatible with FF browser---------
document.onkeydown = function keydown(e){
e = e||window.event;
var key = e.charCode||e.keyCode
select(key)
}
//--------- 5. Judgment function---------
function select(key){
if(key==13){
switch(tab){
case 0:Gametime() ;Gametimeselect();time3=setInterval(countdown,1000);sets();$("#select").hide(2000);break;//Start the game
case 1:Gametime();Gametimeselect() ;time3=setInterval(countdown,1000);sets();$("#select").hide(2000);break;//Start the game
case 2:alert("Game options can be set on the left" );break;
case 3:window.opener=null;window.open('','_self');window.close();break;//Exit the game
case 4:window.opener= null;window.open('','_self');window.close();break;//Exit the game
}
}
for(var j=0;jif(key== datas[j] 55){
$(".divPop" datas[j 1]).hide();//The key values ​​​​are equal, hide the layer
delete datas[j]; //In order to avoid having Duplicate letters, so every time a (key==datas[j] 55) is traversed in the array, this value must be deleted
count =10;
Score();
break;
}
}
}
//---------6. Game menu-------
function Focus(){
//----- ----Initialize the game interface--------
var $selects=$("
< ;td>
");
$(".gameapply").append($selects);
for( var i=30;i$("#select1").append('')
}
for(var j=1;j$("#select2").append('')
}
//---------Get the focus of the first input at the beginning--------
$(".inputs:first").trigger("focus").addClass ("input1");
tab=1;//Because Enter cannot be entered in the first focus, the keyboard input event needs to be called
var tabIndex=1;
//----- -----Get the number of tr rows and the number of inputs--------
$("#tables").find("tr").each(function(r) {
$ (this).find("input").attr("tabindex", r 1);//tabindex is the value of the focus position, the initial value is assigned to 1, and the traversal is 1-2-3-4
}) ;
//---------Response to input keyboard up and down operations
$("#tables .inputs").bind("keydown", function(e){
tabIndex = parseInt($(this).attr("tabindex"));//Get the value of the current tabindex focus
switch(e.which){
case 38://up
tabIndex-=1;
tab=tabIndex;
break;
case 40://down
tabIndex =1;
tab=tabIndex;
break;
default:
return ;
}
//--------Determine the value of tabIndex focus
if (tabIndex > 0 && tabIndex $(".inputs[tabindex= " tabIndex "]").focus().addClass("input1"); //The current input gets focus
for(var i=0;i{
if( i==tabIndex){
break;
}
else{
$(this).removeClass("input1");
}
}
keydown();
return false;
}
return true;
});
}
//--------7. Game time options------ ---
function Gametimeselect(){
var option=document.getElementById("select1");
for(var i=0;iif( option[i].selected) {
gametime=option.options[i].text;
gametimess=gametime;
}
}
}
//---- -8. Display game time -------
function countdown(){
var timesshows=document.getElementById("timeshow");
if(timeshows){//If the web page speed is very fast If it is slow, the control may not be loaded when the timer is running
if(gametimessclearInterval(time3);//Stop the timer
}
else{
timeshows. value=gametimess;
gametimess--;
}
}
}
//--------9. Game difficulty options---------
function Gameselectamount(){
var option=document.getElementById("select2");
for(var i=0;iif(option[i ].selected) {
amounts=option.options[i].text;
}
}
}
//---------10. Game score- ----------
function Score(){
var sum=document.getElementById("sum");
sum.value=count;
if(count== 0){ //Initialize the text box score to 0
sum.value=count;
}
}

Summary: Since the time interval is relatively long, the code has not been optimized. There is one place that is not done very well, that is, the time interval between letters appearing is too long. If you are interested, you can try to fix it. The code is for reference only
Online demo:http://demo.jb51.net/js/2011/StarWars/
Package download: http://www.jb51.net /jiaoben/40902.html
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!