-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
199 lines (174 loc) · 4.53 KB
/
app.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
const grid = document.querySelector(".grid");
const scoreDisplay = document.querySelector(".score");
const blockHeight = 20;
const blockWidth = 100;
let score = 0;
const boardWidth = 560;
const boardHeight = 300;
const ballDiameter = 20;
let xDirection = 2;
let yDirection = 2;
let ballTimerId;
const userStart = [230, 10];
let currentUserPosition = userStart;
const ballStart = [270, 40];
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];
}
}
//create blocks
const blocks = [
new Block(10, 270),
new Block(120, 270),
new Block(230, 270),
new Block(340, 270),
new Block(450, 270),
new Block(10, 240),
new Block(120, 240),
new Block(230, 240),
new Block(340, 240),
new Block(450, 240),
new Block(10, 210),
new Block(120, 210),
new Block(230, 210),
new Block(340, 210),
new Block(450, 210),
];
//draw blocks
function addBlocks() {
for (let index = 0; index < blocks.length; index++) {
const block = document.createElement("div");
block.classList.add("block");
block.style.left = blocks[index].bottomLeft[0] + "px";
block.style.bottom = blocks[index].bottomLeft[1] + "px";
grid.appendChild(block);
}
}
addBlocks();
//draw user
const user = document.createElement("div");
user.classList.add("user");
drawUser();
grid.appendChild(user);
function drawUser() {
user.style.left = currentUserPosition[0] + "px";
user.style.bottom = currentUserPosition[1] + "px";
}
function drawBall() {
ball.style.left = currentBallPosition[0] + "px";
ball.style.bottom = currentBallPosition[1] + "px";
}
//move user
function moveUser(e) {
switch (e.key) {
case "ArrowLeft":
if (currentUserPosition[0] > 0) {
currentUserPosition[0] -= 10;
drawUser();
}
break;
case "ArrowRight":
if (currentUserPosition[0] < boardWidth - blockWidth) {
currentUserPosition[0] += 10;
drawUser();
}
break;
}
}
document.addEventListener("keydown", moveUser);
//draw ball
const ball = document.createElement("div");
ball.classList.add("ball");
drawBall();
grid.appendChild(ball);
//move ball
function moveBall() {
currentBallPosition[0] += xDirection;
currentBallPosition[1] += yDirection;
drawBall();
checkCollisions();
}
moveBall();
ballTimerId = setInterval(moveBall, 30);
function checkCollisions() {
checkForBlockCollision();
checkForWallCollisions();
checkForUserCollision();
checkForGameOver();
}
function checkForUserCollision() {
if (
currentBallPosition[0] > currentUserPosition[0] &&
currentBallPosition[0] < currentUserPosition[0] + blockWidth &&
currentBallPosition[1] > currentUserPosition[1] &&
currentBallPosition[1] < currentUserPosition[1] + blockHeight
) {
changeDirection();
}
}
function checkForBlockCollision() {
for (let index = 0; index < blocks.length; index++) {
let block = blocks[index];
if (
currentBallPosition[0] > block.bottomLeft[0] &&
currentBallPosition[0] < block.bottomRight[0] &&
currentBallPosition[1] + ballDiameter > block.bottomLeft[1] &&
currentBallPosition[1] < block.topLeft[1]
) {
const allBlocks = Array.from(document.querySelectorAll(".block"));
allBlocks[index].remove("block");
blocks.splice(index, 1);
changeDirection();
scoreDisplay.innerHTML = ++score;
checkForWin();
}
}
}
function checkForWin() {
if (blocks.length === 0) {
clearInterval(ballTimerId);
scoreDisplay.innerHTML = `You Win!`;
document.removeEventListener("keydown", moveUser);
}
}
function checkForGameOver() {
if (currentBallPosition[1] <= 0) {
clearInterval(ballTimerId);
scoreDisplay.innerHTML = `${score} You lose!`;
document.removeEventListener("keydown", moveUser);
}
}
function checkForWallCollisions() {
if (currentBallPosition[0] >= boardWidth - ballDiameter) {
changeDirection();
}
if (currentBallPosition[1] >= boardHeight - ballDiameter) {
changeDirection();
}
if (currentBallPosition[0] <= 0) {
changeDirection();
}
}
function changeDirection() {
if (xDirection == 2 && yDirection == 2) {
yDirection = -2;
return;
}
if (xDirection == 2 && yDirection == -2) {
xDirection = -2;
return;
}
if (xDirection == -2 && yDirection == -2) {
yDirection = 2;
return;
}
if (xDirection == -2 && yDirection == 2) {
xDirection = 2;
return;
}
}