今天用OO寫了一個鍵盤字母的遊戲,可以用於平常的鍵盤練習,寫的不好的地方謝謝指出。
主要實現了效果:
1.實現了積分的累加;
2.積分到一定程度後升級;
3.當字母下落到一定程度的時候就遊戲失敗。
附加效果圖:
以下貼出代碼:
<html>
<head>
<title>鍵盤遊戲完整版</title>
</head>
<script>
var speet=1; //移動的速度
function createDiv(){
var createbutton=document.createElement("div");
createbutton.style.width="800px";
createbutton.style.height="1000px";
createbutton.style.border="2px solid";
document.body.appendChild(createbutton);
}
function createSource(){
var createSource;
var score=500; //升級的最低積分
var sco=0; //積分
var i=1; //等級
init();
function init(){
createSource=document.createElement("div");
createSource.style.width="100px";
createSource.style.height="20px";
createSource.style.border="1px solid blue";
createSource.innerHTML="積分:"+sco;
createSource.position="absolute";
document.body.appendChild(createSource);
}
this.leijia=function(){
sco+=10;
createSource.innerHTML="積分:";
createSource.innerHTML="積分:"+sco;
//alert(zhengbai);
if(sco == score){
score+=500;
alert("恭喜升級:"+i++);
speet+=1;
alert("speett-----------------"+speet);
}
}
}
function CharA(charDiv){
var CharArray=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","S","Y","Z");
var a;
init();
function init(){
a=document.createElement("div");
var charDiv=CharArray[parseInt(Math.random() * 26)];
a.innerHTML=charDiv;
a.style.width="50px";
a.style.position="absolute";
a.style.height="50px";
a.style.top="50px";
a.style.left=Math.random() * 100 + Math.random() * 500 + 50 + "px";
document.body.appendChild(a);
}
this.shoaa=function(){
init();
}
this.move=function(){
a.style.top=parseInt(a.style.top)+speet+"px";
if(parseInt(a.style.top)>700){
alert("game over");
clearInterval(time);
}
}
this.charinner=function(){
return a.innerHTML;
}
this.hidden=function(){
a.style.display="none";
document.body.removeChild(a);
}
}
function Game(){
var chars=new Array();
var cs=new createSource();
createDiv();
function initData(){
for(var i=0;i<10;i++){
var charA=new CharA();
chars.push(charA);
}
}
initData();
this.start=function(){
time=setInterval(function (){
for(var i=0;i<chars.length;i++){
chars[i].move();
}
},100);
}
document.οnkeydοwn=function(e){
for(var i=0;i<chars.length;i++){
//alert(chars[i].innerHTML);
if(String.fromCharCode(e.which)==chars[i].charinner()){
chars[i].hidden();
chars[i].shoaa();
cs.leijia();
}
}
}
}
window.οnlοad=function(){
var game=new Game();
game.start();
}
</script>
<body>
</body>
</html>