|
Extra Tutorial > Create Simple Game (web-based) > Code
Simple Game Code
copy paste the following code to your notepad and save it as .html extension and you'll have your simple game ready to be played.
<html>
<head>
<title>Collect Gold</title>
<script type="text/javascript">
var score = 0;
var t;
var time = 0;
var playing = false;
function updateScore(name) {
if (playing) {
if (document.getElementById(name).innerHTML == "Gold") {
document.getElementById(name).innerHTML = "- -";
score = score + 100;
document.getElementById("score").innerHTML = "Gold: " + score;
var random = Math.floor(Math.random()*9)+1;
document.getElementById("btn" + random).innerHTML = "Gold";
}
}
}
function startGame() {
time = 31;
score = 0;
document.getElementById("score").innerHTML = "Gold: " + score;
clearTimeout(t);
updateTime();
playing = true;
}
function updateTime() {
time = time - 1;
document.getElementById("time").innerHTML = "Time: " + time;
t=setTimeout("updateTime()",1000);
if (time <= 0) {
clearTimeout(t);
playing = false;
alert("Times Up !!");
}
}
</script>
</head>
<body style="color:blue; font-family:times" bgcolor="black">
<center>
<h1><b><i><u>Simple Game - Collect Gold</u></i></b></h1>
<h2 id="time" style="color:red">Time: --</h2>
<h2 id="score" style="color:yellow">Gold:</h2>
<button id="btn1" style="width:100; height:100; font-size:30" onclick="updateScore('btn1')">- -</button>
<button id="btn2" style="width:100; height:100; font-size:30" onclick="updateScore('btn2')">- -</button>
<button id="btn3" style="width:100; height:100; font-size:30" onclick="updateScore('btn3')">- -</button><br />
<button id="btn4" style="width:100; height:100; font-size:30" onclick="updateScore('btn4')">- -</button>
<button id="btn5" style="width:100; height:100; font-size:30" onclick="updateScore('btn5')">Gold</button>
<button id="btn6" style="width:100; height:100; font-size:30" onclick="updateScore('btn6')">- -</button><br />
<button id="btn7" style="width:100; height:100; font-size:30" onclick="updateScore('btn7')">- -</button>
<button id="btn8" style="width:100; height:100; font-size:30" onclick="updateScore('btn8')">- -</button>
<button id="btn9" style="width:100; height:100; font-size:30" onclick="updateScore('btn9')">- -</button><br /><br /><br />
<button style="font-size:30" onclick="startGame()">Start</button>
</center>
</body>
</html>
Other Related Links
Ads Column
Create Money Website
copyright © 2009 - www.createmoneywebsite.com - All Rights Reserved
|