-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
334 lines (277 loc) · 7.9 KB
/
Copy pathscript.js
File metadata and controls
334 lines (277 loc) · 7.9 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const rulesBtn = document.getElementById('rules-btn');
const closeBtn = document.getElementsByClassName('close-btn'); // change
const rules = document.getElementById('rules');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const title = document.getElementById('title');
const infoButton = document.getElementById('rules-btn');
const okButton = document.getElementById('close-btn'); // change
const btnGrup = document.getElementById('btn-grup');
const leftBtn = document.getElementById('leftBtn');
const rightBtn = document.getElementById('rightBtn');
const win = document.getElementById('win');
const scoreId = document.getElementById('scoreId');
const scrnBtnsCheck = document.getElementById('scrnBtnsCheck');
let score = 0;
const winScore = 36;
const brickRowCount = 9; //space for bicks in row
const brickColumnCount = 4; //space for bicks in column
//Random Color for each start
const randHexColor = () => {
let color = '#' + Math.floor(Math.random() * 16777215).toString(16);
color == '#ffffff' ? randHexColor() : color;
return color;
}
const mainColor = randHexColor();
infoButton.style.color = mainColor;
for(let i = 0; i < closeBtn.length; i++){
closeBtn[i].style.color = mainColor;
}
rules.style.color = mainColor;
win.style.color = mainColor;
btnGrup.style.color = mainColor;
document.body.style.backgroundColor = mainColor;
console.log(mainColor);
// Create ball props
const ball = { //TRY EACH
x: canvas.width / 2, //position x
y: canvas.height / 2, //position y
size: 10,
speed: 4,
dx: 4,
dy: -4
};
// Create paddle props
const paddle = { //TRY EACH
x: canvas.width / 2 - 40, //position X
y: canvas.height - 20, //position Y
w: 80,
h: 10,
speed: 8,
dx: 0
};
// Create brick props
const brickInfo = { //TRY EACH
w: 70,
h: 20,
padding: 10,
offsetX: 45,
offsetY: 60,
visible: true
};
// Create bricks
const bricks = []; //array with all bricks
for (let i = 0; i < brickRowCount; i++) {
bricks[i] = []; //created space for columns in rows
for (let j = 0; j < brickColumnCount; j++) {
const x = i * (brickInfo.w + brickInfo.padding) + brickInfo.offsetX;//EDIT
const y = j * (brickInfo.h + brickInfo.padding) + brickInfo.offsetY;//EDIT
bricks[i][j] = { x, y, ...brickInfo }; //EDIT
}
}
// Draw ball on canvas
const drawBall = () => {
ctx.beginPath(); //Begins a path, or resets the current path
ctx.arc(ball.x, ball.y, ball.size, 0, Math.PI * 2); //Creates an arc/curve (used to create circles, or parts of circles)
ctx.fillStyle = mainColor; //Sets or returns the color, gradient, or pattern used to fill the drawing
ctx.fill(); //Fills the current drawing (path)
ctx.closePath(); //Creates a path from the current point back to the starting point
}
// Draw paddle on canvas
const drawPaddle = () => {
ctx.beginPath();
ctx.rect(paddle.x, paddle.y, paddle.w, paddle.h); //Creates a rectangle
ctx.fillStyle = mainColor;
ctx.fill();
ctx.closePath();
}
// Draw score oon canvas
const drawScore = () => {
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, canvas.width / 2 - 30, 30); //Draws "filled" text on the canvas
}
// Draw bricks on canvas
const drawBricks = () => {
bricks.forEach(column => {
column.forEach(brick => {
ctx.beginPath();
ctx.rect(brick.x, brick.y, brick.w, brick.h);
ctx.fillStyle = brick.visible ? mainColor : 'transparent';
ctx.fill();
ctx.closePath();
});
});
}
// Move paddle on canvas
const movePaddle = () => {
paddle.x += paddle.dx;
// Wall detection
if (paddle.x + paddle.w > canvas.width) {
paddle.x = canvas.width - paddle.w;
}
if (paddle.x < 0) {
paddle.x = 0;
}
}
// Move ball on canvas
const moveBall = (play) => {
ball.x += ball.dx;
ball.y += ball.dy;
// Wall collision (right/left)
if (ball.x + ball.size > canvas.width || ball.x - ball.size < 0) {
ball.dx *= -1; // ball.dx = ball.dx * -1
}
// Wall collision (top/bottom)
if (ball.y + ball.size > canvas.height || ball.y - ball.size < 0) {
ball.dy *= -1;
}
// console.log(ball.x, ball.y);
// Paddle collision
if (
ball.x - ball.size > paddle.x &&
ball.x + ball.size < paddle.x + paddle.w &&
ball.y + ball.size > paddle.y
) {
ball.dy = -ball.speed;
}
// Brick collision
bricks.forEach(column => {
column.forEach(brick => {
if (brick.visible) {
if (
ball.x - ball.size > brick.x && // left brick side check
ball.x + ball.size < brick.x + brick.w && // right brick side check
ball.y + ball.size > brick.y && // top brick side check
ball.y - ball.size < brick.y + brick.h // bottom brick side check
) {
ball.dy *= -1;
brick.visible = false;
increaseScore();
}
}
});
});
// Hit bottom wall - Lose
if (ball.y + ball.size > canvas.height) {
showAllBricks();
score = 0;
}
}
// Increase score
const increaseScore = () => {
score++;
if (score % (brickRowCount * brickRowCount) === 0) {
showAllBricks();
}
}
// Make all bricks appear
const showAllBricks = () => {
bricks.forEach(column => {
column.forEach(brick => (brick.visible = true));
});
}
// Draw everything
const draw = () => {
// clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
drawScore();
drawBricks();
}
//If user have piked all bars
const lookForWin = () => {
if(score == winScore){
console.log('win');
win.classList.add('show');
scoreId.innerHTML = "Score: " + score;
score = 0;
title.classList.add('blur');
canvas.classList.add('blur');
infoButton.classList.add('blur');
btnGrup.classList.add('blur');
}
}
// Update canvas drawing and animation
const update = () => {
movePaddle();
moveBall(true);
// Draw everything
draw();
//perform an animation and requests that
//the browser calls a specified function
//to update an animation before the next repaint
requestAnimationFrame(update);
lookForWin();
}
update();
// Keydown event
const keyDown = (e) => {
if (e.key === 'Right' || e.key === 'ArrowRight') {
paddle.dx = paddle.speed;
} else if (e.key === 'Left' || e.key === 'ArrowLeft' || e.name == "Left") {
paddle.dx = -paddle.speed;
}
}
// Keyup event
const keyUp = (e) => {
if (
e.key === 'Right' ||
e.key === 'ArrowRight' ||
e.key === 'Left' ||
e.key === 'ArrowLeft'
) {
paddle.dx = 0;
}
}
const btnDown = (arrow) => {
if (arrow == 'left') {
paddle.dx = -paddle.speed;
} else {
paddle.dx = paddle.speed;
}
}
const btnUp = () => {
paddle.dx = 0;
}
// Keyboard event handlers
document.addEventListener('keydown', keyDown);
document.addEventListener('keyup', keyUp);
document.addEventListener("click", keyDown);
document.addEventListener("click", keyDown);
// Rules and close event handlers
rulesBtn.addEventListener('click', () => {
title.classList.add('blur');
canvas.classList.add('blur');
infoButton.classList.add('blur');
btnGrup.classList.add('blur');
rules.classList.add('show');
});
for(let i = 0; i < closeBtn.length; i++){
closeBtn[i].addEventListener('click', () => {
title.classList.remove('blur');
canvas.classList.remove('blur');
infoButton.classList.remove('blur');
btnGrup.classList.remove('blur');
rules.classList.remove('show');
win.classList.remove('show');
});
}
scrnBtnsCheck.addEventListener('change', (e) =>{
console.log(e.target.checked);
if(e.target.checked == false){
btnGrup.classList.add('display-none');
} else {
btnGrup.classList.remove('display-none');
}
})
document.addEventListener('touchmove', (e)=>{
let touch = e.touches[0];
paddle.x = touch.pageX * 2;
// console.log("Touch x:" + touch.pageX + ", y:" + touch.pageY);
});
// document.addEventListener('touchmove', function(event) {
// event.preventDefault();
// var touch = event.touches[0];
// console.log("Touch x:" + touch.pageX + ", y:" + touch.pageY);
// }, false);