-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
31 lines (25 loc) · 863 Bytes
/
main.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
const canvas = document.getElementById("myCanvas");
canvas.width = 200;
const ctx = canvas.getContext("2d");
const road = new Road(canvas.width/2,canvas.width*0.9);
const car = new Car(road.getLaneCenter(1),100,30,50,"AI");
const traffic = [
new Car(road.getLaneCenter(1),-100,30,50,"DUMMY",2)
];
animate();
function animate(){
for(let i=0; i<traffic.length; i++) {
traffic[i].update(road.borders,[]); // empty aray instead of traffic aray or else the traffic would interract and have collisions with itself
}
car.update(road.borders,traffic);
canvas.height = window.innerHeight;
ctx.save();
ctx.translate(0,-car.y+canvas.height*0.7);
road.draw(ctx);
for(let i=0; i<traffic.length; i++) {
traffic[i].draw(ctx,"red");
}
car.draw(ctx,"blue");
ctx.restore();
requestAnimationFrame(animate);
}