-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakout.js
264 lines (215 loc) · 6.92 KB
/
breakout.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
(function () {
var FRAMES_PER_SECOND = 30,
PADDLE_VERTICAL_OFFSET = 40,
BRICK_ROWS = 5,
BRICK_COLUMNS = 10,
SCORE_HORIZONTAL_OFFSET = 12,
SCORE_VERTICAL_OFFSET = 12,
COLOR_BLACK = '#2a363b',
START_VELOCITY = 6.5,
canvasElement,
// 2d canvas context
canvas,
// canvas width
width,
// canvas height
height,
// game loop
loop,
// array of keys for which an element is true if pressed
activeKeys,
// game objects
ball,
paddle,
bricks,
score;
// event listeners ------------------------------------------------------------
document.addEventListener('DOMContentLoaded', function () {
initialize();
}, false);
document.addEventListener('keydown', function (event) {
activeKeys[event.keyCode] = true;
}, false);
document.addEventListener('keyup', function (event) {
activeKeys[event.keyCode] = false;
}, false);
// constructors ---------------------------------------------------------------
function Ball () {
// origin at center of object
this.x = 0;
this.y = 0;
// velocity
this.vx = 6.5;
this.vy = -6.5;
this.radius = 2;
// for convenience
this.width = this.radius * 2;
this.height = this.radius * 2;
this.color = COLOR_BLACK;
this.draw = function (canvas) {
canvas.beginPath();
canvas.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
canvas.fillStyle = this.color;
canvas.fill();
}
this.move = function () {
this.x += this.vx;
this.y += this.vy;
}
}
function Paddle () {
this.x = 0;
this.y = 0;
this.vx = 5;
this.width = 35;
this.height = 4;
this.color = COLOR_BLACK;
this.draw = function (canvas) {
canvas.beginPath();
canvas.rect(this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
canvas.fillStyle = this.color;
canvas.fill();
}
}
function Brick (color) {
this.x = 0;
this.y = 0;
this.width = 40;
this.height = 20;
this.broken = false;
this.color = color;
this.draw = function (canvas) {
if (!this.broken) {
canvas.beginPath();
canvas.rect(this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
canvas.fillStyle = this.color;
canvas.fill();
}
}
}
// other functions ------------------------------------------------------------
function initialize () {
canvasElement = document.getElementsByTagName('canvas')[0];
canvas = canvasElement.getContext('2d');
width = canvasElement.offsetWidth;
height = canvasElement.offsetHeight;
activeKeys = {};
ball = {};
paddle = {};
bricks = [];
score = 0;
// initialize canvas
clear();
ball = new Ball();
paddle = new Paddle();
// position ball
ball.x = width / 2 - ball.radius;
ball.y = height - PADDLE_VERTICAL_OFFSET - ball.radius - paddle.height;
// position paddle
paddle.x = width / 2;
paddle.y = height - PADDLE_VERTICAL_OFFSET;
// create and position bricks
for (var i = 0; i < BRICK_ROWS; i++) {
bricks[i] = [];
for (var j = 0; j < BRICK_COLUMNS; j++) {
var color;
// The color of a brick depends on its row.
switch(i) {
case 0:
color = COLOR_BLACK;
break;
case 1:
color = '#e84a5f';
break;
case 2:
color = '#ff847c';
break;
case 3:
color = '#fece78';
break;
default:
color = '#99b898';
break;
}
brick = new Brick(color);
brick.x = j * brick.width + brick.width / 2;
brick.y = i * brick.height + brick.height / 2;
bricks[i][j] = brick;
}
}
// start game loop and set frame rate to 60 fps
loop = setInterval(function () {
update();
draw();
}, 1000 / FRAMES_PER_SECOND);
}
// update game logic each frame
function update () {
// handle keyboard input
if (activeKeys['37'] && paddle.x - paddle.width / 2 > 0) {
paddle.x -= paddle.vx;
} else if (activeKeys['39'] && paddle.x + paddle.width / 2 < width) {
paddle.x += paddle.vx;
}
// check ball-paddle collision
if (isColliding(ball, paddle)) {
ball.vy *= -1;
}
// check ball-brick collision
for (var i = 0; i < BRICK_ROWS; i++) {
for (var j = 0; j < BRICK_COLUMNS; j++) {
var brick = bricks[i][j];
if (!brick.broken) {
if (isColliding(ball, brick)) {
brick.broken = true;
ball.vy *= -1;
score++;
}
}
}
}
// check edge collisions
if (ball.x - ball.radius < 0 || ball.x + ball.radius > width) {
ball.vx *= -1;
}
if (ball.y - ball.radius < 0) {
ball.vy *= -1
} else if (ball.y + ball.radius > height) {
clearInterval(loop);
initialize();
}
ball.move();
}
// render canvas graphics each frame
function draw () {
clear();
// ball
ball.draw(canvas);
// paddle
paddle.draw(canvas);
// bricks
for (var i = 0; i < BRICK_ROWS; i++) {
for (var j = 0; j < BRICK_COLUMNS; j++) {
if (!bricks[i][j].broken) {
bricks[i][j].draw(canvas);
}
}
}
// score
canvas.font = 'bold 8px "Courier New"';
canvas.fillStyle = COLOR_BLACK;
canvas.fillText(score, SCORE_HORIZONTAL_OFFSET, height - SCORE_VERTICAL_OFFSET);
}
// initialize canvas to background color each frame
function clear () {
canvas.fillStyle = '#eeeeee';
canvas.fillRect(0, 0, width, height);
}
// bounding-box collision detection
function isColliding (a, b) {
return ball.x - a.width / 2 < b.x + b.width / 2 &&
a.x + a.width / 2 > b.x - b.width / 2 &&
a.y - a.height / 2 < b.y + b.height &&
a.y + a.height / 2 > b.y - b.height;
}
})();