-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreak.js
268 lines (242 loc) · 6.67 KB
/
break.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
const grid= document.querySelector(".grid");
const blockWidth= 10;
const blockHeight= 2
const boardWidth=56;
const boardHeight=30
const ballDiameter= 1.5
const scoreDisplay= document.querySelector("#score");
let xDirection= 0.35;
let yDirection= 0.35;
// was originally 2
let score=0;
let timerId;
const userStart= [23,1];
let currentPosition= userStart;
const ballStart=[27.25,4];
let currentBallPosition= ballStart;
class Block
{
constructor(xAxis, yAxis)
{
this.bottomLeft= [xAxis, yAxis];
this.bottomRight=[xAxis+ blockWidth, yAxis];
this.topLeft= [xAxis, yAxis+ blockHeight];
this.topRight= [xAxis+blockWidth, yAxis+ blockHeight];
}
}
// all of our blocks are stored in an array;
const blocks= [
new Block(1,27),
new Block(12,27),
new Block(23,27),
new Block(34,27),
new Block(45,27),
new Block(1,24),
new Block(12,24),
new Block(23,24),
new Block(34,24),
new Block(45,24),
new Block(1,21),
new Block(12,21),
new Block(23,21),
new Block(34,21),
new Block(45,21),
new Block(1,18),
new Block(12,18),
new Block(23,18),
new Block(34,18),
new Block(45,18),
new Block(1,15),
new Block(12,15),
new Block(23,15),
new Block(34,15),
new Block(45,15),
]
function randomXgenerator()
{
let randomX= (Math.random()*9+1-5)/10;
xDirection= randomX;
console.log(randomX)
}
randomXgenerator()
function randomYgenerator()
{
let randomY= (Math.random()*5+1)/10;
yDirection= randomY;
}
function addBlock()
{
for(let i=0; i<blocks.length; i++){
const block= document.createElement("div");
block.classList.add("block")
block.style.left= blocks[i].bottomLeft[0]+ "vw";
block.style.bottom= blocks[i].bottomLeft[1]+ "vw";
if(i%2==0)
{
block.style.backgroundColor="rgb(78, 202, 192)";
}
else{
block.style.backgroundColor="orange";
}
grid.appendChild(block);
}
}
addBlock();
const user= document.createElement("div");
user.classList.add("user");
drawUser()
function drawUser()
{
user.style.left= currentPosition[0]+ "vw";
user.style.bottom= currentPosition[1]+ "vw";
}
function drawBall()
{
ball.style.left= currentBallPosition[0]+ "vw";
ball.style.bottom= currentBallPosition[1]+ "vw";
}
grid.appendChild(user);
function moveUser(e)
{
switch(e.key)
// this means it's an event under the key subtype;
{
case "ArrowLeft":
if(currentPosition[0]>0)
{
currentPosition[0]-=1.5;
drawUser();
}
break;
case "ArrowRight":
if(currentPosition[0]<46)
{
currentPosition[0]+=1.5;
drawUser();
}
break;
case "a":
if(currentPosition[0]>0)
{
currentPosition[0]-=1.5;
drawUser();
}
break;
case "d":
if(currentPosition[0]<46)
{
currentPosition[0]+=1.5;
drawUser();
}
break;
}
}
//createBall
const ball= document.createElement("div");
ball.classList.add("ball");
drawBall();
grid.appendChild(ball);
// move ball
function moveBall()
{
currentBallPosition[0]+=xDirection;
currentBallPosition[1]+=yDirection;
drawBall();
checkForCollisions();
}
function checkForCollisions()
{
// check for block collisions:
for(let i=0; i<blocks.length; i++)
{
if(
(currentBallPosition[0]> blocks[i].bottomLeft[0] && currentBallPosition[0]< blocks[i].bottomRight[0]) &&
((currentBallPosition[1]+ ballDiameter)> blocks[i].bottomLeft[1] && currentBallPosition[1]<blocks[i].topLeft[1])
)
{
const allBlocks= Array.from(document.querySelectorAll(".block"));
// the Array.from method converts iterable objects into an array;
//console.log(allBlocks);
allBlocks[i].classList.remove("block");
blocks.splice(i,1);
// the list.splice(index removed, # of removing values, what to insert)
changeDirection();
score++;
scoreDisplay.innerHTML= score;
// check for win
if(blocks.length==0)
{
scoreDisplay.innerHTML= "You win!";
clearInterval(timerId);
document.removeEventListener('keydown',moveUser);
}
}
}
// check for user collision:
if(
(currentBallPosition[0]> currentPosition[0] && currentBallPosition[0] < currentPosition[0]+ blockWidth) &&
(currentBallPosition[1]> currentPosition[1] && currentBallPosition[1]< currentPosition[1]+ blockHeight)
)
{
changeDirection();
}
if(
(currentBallPosition[0]> boardWidth- ballDiameter)||
(currentBallPosition[1]> boardHeight- ballDiameter)||
(currentBallPosition[0]<0)
)
{
changeDirection()
}
if(currentBallPosition[1]<=0)
{
clearInterval(timerId)
scoreDisplay.innerHTML= "You lose! The final score is "+ score
document.removeEventListener("keydown",moveUser)
// user.classList.remove("user");
// ball.classList.remove("ball")
return true;
}
return false;
}
// control+ d makes sure you select and change the element with same vlaue at the same time.
function changeDirection()
{
if(xDirection>=0 && yDirection>=0.1)
{
yDirection= -yDirection;
return
}
else if(xDirection<0 && yDirection>=0.1)
{
xDirection= -xDirection;
return
}
else if(xDirection>= 0 && yDirection<= -0.1)
{
xDirection= -xDirection;
return
}
else if(xDirection< 0 && yDirection<=-0.1)
{
yDirection= -yDirection;
return
}
}
let num=0;
document.querySelector('#start').onclick= ()=>
{
document.addEventListener("keydown", moveUser)
// the event keydown refers to the action that a user press anykey on the keyBoard.
// as you can notice, the addEventListener function is added to the document instead of
// any specific element, this is because the keydown is a more general event that applyies to the entire
// screen.
randomXgenerator();
randomYgenerator();
timerId= setInterval(moveBall,30);
if(num>0)
{
location.reload();
}
num++;
}