A Simple Game in Javascript

                                     A Simple Game in Javascript 

A week ago i thought about to take a revision of my Javascript skills ,so i planned to make some useful applications in Javascript.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
<html>
<title>Rocket Game</title>
<head>
<style type="text/css">
html{}
img#obstacle{position: absolute;margin-left: 0px;margin-top: 0px;}
</style>
</head>
<body id="body" style="opacity:0" onload="show()">
<img id="img" src="http://s21.postimg.org/8xv7zmscz/image.gif" alt='rocket' style="position:absolute;left:0;top:0"/>
</body>
<script type="application/javascript">
throwballs();
var obs= document.getElementById("obstacle");
obs.style.left=window.innerWidth+"px";
var body = document.getElementById("body");
body.addEventListener("keydown",moverocket);
function show()
{
var body = document.getElementById("body");
body.style.opacity= 1;
}
function moverocket(evt){
var move;
var left;
var top;
var right;
var bottom;
if(evt.keyCode==39)
{
//right arrow
move=document.getElementById("img");
left= parseInt(/[0-9]+/.exec(move.style.left));
if(window.innerWidth>left)
{
left+=4;
move.style.left=left+"px";
}
}
else if(evt.keyCode==40)
{
//arrow down
move=document.getElementById("img");
top= parseInt(/[0-9]+/.exec(move.style.top));
if(window.innerHeight>top)
{
top+=4;
move.style.top=top+"px";
}
}
else if(evt.keyCode==37)
{
//left arrow
move=document.getElementById("img");
right= parseInt(/[0-9]+/.exec(move.style.left));
right-=4;
move.style.left=right+"px";
}
else if(evt.keyCode==38)
{
//up arrow
move=document.getElementById("img");
top= parseInt(/[0-9]+/.exec(move.style.top));
top-=4;
move.style.top=top+"px";
}
}
function throwballs(){
var img = new Image;
img.src="http://s13.postimg.org/bzm5kkskz/obst1.gif";
img.alt="Keep Distance";
img.id ="obstacle";
document.body.appendChild(img);
}
function moveball(ball)
{
var r;
var rk=document.getElementById("img");
var obs=document.getElementById("obstacle");
right= parseInt(/[-0-9]+/.exec(ball.style.left));
var rkl= parseInt(/[-0-9]+/.exec(rk.style.left))+100;
var obsl=parseInt(/[-0-9]+/.exec(obs.style.left));
if(right>0)
{
if(rkl==obsl)
{
var rkls= parseInt(/[-0-9]+/.exec(rk.style.top));
var obsls=parseInt(/[-0-9]+/.exec(obs.style.top));
if((obsls>(rkls-80))&&(obsls<(rkls+35)))
alert("Game Over");
}
right=right-2;
ball.style.left=right+"px";
}
else
{
ball.style.top= parseInt(300*Math.random());
ball.style.left=1200+"px";
}
}
setInterval(function(){moveball(document.getElementById("obstacle"))},10);
</script></html>


In the next blog i will  discuss about , how we can make it multiuser using PHP and Mysql Database :)  
           
See Live Example Here

Comments

Popular posts from this blog

ciphers continued--Vegerne Cipher and its application in php

Message Sending API (Unofficial) for Way2SMS [ PHP ]

Working with Frameworks of PHP- The MVC structure