-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship2.js
More file actions
102 lines (88 loc) · 2.06 KB
/
ship2.js
File metadata and controls
102 lines (88 loc) · 2.06 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
window.onload = function () {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = (canvas.width = window.innerWidth),
height = (canvas.height = window.innerHeight),
ship = particle.create(width / 2, height / 2, 0, 0),
thrust = vector.create(0, 0),
angle = 0,
turningLeft = false,
turningRight = false,
thrusting = false;
ship.friction = 1;
document.body.addEventListener("keydown", function (event) {
// console.log(event.keyCode);
switch (event.keyCode) {
case 38: // up
thrusting = true;
break;
case 37: // left
turningLeft = true;
break;
case 39: // right
turningRight = true;
default:
break;
}
});
document.body.addEventListener("keyup", function (event) {
// console.log(event.keyCode);
switch (event.keyCode) {
case 38: // up
thrusting = false;
break;
case 37: // left
turningLeft = false;
break;
case 39: // right
turningRight = false;
default:
break;
}
});
update();
function update() {
// context.clearRect(0, 0, width, height);
if (turningRight) {
angle += 0.05;
}
if (turningLeft) {
angle -= 0.05;
}
if (thrusting) {
thrust.setLength(0.1);
} else {
thrust.setLength(0);
}
thrust.setAngle(angle);
ship.accelerate(thrust.getX(), thrust.getY());
ship.update();
if (ship.x > width) {
ship.x=(0);
}
if (ship.x < 0) {
ship.x=(width);
}
if (ship.y > height) {
ship.y=(0);
}
if (ship.y < 0) {
ship.y=(height);
}
context.save();
context.translate(ship.x, ship.y);
context.rotate(angle);
context.beginPath();
context.moveTo(10, 0);
context.lineTo(-10, -7);
context.lineTo(-10, 7);
context.lineTo(10, 0);
if (thrusting) {
context.moveTo(-10, 0);
context.lineTo(-18, 0);
}
context.stroke();
context.restore();
requestAnimationFrame(update);
}
};