-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
135 lines (122 loc) · 3.31 KB
/
game.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var app = {};
function startGame() {
app.canvas = document.getElementById("canvas");
app.context = app.canvas.getContext("2d");
app.shipImage = new Image();
app.shipImage.src = "images/ship.png";
app.rockImage = new Image();
app.rockImage.src = "images/rock.png";
app.explodedImage = new Image();
app.explodedImage.src = "images/exploded.png";
spawnHero();
spawnRocks();
app.canvas.addEventListener("mousemove", onMouseMove, false);
app.startTime = performance.now();
window.requestAnimationFrame(nextGameStep);
}
function spawnHero() {
app.hero = {
position: {x: 400, y: 400},
size: 60,
image: app.shipImage,
};
}
function spawnRocks() {
app.rocks = [];
for (var i = 0; i < 10; i++) {
spawnRock();
}
}
function spawnRock() {
var rock = {
position: {
x: Math.random() * app.canvas.width,
y: Math.random() * -app.canvas.height,
},
size: 120,
speed: 3,
image: app.rockImage,
angle: Math.random() * 2 * Math.PI,
rotateSpeed: Math.random() * 2 * Math.PI / 150,
nextRockStep: function() {
this.move();
this.angle += this.rotateSpeed;
this.respawnRock();
this.didRockHitHero();
},
move: function() {
this.position.y += this.speed;
},
respawnRock: function() {
if (this.position.y > app.canvas.height + this.size) {
this.position = {
x: Math.random() * app.canvas.width,
y: Math.random() * -app.canvas.height,
}
if (app.hero.state !== "exploded") {
this.speed += 1;
}
}
},
didRockHitHero: function(){
if (getDistance(app.hero, this) < 50) {
app.hero.image = app.explodedImage;
app.hero.state = "exploded";
}
}
};
app.rocks.push(rock);
}
function onMouseMove(event) {
if (app.hero.state !== "exploded") {
app.hero.position.x = event.pageX;
app.hero.position.y = event.pageY;
}
}
function drawObject(object) {
var context = app.context;
context.save();
context.translate(object.position.x, object.position.y);
context.rotate(object.angle);
context.drawImage(object.image, -object.size/2, -object.size/2, object.size, object.size);
context.restore();
}
function drawBackground() {
app.context.fillStyle = "black";
app.context.fillRect(0, 0, app.canvas.width, app.canvas.height);
}
function nextGameStep(timestamp) {
window.requestAnimationFrame(nextGameStep);
if (app.hero.state !== "exploded") {
app.score = Math.floor((timestamp - app.startTime) / 1000);
}
app.rocks.forEach(function(rock) {
rock.nextRockStep();
});
drawFrame();
}
function getDistance(object1, object2) {
var dx = object1.position.x - object2.position.x;
var dy = object1.position.y - object2.position.y;
return Math.sqrt(dx * dx + dy * dy);
}
function drawFrame() {
drawBackground();
drawObject(app.hero);
app.rocks.forEach(function(rock) {
drawObject(rock);
});
drawScore();
}
function drawScore() {
var context = app.context;
context.textAlign = "center";
context.fillStyle = "yellow";
if (app.hero.state === "exploded") {
context.font = "italic 125px Calibri";
context.fillText("Score " + app.score, app.canvas.width/2, app.canvas.height/2);
} else {
context.font = "italic 30px Calibri";
context.fillText("Score " + app.score, app.canvas.width/2, 40);
}
}