-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakout.js
236 lines (207 loc) · 6.22 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
//board
let board;
let boardWidth = 500;
let boardHeight = 500;
let context;
//player paddle
let playerWidth = 85; //500 for testing, 85 normal
let playerHeight = 10;
let playerVelocityX = 10; //move 10 pixels each time
let player = {
x: boardWidth / 2 - playerWidth / 2,
y: boardHeight - playerHeight - 5,
width: playerWidth,
height: playerHeight,
velocityX: playerVelocityX,
};
//ball
let ballWidth = 10;
let ballHeight = 10;
let ballVelocityX = 3; //15 for testing, 3 normal
let ballVelocityY = 2; //10 for testing, 2 normal
let ball = {
x: boardWidth / 2,
y: boardHeight / 2,
width: ballWidth,
height: ballHeight,
velocityX: ballVelocityX,
velocityY: ballVelocityY,
};
//blocks
let blockArray = [];
let blockWidth = 50;
let blockHeight = 10;
let blockColumns = 8;
let blockRows = 3; //add more as game goes on
let blockMaxRows = 10; //limit how many rows
let blockCount = 0;
//starting block corners top left
let blockX = 15;
let blockY = 45;
//score and game over
let score = 0;
let gameOver = false;
window.onload = () => {
board = document.getElementById("board");
board.height = boardHeight;
board.width = boardWidth;
context = board.getContext("2d"); //used for drawing on the board
//draw initial player paddle
context.fillStyle = "skyblue";
context.fillRect(player.x, player.y, player.width, player.height);
requestAnimationFrame(update);
document.addEventListener("keydown", movePlayer);
//create blocks
createBlocks();
};
function update() {
requestAnimationFrame(update);
//stop drawing
if (gameOver) {
return;
}
context.clearRect(0, 0, board.width, board.height);
// player
context.fillStyle = "lightgreen";
context.fillRect(player.x, player.y, player.width, player.height);
// ball
context.fillStyle = "white";
ball.x += ball.velocityX;
ball.y += ball.velocityY;
context.fillRect(ball.x, ball.y, ball.width, ball.height);
//bounce the ball off player paddle
if (topCollision(ball, player) || bottomCollision(ball, player)) {
ball.velocityY *= -1; // flip y direction up or down
} else if (leftCollision(ball, player) || rightCollision(ball, player)) {
ball.velocityX *= -1; // flip x direction left or right
}
if (ball.y <= 0) {
// if ball touches top of canvas
ball.velocityY *= -1; //reverse direction
} else if (ball.x <= 0 || ball.x + ball.width >= boardWidth) {
// if ball touches left or right of canvas
ball.velocityX *= -1; //reverse direction
} else if (ball.y + ball.height >= boardHeight) {
// if ball touches bottom of canvas
context.font = "20px sans-serif";
context.fillText("Game Over: Press 'Space' To Restart", 80, 400);
gameOver = true;
}
//blocks
context.fillStyle = "skyblue";
for (let i = 0; i < blockArray.length; i++) {
let block = blockArray[i];
if (!block.break) {
if (topCollision(ball, block) || bottomCollision(ball, block)) {
block.break = true; // block is broken
ball.velocityY *= -1; // flip y direction up or down
score += 10;
blockCount -= 1;
} else if (leftCollision(ball, block) || rightCollision(ball, block)) {
block.break = true; // block is broken
ball.velocityX *= -1; // flip x direction left or right
score += 10;
blockCount -= 1;
}
context.fillRect(block.x, block.y, block.width, block.height);
}
}
//next level
if (blockCount == 0) {
score += 10 * blockRows * blockColumns; //bonus points :)
blockRows = Math.min(blockRows + 1, blockMaxRows);
createBlocks();
}
//score
context.font = "20px sans-serif";
context.fillText(score, 10, 25);
}
function outOfBounds(xPosition) {
return xPosition < 0 || xPosition + playerWidth > boardWidth;
}
// move player paddle using arrow keys
function movePlayer(e) {
if (gameOver) {
if (e.code == "Space") {
resetGame();
console.log("RESET");
}
return;
}
if (e.code == "ArrowLeft") {
// player.x -= player.velocityX;
let nextplayerX = player.x - player.velocityX;
if (!outOfBounds(nextplayerX)) {
player.x = nextplayerX;
}
} else if (e.code == "ArrowRight") {
let nextplayerX = player.x + player.velocityX;
if (!outOfBounds(nextplayerX)) {
player.x = nextplayerX;
}
// player.x += player.velocityX;
}
}
function detectCollision(a, b) {
return (
a.x < b.x + b.width && //a's top left corner doesn't reach b's top right corner
a.x + a.width > b.x && //a's top right corner passes b's top left corner
a.y < b.y + b.height && //a's top left corner doesn't reach b's bottom left corner
a.y + a.height > b.y
); //a's bottom left corner passes b's top left corner
}
function topCollision(ball, block) {
//a is above b (ball is above block)
return detectCollision(ball, block) && ball.y + ball.height >= block.y;
}
function bottomCollision(ball, block) {
//a is above b (ball is below block)
return detectCollision(ball, block) && block.y + block.height >= ball.y;
}
function leftCollision(ball, block) {
//a is left of b (ball is left of block)
return detectCollision(ball, block) && ball.x + ball.width >= block.x;
}
function rightCollision(ball, block) {
//a is right of b (ball is right of block)
return detectCollision(ball, block) && block.x + block.width >= ball.x;
}
function createBlocks() {
blockArray = []; //clear blockArray
for (let c = 0; c < blockColumns; c++) {
for (let r = 0; r < blockRows; r++) {
let block = {
x: blockX + c * blockWidth + c * 10, //c*10 space 10 pixels apart columns
y: blockY + r * blockHeight + r * 10, //r*10 space 10 pixels apart rows
width: blockWidth,
height: blockHeight,
break: false,
};
blockArray.push(block);
}
}
blockCount = blockArray.length;
}
//reset game if ball touches bottom of the canvas
function resetGame() {
gameOver = false;
player = {
x: boardWidth / 2 - playerWidth / 2,
y: boardHeight - playerHeight - 5,
width: playerWidth,
height: playerHeight,
velocityX: playerVelocityX,
};
ball = {
x: boardWidth / 2,
y: boardHeight / 2,
width: ballWidth,
height: ballHeight,
velocityX: ballVelocityX,
velocityY: ballVelocityY,
};
blockArray = [];
blockRows = 3;
score = 0;
createBlocks();
}