-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
270 lines (217 loc) · 6.33 KB
/
main.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
const showRules=document.getElementById("rule-btn");
const menu=document.getElementById("rules");
const canvas=document.getElementById('canvas');
const ctx=canvas.getContext('2d');
const close=document.getElementById("close-btn");
let score=0;
let rows=9;
let columns=5;
let allBricks=[]
showRules.addEventListener('click',slideItOver)
close.addEventListener('click',slideItOver)
function slideItOver() {
menu.classList.toggle('active')
}
//creating ball properties
const ball={
x:canvas.width/2,
y:canvas.height/2,
dx:4,
dy:-4,
size:10
}
//draw the ball on canvas
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x,ball.y,ball.size,0,2*Math.PI)
ctx.fillStyle='#0095dd'
ctx.fill();
ctx.closePath();
}
//create paddle properties
const paddle={
x:canvas.width/2 - 40,
y:canvas.height - 20,
w:80,
h:10,
speed:20,
dx:0 //why zero?
}
//create bricks properties
const brick={
w:70,
h:20,
visible:true,
padding:10,
startingX:45,
startingY:50
}
//add bricks to the whole array
for (let i=0;i<rows;i++){
for (let j=0;j<columns;j++){
var singleBrick={
w:brick.w,
h:brick.h,
x:brick.startingX + (i*80),
y:brick.startingY + (j*30),
visible:true,
padding:10
}
allBricks.push(singleBrick)
}
}
//draw paddle on canvas
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddle.x,paddle.y,paddle.w,paddle.h);
ctx.fillStyle='#0095dd';
ctx.fill();
ctx.closePath();
}
function drawBricks() {
allBricks.forEach(item=>{
if (item.visible){
ctx.beginPath()
ctx.rect(item.x,item.y,item.w,item.h);
ctx.fillStyle='#0095dd';
ctx.fill();
ctx.closePath()
}
})
}
function writeScore() {
ctx.font='20px Yekan';
ctx.fillText(`امتیاز: ${ConvertToPersian(score)}`,canvas.width-100,30)
}
function moveBall() {
ball.x += ball.dx;
ball.y += ball.dy;
ball.y<2 ? console.log(ball.y) : false
//ball bounce when it hits the wall (top/bottom)
if (ball.y < 0 || ball.y > canvas.height){
ball.dy = -(ball.dy)
}
//ball bounce when it hits the wall (left/right)
if (ball.x < 0 || ball.x > canvas.width){
ball.dx = -(ball.dx)
}
//ball when it hits the paddle
const ballOnPaddle=ball.x + ball.size<paddle.x + paddle.w && ball.x - ball.size>paddle.x
&& ball.y +ball.size > 480
if (ballOnPaddle){
ball.dy = -(ball.dy);
}
//ball when it hits a brick
for (let i=0;i<allBricks.length;i++){
allBricks.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
) {
score++
if (score==rows*columns){
document.body.innerHTML=`
<div id='won' class='won'>
<h3>بازی رو بردید</h3>
<button onclick="reload()" class="play-again">دوباره</button>
</div>
`
canvas.remove();
ball.remove()
//ctx.remove();
}
brick.visible=false;
ball.dy= -(ball.dy)
}
}
})
}
/*allBricks.forEach(brick=>{
})*/
//ball when it doesn't hit the paddle (user loses)
if (ball.size+ball.y > canvas.height){
document.body.innerHTML=`
<div id='lost' class='lost'>
<h3>بازی رو باختید</h3>
<p>امتیاز شما:${ConvertToPersian(score)} </p>
<button onclick="reload()" class="play-again">دوباره</button>
</div>
`
score=0;
ball.remove();
canvas.remove();
}
}
console.log(allBricks)
update();
function update() {
draw();
moveBall();
requestAnimationFrame(update);
}
function draw() {
//first clear the canvas
ctx.clearRect(0,0,800,500)
drawBall();
drawPaddle();
writeScore();
drawBricks();
}
function showAllBricks() {
for (let i=0;i<rows*columns;i++){
allBricks[i].visible=true;
}
}
//the paddle moves (the speed will be added to coordinate) when right or left key is being pressed
function keydown(e) {
if(e.keyCode==39){
if (paddle.x == canvas.width - paddle.w){
paddle.x=canvas.width - paddle.w;
} else {
paddle.x += paddle.speed;
draw();
}
} else if (e.keyCode==37){
if (paddle.x == 0){
paddle.x=0;
} else {
paddle.x -= paddle.speed;
draw();
}
}
}
document.addEventListener('keydown',keydown);
//document.addEventListener('keyup',keyup);
//a function to convert english numbers to persian
function ConvertToPersian(number){
const persian = { 0: '۰', 1: '۱', 2: '۲', 3: '۳', 4: '۴', 5: '۵', 6: '۶', 7: '۷', 8: '۸', 9: '۹' };
let PersianNumber='';
const numberAsText=number.toString();
for (let i=0;i<numberAsText.length;i++){
PersianNumber+=persian[+numberAsText[i]];
}
return PersianNumber
}
/*function showWonMsg() {
document.body.innerHTML=`
<div id='won' class='won'>
<h3>بازی رو بردید</h3>
<button onclick="reload()" class="play-again">دوباره</button>
</div>
`
}
function showLostMsg() {
document.body.innerHTML=`
<div id='lost' class='lost'>
<h3>بازی رو باختید</h3>
<button onclick="reload()" class="play-again">دوباره</button>
</div>
`
}*/
function reload(){
window.location.reload();
}
draw();