-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
135 lines (117 loc) · 3.79 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
(function (root) {
var Asteroids = root.Asteroids = (root.Asteroids || {});
var Game = Asteroids.Game = function (ctx, bg) {
this.ctx = ctx;
this.asteroids = [];
this.bullets = [];
this.ship = new Asteroids.Ship(300, 300);
this.loopID = null;
this.bg = bg;
this.state = null;
};
Game.DIM_X = 600;
Game.DIM_Y = 600;
Game.FPS = 33;
Game.NUM_ASTEROIDS = 10;
var addAsteroids = Game.prototype.addAsteroids = function (num) {
for (var i = 0; i < num; i++) {
var randX = Math.random() * Game.DIM_X;
var randY = Math.random() * Game.DIM_Y;
this.asteroids.push(Asteroids.Asteroid.randomAsteroid(randX, randY));
}
};
var draw = Game.prototype.draw = function () {
this.ctx.clearRect(0, 0, Game.DIM_X, Game.DIM_Y);
this.ctx.drawImage(this.bg, 0, 0);
for(var i = 0; i < this.asteroids.length; i++) {
this.asteroids[i].draw(this.ctx);
}
for(var i = 0; i < this.bullets.length; i++) {
this.bullets[i].draw(this.ctx);
}
this.ship.draw(this.ctx);
};
var move = Game.prototype.move = function () {
for(var i = 0; i < this.asteroids.length; i++) {
this.asteroids[i].move();
}
for(var i = 0; i < this.bullets.length; i++) {
this.bullets[i].move();
if (outOfBounds(this.bullets[i])) {
this.bullets.splice(i, 1);
}
}
this.ship.move();
};
var outOfBounds = Game.prototype.outOfBounds = function (obj) {
var posX = obj.posX;
var posY = obj.posY;
return ((posX < 0) || (posY < 0) || (posX > Game.DIM_X) || (posY > Game.DIM_Y));
}
var step = Game.prototype.step = function () {
this.move();
this.draw();
this.checkCollisions();
};
var fire = Game.prototype.fire = function () {
var bullet = this.ship.fire();
if(bullet) {
this.bullets.push(bullet);
}
};
var checkCollisions = Game.prototype.checkCollisions = function () {
if (this.asteroids.length === 0) {
this.state = 'win';
this.stop();
}
for (var i = 0; i < this.asteroids.length; i++) {
if (this.ship.isCollidedWith(this.asteroids[i])) {
this.state = 'lose';
this.stop();
}
for (var j = 0; j < this.bullets.length; j++) {
if (this.bullets[j].isCollidedWith(this.asteroids[i])) {
this.asteroids.splice(i, 1);
this.bullets.splice(j, 1);
}
}
}
};
var bindKeyHandlers = Game.prototype.bindKeyHandlers = function () {
var that = this;
key('up', function() { that.ship.power([0, -1]) });
key('left', function() { that.ship.power([-1, 0]) });
key('right', function () { that.ship.power([1, 0]) });
key('down', function () { that.ship.power([0, 1]) });
key('space', function () { that.fire() });
};
var stop = Game.prototype.stop = function () {
this.ctx.clearRect(0, 0, Game.DIM_X, Game.DIM_Y);
this.ctx.fillStyle ="#7B7DB5";
this.ctx.fillRect(0, 0, Game.DIM_X, Game.DIM_Y);
this.ctx.font = "30px Courier New"
this.ctx.textAlign = "center";
this.ctx.fillStyle = "#fff";
this.ctx.moveTo(0, 0);
if (this.state == 'win'){
this.ctx.fillText("You're awesome!", 300, 300);
} else {
this.ctx.fillText("Game Over", 300, 300);
}
this.showRestart();
window.clearInterval(this.loopID);
};
var start = Game.prototype.start = function () {
this.bindKeyHandlers();
this.addAsteroids(Game.NUM_ASTEROIDS);
this.loopID = window.setInterval(this.step.bind(this), Game.FPS);
};
var showRestart = Game.prototype.showRestart = function () {
var canvas = document.getElementById('content');
var restart = document.createElement('a');
restart.setAttribute("id", "restart");
restart.setAttribute("onClick", "restart()");
restart.innerHTML = 'Restart';
canvas.appendChild(restart);
};
})(this);