I'm encountering a small issue with my javascript code. I am developing a game for a school project where the objective is to click (remove) fish using a fishing rod. However, the game does not have an end condition set up, so players cannot win. Below you will find the complete HTML and JavaScript code for reference. Additionally, there is a link to a jsbin demonstration of the game with all CSS, HTML, and JavaScript elements combined for easier understanding. Does anyone know how to implement a win condition that stops the game when all fish are clicked (removed) and triggers an alert saying "You Won!"?
Thank you
https://jsbin.com/fihebiwiqi/edit?html,css,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<link rel="stylesheet" type="text/css" href="css1.css">
</head>
<body background="https://guideinparadise.files.wordpress.com/2013/01/down-below2.jpg">
<h1>Fishing Game</h1>
<img id="fishingrod" src ="https://pixabay.com/static/uploads/photo/2014/03/24/17/07/fishing-rod-295096_960_720.png">
<p>Seconds:</p>
<p id="clock"></p>
<form action="html.html">
<input id='level1' type="submit" value="Level 1">
</form>
<form action="html2.html">
<input id='level2' type="submit" value="Level 2">
</form>
<form action="html3.html">
<input id='level3' type="submit" value="Level 3">
</form>
<form action="html4.html">
<input id='level4' type="submit" value="Level 4">
</form>
<form action="html5.html">
<input id='level5' type="submit" value="Bonus Level">
</form>
<script>
document.body.style.cursor = 'none'; // remove cursor
$(document).mousemove(function (e) { //mouse moves over image
$('#fishingrod').offset({
left: e.pageX + -190,
top: e.pageY + -110
});
});
var b = 1;
for (var i= 0; i<5;i++){ // create loop to display 5 fish images
fish(b);
}
function position(element) {
var x = document.body.offsetHeight-element.clientHeight;
var y = document.body.offsetWidth-element.clientWidth;
var Xcoord = Math.floor(Math.random()*1*x + 300);
var Ycoord = Math.floor(Math.random()*y + 50);
return [Xcoord,Ycoord];
}
function fish() {
var img = document.createElement('img');
img.className = 'fish2';
img.setAttribute("style", "position:fixed;");
img.setAttribute("src", "http://res.freestockphotos.biz/pictures/16/16806-illustration-of-a-cartoon-blue-fish-pv.png");
img.setAttribute("width", "200");
document.body.appendChild(img);
var xy = position(img);
img.style.top = xy[0] + 'px';
img.style.left = xy[1] + 'px';
$(img).click(function(){ $(this).remove();});
}
myTimer = setInterval(myCounter, 1000);
var seconds = 0;
function myCounter() {
document.getElementById("clock").innerHTML = ++seconds;
}
</script>
</body>
</html>