Javascript is one language that gives coders a lot of freedom within a web-browser. I hope everyone agrees with me about the prev statement. It is the best choice (perhaps the second best only after flash) to develop simple games in a browser...I always wanted to try some web games using javascript...Here is my first one.
This one is a simple word game...Letters (a to z) fall from the top of the browser's client area towards the bottom at some fixed speed... The player has to type the correct letter (one of those which are currently visible on the screen)....a correct hit on the keyboard earns a point for the player....but a wrong hit costs him a point (negetive point)......The number of alphabets that are displayed at an instant is specified as a variable in the code....Here is the code..
IMPORTANT : This Code works only in Interner Explorer 6.0 or higher.
A code that works in all browsers will be updated soon.
------------------------------------wordgame.js----------------------------------
//This array contains the alphabets that are currently displayed on the screen
var curletters = new Array();
//speed of falling alphabets
var level = 5;
var intr = new Array();
var interval;
var j = 0;
//number of alphabets displayed at an instant on the screen
var nol = 5;
var score = 0;
function body_onload(){
//alert(randomletter());
}
function randomletter(){
var randno = Math.round(Math.random() * 26);
var letter = String.fromCharCode(randno + 97);
return letter;
}
function createletter(i){
var letter = randomletter();
var div = document.createElement("div");
div.id = "div" + i;
div.innerHTML = letter;
curletters[i] = letter;
div.style.position = "absolute";
div.style.top = -5;
div.style.left = randomx();
document.body.appendChild(div);
intr[i] = setInterval("moveletter(" + i + ")",(level * 10));
}
function randomx(){
var x = document.documentElement.clientWidth;
var rand = Math.random();
var xpos = Math.round(rand * x);
return xpos;
}
function moveletter(i){
var top = document.getElementById("div" + i).style.top;
top = parseInt(top.substring(0,top.indexOf("px")));
top = top + 5;
var max = document.documentElement.clientHeight;
//alert(max);
//max = parseInt(max.substring(0,max.indexOf("px")));
document.getElementById("div" + i).style.top = top;
if(top > max){
clearInterval(intr[i]);
document.body.removeChild(document.getElementById("div" + i));
createletter(i);
}
}
function Button1_onclick() {
interval = setInterval("start()",1000);
}
function start(){
//alert(j);
j = j + 1;
if(j >= nol){
clearInterval(interval);
}
createletter(j - 1);
}
function test(){
var num;
var key = event.keyCode;
for (num=0;num < nol; num++)
{
if (key == curletters[num].charCodeAt(0))
{
if (document.getElementById("div" + num).innerHTML == "")
{
continue;
}
deleteletter(num);
score += 1;
document.getElementById("Text1").value = score;
break;
}
if (num == (nol-1))
{
score -= 1;
document.getElementById("Text1").value = score;
}
}
}
function deleteletter(num){
document.getElementById("div" + num).innerHTML = "";
}
----------------------------------------------------------------------------------------------
Click here to play the game in ur browser
OR
Click Here to download the javascript (.js) code file
I hope you like my game......
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment