游戏名称:雷电游戏
游戏简介:
游戏类型:益智游戏
游戏操作:按键盘的上下左右键来控制战机的移动,打中敌机,敌机就会爆炸
代码分享:
<html>
<head>
<title>雷电贝贝版</title>
<script>
//start game
function start(){
//alert("start");
fighterMove(); //call fighter move way
//fireBullet(); //call fire Bullet way
enemyMove(); // call enemy move way
backgroundMove(); //call background loop moving way
timer=setInterval("fireBullet()",100);
timers=setInterval("enemyFiredBullet()",2000);
}
//background loop moving
j=0;
function backgroundMove(){
//alert("backgroundMove");
j++;
interface.style.backgroundPosition="0 "+j;
setTimeout("backgroundMove()",16);
}
//control fighter move
function fighterMove(){
//alert("fighterMove");
document.onkeydown = keydown;
function keydown(e){
var keyName=String.fromCharCode(e.which);
//alert(keyName);
//turn up
if(keyName=="&" && parseInt(fighter.style.top) > 50){
//alert("up");
top-=10;
fighter.style.top=parseInt(fighter.style.top)-10;
}
//turn down
if(keyName=="(" && parseInt(fighter.style.top) < 470){
//alert("down");
top+=10;
fighter.style.top=parseInt(fighter.style.top)+10;
}
//turn left
if(keyName=="%" && parseInt(fighter.style.left) > 10){
//alert("left");
left-=10;
fighter.style.left=parseInt(fighter.style.left)-10;
}
//turn right
if(keyName=="'" && parseInt(fighter.style.left) < 360){
//alert("right");
left+=10;
fighter.style.left=parseInt(fighter.style.left)+10;
}
//the fighter fire bullet
//if(keyName=="A"){
// alert("bulletFire");
//fireBullet();
//}
}
}
//fire bullet
var top="440";
var left="220";
function fireBullet(){
var bullets;
//alert("fireBullet");
createBullet();
//create bullet
function createBullet(){
bullets=document.createElement("div");
bullets.style.width="5px";
bullets.style.height="5px";
bullets.style.background="red";
//alert("ok");
bullets.style.position="absolute";
bullets.style.top=top;
bullets.style.left=left;
document.body.appendChild(bullets);
//alert(document.body.appendChild(bullets));
}
time = setInterval(function(){
//alert("time");
bullets.style.top=parseInt(bullets.style.top)-10;
if(parseInt(bullets.style.top) < 30){
//alert("none");
bullets.style.display="none";
}
//bullet hit enemy,change image
if(parseInt(bullets.style.top) - parseInt(enemy.style.top) < 50
&& parseInt(bullets.style.left) - parseInt(enemy.style.left) < 30
&& parseInt(bullets.style.top) > parseInt(enemy.style.top)
&& parseInt(bullets.style.left) > parseInt(enemy.style.left)){
//alert("burst");
enemy.style.background="url(hong2.png)";
}
if(parseInt(bullets.style.top) - parseInt(enemy1.style.top) < 50
&& parseInt(bullets.style.left) - parseInt(enemy1.style.left) < 30
&& parseInt(bullets.style.top) > parseInt(enemy1.style.top)
&& parseInt(bullets.style.left) > parseInt(enemy1.style.left)){
//alert("burst");
enemy1.style.background="url(hong2.png)";
}
if(parseInt(bullets.style.top) - parseInt(enemy2.style.top) < 50
&& parseInt(bullets.style.left) - parseInt(enemy2.style.left) < 30
&& parseInt(bullets.style.top) > parseInt(enemy2.style.top)
&& parseInt(bullets.style.left) > parseInt(enemy2.style.left)){
//alert("burst");
enemy2.style.background="url(hong2.png)";
}
if(parseInt(bullets.style.top) - parseInt(enemy3.style.top) < 50
&& parseInt(bullets.style.left) - parseInt(enemy3.style.left) < 30
&& parseInt(bullets.style.top) > parseInt(enemy3.style.top)
&& parseInt(bullets.style.left) > parseInt(enemy3.style.left)){
//alert("burst");
enemy3.style.background="url(hong2.png)";
}
},60);
}
//enemy move
function enemyMove(){
//alert("enemyMove");
var left1=parseInt(Math.random()*3);
var left2=parseInt(Math.random()*3);
times=setInterval(function(){
enemy.style.top=parseInt(enemy.style.top)+4;
enemy1.style.top=parseInt(enemy1.style.top)+3;
enemy2.style.top=parseInt(enemy2.style.top)+3;
enemy3.style.top=parseInt(enemy3.style.top)+2;
//enemy2.style.left=parseInt(enemy2.style.left)+left1;
//enemy3.style.left=parseInt(enemy3.style.left)-left2;
if(parseInt(enemy.style.top) > 450){
//alert("stop");
var num=parseInt(Math.random()*350);
//alert(num);
enemy.style.top=50;
enemy.style.left=num;
enemy.style.background="url(enemy.png)";
}
if(parseInt(enemy1.style.top) > 450){
//alert("stop");
var num=parseInt(Math.random()*350);
//alert(num);
enemy1.style.top=50;
enemy1.style.left=num;
enemy1.style.background="url(enemy.png)";
}
if(parseInt(enemy2.style.top) > 450 || parseInt(enemy2.style.left) > 360){
//alert("stop");
var num=parseInt(Math.random()*320);
//alert(num);
enemy2.style.top=50;
enemy2.style.left=num;
enemy2.style.background="url(enemy.png)";
}
if(parseInt(enemy3.style.top) > 450 || parseInt(enemy3.style.left) < 20){
//alert("stop");
var num=parseInt(Math.random()*320);
//alert(num);
enemy3.style.top=50;
enemy3.style.left=num;
enemy3.style.background="url(enemy.png)";
}
},30);
}
//enemy fired bullet
var ko=0;
function enemyFiredBullet(){
//create enemy bullet
var enemyBullet;
var BulletLeft;
CreateEnemyBullet();
function CreateEnemyBullet(){
enemyBullet=document.createElement("div");
enemyBullet.style.width="5px";
enemyBullet.style.height="5px";
enemyBullet.style.background="yellow";
//alert("ok");
enemyBullet.style.position="absolute";
enemyBullet.style.top="50";
enemyBullet.style.left="150";
document.body.appendChild(enemyBullet);
//alert(document.body.appendChild(enemyBullet));
}
timess = setInterval(function(){
//alert("timess");
enemyBullet.style.top=parseInt(enemyBullet.style.top)+10;
if(parseInt(enemyBullet.style.top) > 500){
//alert("kk");
enemyBullet.style.top=50;
var num=parseInt(Math.random()*350);
enemyBullet.style.left=num;
}
if(parseInt(enemyBullet.style.top) - parseInt(fighter.style.top) < 10 &&
parseInt(enemyBullet.style.left) - parseInt(fighter.style.left) < 10 &&
parseInt(enemyBullet.style.left) > parseInt(fighter.style.left) &&
parseInt(enemyBullet.style.top) < parseInt(fighter.style.top)){
//alert("ko");
ko++;
//alert(ko);
if(ko > 2){
//alert(ko);
for(var i=0;i<500;i++){
clearInterval(timer);
clearInterval(timers);
clearInterval(timess);
clearInterval(time);
}
//change fighter background image
fighter.style.width="80";
fighter.style.height="80";
fighter.style.background="url(over.png)";
alert(myrefresh());
}
}
},60);
}
//refresh page
function myrefresh()
{
alert("game over");
window.location.reload();
}
</script>
</head>
<body>
<button οnclick="start()">start</button>
<!--the size of the game interface-->
<div id="interface" style="width:400px;height:500px;background-image:url(bg.gif);border:solid 1px black">
<!--enemy-->
<div id="enemy" style="width:50px;height:50px;background-image:url(enemy.png);position:absolute;left:200px;top:50px"></div>
<div id="enemy1" style="width:50px;height:50px;background-image:url(enemy.png);position:absolute;left:140px;top:50px"></div>
<div id="enemy2" style="width:50px;height:50px;background-image:url(enemy.png);position:absolute;left:80px;top:50px"></div>
<div id="enemy3" style="width:50px;height:50px;background-image:url(enemy.png);position:absolute;left:260px;top:50px"></div>
<!--fighter-->
<div id="fighter" style="width:50px;height:70px;background-image:url(fight.png);top:450px;left:200px;position:absolute;"></div>
</div>
</body>
</html>