-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart.js
273 lines (250 loc) · 6.71 KB
/
part.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
264
265
266
267
268
269
270
271
272
//selecting canvas element
const canvas = document.getElementById('canva');
//we are passing tons of methods which we can actually use to draw within our canvas
const c = canvas.getContext("2d");
// defining bricks
const brick = {
row: 3,
column: 5,
width: 55,
height: 20,
offSetLeft: 20,
offSetTop: 20,
marginTop: 30,
fillColor: "brown",
strokeColor: "black",
};
let bricks = [];
function createBricks() {
for (let r = 0; r < brick.row; r++) {
bricks[r] = [];
for (let d = 0; d < brick.column; d++) {
bricks[r][d] = {
x: d * (brick.offSetLeft + brick.width) + brick.offSetLeft,
y: r * (brick.offSetTop + brick.height) + brick.offSetTop + brick.marginTop,
//when the brick is not broken
status: true,
};
}
}
}
createBricks();
function drawBricks() {
for (let r = 0; r < brick.row; r++) {
for (let d = 0; d < brick.column; d++) {
let b = bricks[r][d];
if (b.status) {
c.fillStyle = brick.fillColor;
c.fillRect(b.x, b.y, brick.width, brick.height);
c.strokeStyle = brick.strokeColor;
c.strokeRect(b.x, b.y, brick.width, brick.height);
}
}
}
}
//creating game variable and constants
const paddlewidth = 150;
const paddleheight = 40;
const hbottom = 20;
let rightarrow = false;
let leftarrow = false;
//using line width
c.lineWidth = 3;
//creating paddle and ball
let paddle = {
x: canvas.width / 2 - paddlewidth / 2,
y: canvas.height - paddleheight - hbottom,
width: paddlewidth,
height: paddleheight,
dx: 4,
}
function creatingpaddle() {
c.fillStyle = "black";
c.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
c.strokeStyle = "yellow";
c.strokeRect(paddle.x, paddle.y, paddle.width, paddle.height);
}
document.addEventListener("keydown", function (event) {
if (event.keyCode == 37) {
leftarrow = true;
} else if (event.keyCode == 39) {
rightarrow = true;
}
});
document.addEventListener("keyup", function (event) {
//37 is keycode for leftarrow and 39 is keycode for rightarrow
if (event.keyCode == 37) {
leftarrow = false;
} else if (event.keyCode == 39) {
rightarrow = false;
}
});
//moving of paddle
function movepaddle() {
if (rightarrow && paddle.x + paddle.width < canvas.width) {
paddle.x += paddle.dx;
}
else if (leftarrow && paddle.x > 0) {
paddle.x -= paddle.dx;
}
}
//intializing ball radius
const ballradius = 20;
let ball = {
x: canvas.width / 2,
y: paddle.y - ballradius,
radius: ballradius,
speed: 4,
dx: 3,
dy: -3,
}
//drawing ball
function creatingball() {
c.beginPath();
c.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
c.fillStyle = "yellow";
c.fill();
//to draw this path we need to use stroke method
c.strokeStyle = "black";
c.stroke();
c.closePath();
}
//movement of ball
function moveball() {
ball.x += ball.dx;
ball.y += ball.dy;
}
//moving the ball
function ballwallcollide() {
if (ball.x + ball.radius > canvas.width || ball.x - ball.radius < 0) {
ball.dx = -ball.dx;
}
if (ball.y - ball.radius < 0) {
ball.dy = -ball.dy;
}
if (ball.y + ball.radius > canvas.height) {
liffe--;
resetball();
}
}
//when ball reaches bottom create a new ball
function resetball() {
ball.x = canvas.width / 2;
ball.y = paddle.y - ballradius;
ball.dx = 3;
ball.dy = -3;
}
//ball and paddle collision
function ballpaddlecollision() {
if (ball.x < paddle.x + paddle.width && ball.x > paddle.x && paddle.y < paddle.y + paddleheight && ball.y > paddle.y) {
collide.play();
// setTimeout(()=>{
// audio2.play();
// },10);
ball.dx = -ball.dx;
ball.dy = -ball.dy;
}
}
let score = 0;
//brick and ball collision
function ballbrickcollision() {
for (let i = 0; i < brick.row; i++) {
for (let j = 0; j < brick.column; j++) {
let b = bricks[i][j];
if (b.status) {
if (ball.x + ball.radius > b.x && ball.x - ball.radius < b.x + brick.width && ball.y + ball.radius > b.y && ball.y - ball.radius < b.y + brick.height) {
// setTimeout(()=>{
// audio3.play();
// },100);
ball.dy = -ball.dy;
b.status = false;//the brick is broken
score++;
}
}
}
}
}
function wonn(){
let allbroken = true;
// check if all the bricks are broken
for (let i = 0; i < brick.row; i++) {
for (let j = 0; j < brick.column; j++) {
allbroken = allbroken && !bricks[i][j].status;
}
}
if (allbroken) {
c.fillStyle = "black";
c.font = "30px Arial"
c.fillText("youwin", canvas.width / 2-30, canvas.height / 2);
gameover=true;
// over.play();
// setTimeout(()=>{
// audio1.play();
// },100);
}
}
//displaying score
function drawscore() {
c.font = '20px Arial';
c.fillStyle = "yellow";
c.fillText("score: " + score, 9, 20);
}
//life left for the player
let liffe = 1;
function gameliffe() {
c.fillStyle = "yellow";
c.font = "20px Arial";
c.fillText("life : " + liffe, canvas.width / 2 + 90, 20);
}
let gameover = false;
// when is life becomes game is over
function over() {
if (liffe <= 0) {
c.fillStyle = "black";
c.font = "30px Arial"
c.fillText("youlost", canvas.width / 2-30, canvas.height / 2);
gameover = true;
hit.play();
// setTimeout(()=>{
// audio1.play();
// },100);
}
}
function update(){
movepaddle();
moveball();
ballwallcollide();
ballpaddlecollision();
ballbrickcollision();
over();
wonn();
}
function draw() {
creatingpaddle();
creatingball();
drawBricks();
drawscore();
gameliffe();
}
function animate() {
c.clearRect(0, 0, canvas.width, canvas.height);
draw();
update();
if (!gameover) {
//using request animation frame to create a loop.when we call this animate function it is going to create loop for us.
requestAnimationFrame(animate);
}
}
animate();
//when user clicks on start again
// const restart = document.getElementById('restart');
// restart.addEventListener("click", function(){
// location.reload(); //load the page
// })
//for adding audio
const hit=new Audio();
hit.src="img/sound1.mp3";
//const over=new Audio();
// over.src="img/sound2.mp3";
const collide=new Audio();
collide.src="img/sound3.mp3";