-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
executable file
·68 lines (59 loc) · 1.21 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
window.onload = function()
{
x = 0;
y = 0;
speed = 5;
angle = 0;
mod = 0;
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
car = new Image();
car.src="myimage.png";
window.addEventListener("keydown", keypress_handler, false);
window.addEventListener("keyup", keyup_handler, false);
var move = setInterval(function()
{
draw();
}, 30);
};
function draw()
{
context = canvas.getContext("2d");
context.clearRect(0, 0, 800, 800);
context.fillStyle = "rgb(200, 100, 220)";
context.fillRect(50, 50, 100, 100);
x += (speed*mod) * Math.cos(Math.PI/180 * angle);
y += (speed*mod) * Math.sin(Math.PI/180 * angle);
context.save();
context.translate(x, y);
context.rotate(Math.PI/180 * angle);
context.drawImage(car, -(car.width/2), -(car.height/2));
context.restore();
}
function keyup_handler(event)
{
if(event.keyCode == 87 || event.keyCode == 83)
{
mod = 0;
}
}
function keypress_handler(event)
{
console.log(event.keyCode);
if(event.keyCode == 87)
{
mod = 1;
}
if(event.keyCode == 83)
{
mod = -1;
}
if(event.keyCode == 65)
{
angle -= 5;
}
if(event.keyCode == 68)
{
angle+=5;
}
}