-
Notifications
You must be signed in to change notification settings - Fork 2
/
HTML5 - Beginners - rectsoverlap.html
54 lines (43 loc) · 1021 Bytes
/
HTML5 - Beginners - rectsoverlap.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE = html>
<html><body>
<canvas id="myCanvas" width="640" height="480" style="border:1px solid #d3d3d3;">
Use different browser.
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var player = {
x:10,
y:10,
w:32,
h:32,
};
var enemy = {
x:100,
y:100,
w:100,
h:100,
};
gameloop=setInterval(dogameloop,16);
function dogameloop(){
ctx.clearRect(0,0,c.width,c.height);
ctx.fillStyle="black";
ctx.font="30px Arial";
ctx.fillRect(player.x,player.y,player.w,player.h);
ctx.fillRect(enemy.x,enemy.y,enemy.w,enemy.h);
if (rectsoverlap( player.x,player.y,player.w,player.h,
enemy.x,enemy.y,enemy.w,enemy.h)){
ctx.fillText("Collision",30,30);
}
}
document.onmousemove = function(mouse){
player.x = mouse.clientX;
player.y = mouse.clientY;
}
function rectsoverlap(x1,y1,w1,h1,x2,y2,w2,h2){
return x1 <= x2+w2
&& x2 <= x1+w1
&& y1 <= y2+h2
&& y2 <= y1+h1;
}
</script></body></html>