<body>
<div id="content">
<h1>Whack-A-Mole</h1>
<p>After clicking "start", you will have 30 seconds to click
as many moles as you can. The moles appear randomly so be ready! </p>
<div id="controls">
<span id="score">0 pts</span>
<button type="button" id="start_button">Start!</button>
</div>
<div id="timer">30 seconds left</div>
<div id="gamespace">
</div>
</div>
</body>
JavaScript
<script>
var score=0;
var time_left=30;
var clock;
var t;
var xPos;
var yPos;
$(document).ready(function(){
$("#start_button").click(function(){
start();
updateTimer();
});//end start_button
$("#gamespace").on("click", "img", function(){
incScore();
});//end.on
});//end.ready
function start(){
addMole();
$("h1").css("color","#005b7f");
$("#gamespace").css("backgroundColor","#cdeaf5");
};//end start
function randPosX(){
return Math.floor(Math.random()*300);
};//end x
function randPosY(){
return Math.floor(Math.random()*600);
};//end y
function addMole(){
$("#gamespace").append('<img src="img/mole.png" style="top:'+xPos+'px; left:'+yPos+'px;" />');
randPosX();
randPosY();
t=setTimeout("addMole()", 2000);
};//end mole
function updateTimer(){
$("#timer").show();
if (time_left >0){
$("#timer").html(time_left+" seconds left");
clock=setTimeout("updateTimer()", 1000);
time_left -=1;
}else{
$("#timer").html("Out of time!")
clearTimeout();
}
};//end timer
function incScore(){
score +=1;
$("#score").html(score+" points");
};//end score
</script>
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.