Home Web Front-end JS Tutorial JavaScript typing game code_javascript skills

JavaScript typing game code_javascript skills

May 16, 2016 pm 05:57 PM

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":[""],"11":[""],"12":[""],"13":[""], 
"14":[""],"15":[""],"16":[""],"17":[""], 
"18":[""],"19":[""],"20":[""],"21":[""], 
"22":[""],"23":[""],"24":[""],"25":[""], 
"26":[""],"27":[""],"28":[""],"29":[""], 
"30":[""],"31":[""],"32":[""],"33":[""], 
"34":[""],"35":[""]
};
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(time<5000){
var $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<=300;i=i 30){
$("#select1").append('')
}
for(var j=1;j<=9;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 < 4) {
$(".inputs[tabindex= " tabIndex "]").focus().addClass("input1"); //The current input gets focus
for(var i=0;i<=4;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(gametimess<0){
clearInterval(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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

10 Ways to Instantly Increase Your jQuery Performance 10 Ways to Instantly Increase Your jQuery Performance Mar 11, 2025 am 12:15 AM

This article outlines ten simple steps to significantly boost your script's performance. These techniques are straightforward and applicable to all skill levels. Stay Updated: Utilize a package manager like NPM with a bundler such as Vite to ensure

Using Passport With Sequelize and MySQL Using Passport With Sequelize and MySQL Mar 11, 2025 am 11:04 AM

Sequelize is a promise-based Node.js ORM. It can be used with PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. In this tutorial, we will be implementing authentication for users of a web app. And we will use Passport, the popular authentication middlew

How to Build a Simple jQuery Slider How to Build a Simple jQuery Slider Mar 11, 2025 am 12:19 AM

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

See all articles