-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.js
52 lines (37 loc) · 752 Bytes
/
box.js
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
// Snakes Game
//Game Loop - Init, Draw, Update,
function init(){
// console.log("Init");
canvas = document.getElementById('mycanvas');
pen = canvas.getContext('2d');
W = canvas.width;
H = canvas.height;
box = {
x:10,
y:20,
w:20,
h:20,
speed:5,
}
}
function draw(){
pen.clearRect(0,0,W,H);
console.log("Draw");
pen.fillStyle = "green";
pen.fillRect(box.x,box.y,box.w,box.h);
}
function update(){
console.log("Update");
box.x += box.speed;
box.y += 3;
if(box.x>=W){
box.speed *= -1;
}
}
function gameLoop(){
draw();
update();
}
init();
//Call Game Looper after t time
setInterval(gameLoop,100);