I would like to develop a simple 2D game using mouse controls. I am developing this game for a handicapped person that can only use one leg, so this game with be played with feet.
The main purpose of this game is to show healthy people what a struggle it is to use a computer if you are physically challenged. A player has 30 seconds to destroy all squares in the canvas. The squares are all the same size but they move at random speeds. I have managed to do all of this but things are too complicated for me to continue without assistance.
I need some help with mouse over function. I would like it so that when a player hovers over a square with his or her mouse the square disappears. Below the canvas there are two counters; One is counting down the time the player has left and the other one is counting the number of squares left or the amount that have been destroyed. When time runs out or there are no more squares to destroy the game should stop.
I am a rookie in JS so any help would be much appreciated. I have managed to come this far. The code is as it follows....
<html>
<head>
<title>Primer z več objekti</title>
<script type="text/javascript">
var canvas, ctx;
var num_objects = 20;
var objekti = [];
var time = 1000;
function Objekt() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.xvel = Math.random() * 4 - 2.5;
this.yvel = Math.random() * 4 - 2.5;
this.waitTime = Math.random() * 1000;
this.color = 0; // določimo lastnost barve objekta
}
Objekt.prototype.update = function () {
this.x += this.xvel;
this.y += this.yvel;
this.yvel += 0;
if (this.x > canvas.width || this.x < 0) {
this.xvel = -this.xvel;
}
if (this.y > canvas.height || this.y < 0) {
this.yvel = -this.yvel;
}
if (time > this.waitTime) {
this.color = 1;
}
}
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < num_objects; i++) {
objekti[i].update();
if (objekti[i].color == 1) {
ctx.fillStyle = '#0000FF';
} else {
ctx.fillStyle = '#FF0000';
}
ctx.fillRect(objekti[i].x, objekti[i].y, 35, 35);
}
time = time - 1;
document.getElementById("time").value = "Time:" + " " + time;
document.getElementById("num_objects").value = "OBJECTS:" + " " + num_objects;
setTimeout(loop, 1);
}
function load() {
canvas = document.getElementById("cv");
ctx = canvas.getContext("2d");
for (var i = 0; i < num_objects; i++) {
objekti[i] = new Objekt();
}
ctx.lineWidth = "4";
ctx.strokeStyle = "rgb(255,255,255)";
loop();
}
</script>
<style>
body {
background-color:#ACFA58;
margin:50px;
text-align:center;
}
canvas {
border:1px solid #FF0000;
}
</style>
</head>
<body onload="load();">
<canvas id="cv" width = "1000" height="750"> </canvas>
<p> </p>
<form>
<input type = "text" id="time" value = "0" />
<input type = "text" id="num_objects" value = "0" />
</form>
</body>
</html>