原生js實(shí)現(xiàn)的金山打字小游戲(實(shí)例代碼詳解)
首先先來(lái)看一下效果圖


如果感興趣的就來(lái)看一下Js源碼吧
//計(jì)分板
var board = {
dom: document.getElementById("score"),
maxLost: 3, //最大丟失量
lost: 0, //當(dāng)前丟失了多少個(gè)
score: 0, //當(dāng)前分?jǐn)?shù)
render: function() {
//顯示
this.dom.innerHTML =
"<p>得分:" +
this.score +
"</p><p>丟失:" +
this.lost +
" / " +
this.maxLost +
"</p>";
},
//增加一個(gè)丟失數(shù)
addLost: function() {
if (this.lost === this.maxLost) {
return; //游戲已經(jīng)結(jié)束了
}
this.lost++;
if (this.lost === this.maxLost) {
//丟失量達(dá)到最大
game.gameOver();
}
this.render();
},
reset: function() {
this.lost = 0;
this.score = 0;
this.render();
},
//增加得分
addScore: function(number) {
if (this.lost === this.maxLost) {
//已經(jīng)結(jié)束了
return;
}
this.score += number;
this.render();
}
};
board.render();
var letters = []; //目前的所有字母,一個(gè)字母就是一個(gè)對(duì)象
//字母的容器
var divContainer = document.getElementById("letter-container");
/**
* 產(chǎn)生一個(gè)字母對(duì)象,并將其加入到數(shù)組中
*/
function createLetter() {
//創(chuàng)建img元素
var img = document.createElement("img");
img.className = "letter";
divContainer.appendChild(img);
//設(shè)置src路徑
var charNumber = getRandom(65, 65 + 26); //字母的隨機(jī)ASCII碼
var char = String.fromCharCode(charNumber);
img.src = "img/letter/" + char + ".png";
//left隨機(jī)
var left = getRandom(0, divContainer.clientWidth - img.width);
img.style.left = left + "px";
var letter = {
dom: img,
char: char,
left: left,
top: -img.height, //初始的top值
speed: getRandom(100, 500), //速度: 像素/秒
render: function() {
this.dom.style.top = this.top + "px";
},
// 每調(diào)用一次該方法,字母移動(dòng)一段距離
// duration是經(jīng)過(guò)的時(shí)間: 秒
move: function(duration) {
var dis = duration * this.speed; //計(jì)算距離
this.top += dis;
this.render();
},
kill: function() {
// 從數(shù)組中移除
var index = letters.indexOf(this); //找到字母在數(shù)組中的下標(biāo)
if (index >= 0) {
letters.splice(index, 1);
}
// 移除dom元素
this.dom.remove();
}
};
letter.render();
letters.push(letter);
}
// 根據(jù)最小值和最大值產(chǎn)生一個(gè)隨機(jī)整數(shù)(不包含最大值)
function getRandom(min, max) {
// Math.random() 0~1
// Math.random() * (max - min) 0 ~ (max - min)
// Math.random() * (max - min) + min min ~ max
return Math.floor(Math.random() * (max - min) + min);
}
//游戲?qū)ο?,統(tǒng)籌規(guī)劃
var game = {
timerProduce: null, //自動(dòng)產(chǎn)生字母的timerid
timerMove: null, //自動(dòng)移動(dòng)的timerid
isOver: false,
//自動(dòng)的,不斷的,產(chǎn)生字母
startProduce: function() {
if (this.timerProduce) {
return; //正在產(chǎn)生中,什么也不做
}
this.timerProduce = setInterval(createLetter, 500);
},
//停止自動(dòng)產(chǎn)生字母
stopProduce: function() {
clearInterval(this.timerProduce);
this.timerProduce = null;
},
//開(kāi)始不斷的移動(dòng)所有字母
startMove: function() {
if (this.timerMove) {
return;
}
var duration = 0.016; //間隔時(shí)間,秒
this.timerMove = setInterval(function() {
for (var i = 0; i < letters.length; i++) {
var letter = letters[i]; //要移動(dòng)的字母
letter.move(duration);
//判斷該字母是不是可以消除了
if (letter.top >= divContainer.clientHeight) {
letter.kill();
i--;
//丟失
board.addLost();
}
}
}, duration * 1000);
},
//停止移動(dòng)所有字母
stopMove: function() {
clearInterval(this.timerMove);
this.timerMove = null;
},
gameOver: function() {
this.stopMove();
this.stopProduce();
document.getElementById("over").style.display = "block";
this.isOver = true;
},
restart: function() {
//清空字母
for (var i = 0; i < letters.length; i++) {
var letter = letters[i];
letter.kill();
i--;
}
this.startMove();
this.startProduce();
board.reset();
this.isOver = false;
document.getElementById("over").style.display = "none";
}
};
game.startProduce();
game.startMove();
//注冊(cè)事件
window.onkeydown = function(e) {
if (game.isOver) {
return;
}
var key = e.key.toUpperCase();
//匹配
for (var i = 0; i < letters.length; i++) {
var letter = letters[i];
if (letter.char === key) {
letter.kill();
board.addScore(10);
return; //只移除一個(gè)
}
}
};僅僅使用js的面向?qū)ο缶幊?,可?ài)的金山打字小游戲就實(shí)現(xiàn)了
總結(jié)
到此這篇關(guān)于原生js實(shí)現(xiàn)的金山打字小游戲的文章就介紹到這了,更多相關(guān)js金山打字游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用js實(shí)現(xiàn)下載遠(yuǎn)程文件并保存在本地的腳本
//將常用的vbs下載者改成js版了。本來(lái)想用jsc.exe編譯,可是不成功。jsc.exe不認(rèn)WScript2008-05-05
怎樣用JavaScript實(shí)現(xiàn)原型模式
這篇文章主要介紹了怎樣用JavaScript實(shí)現(xiàn)原型模式,想學(xué)習(xí)設(shè)計(jì)模式的同學(xué),可以參考下2021-04-04
.net JS模擬Repeater控件的實(shí)現(xiàn)代碼
一個(gè)模板控件規(guī)定了它的模板語(yǔ)法和js api,這是一個(gè)repeater控件的JS實(shí)現(xiàn):2013-06-06
JavaScript實(shí)現(xiàn)在數(shù)組中查找不同順序排列的字符串
這篇文章主要介紹了JavaScript實(shí)現(xiàn)在數(shù)組中查找不同順序排列的字符串,本文用兩個(gè)方法解決了這道算法題,需要的朋友可以參考下2014-09-09
微信小程序?qū)崿F(xiàn)自定義modal彈窗封裝的方法
這篇文章主要介紹了小程序自定義modal彈窗封裝實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06
javascript 對(duì)象數(shù)組根據(jù)對(duì)象object key的值排序
本文僅僅是實(shí)現(xiàn)了javascript 對(duì)象數(shù)組根據(jù)對(duì)象object key的值排序,算是對(duì)自己學(xué)習(xí)javascript這么久的一個(gè)小結(jié),希望大家能夠喜歡2015-03-03
淺談?dòng)肳ebpack路徑壓縮圖片上傳尺寸獲取的問(wèn)題
下面小編就為大家分享一篇淺談?dòng)肳ebpack路徑壓縮圖片上傳尺寸獲取的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

