-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
203 lines (179 loc) · 5.52 KB
/
app.js
File metadata and controls
203 lines (179 loc) · 5.52 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
const grid = document.querySelector(".grid");
const scoreDisplay = document.querySelector("#score");
const startButton = document.querySelector(".start");
const blockWidth = 100;
const blockHeight = 20;
const boardWidth = 560;
const boardHeight = 300;
const ballDiameter = 20;
let ballTimerID;
let xDirection = -2;
let yDirection = 2;
let score = 0;
const userStart = [230, 10];
let userCurrentPos = userStart;
const ballStart = [270, 40];
let ballCurrentPos = ballStart;
// --------------- create a Block class ------------------
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];
}
}
// -------------------------------------------------------
// -------------- constructing 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),
];
//console.log(blocks[0]);
// ------------------------------------------------------
// -------------- draw all the blocks -------------------
function drawBlocks() {
for (let i = 0; i < blocks.length; i++) {
const block = document.createElement("div");
block.classList.add("block");
block.style.left = blocks[i].bottomLeft[0] + "px";
block.style.bottom = blocks[i].bottomLeft[1] + "px";
grid.appendChild(block);
}
}
drawBlocks();
// ------------------------------------------------------
// ------------------ add user --------------------------
const user = document.createElement("div");
user.classList.add("user");
drawUser();
grid.appendChild(user);
// ------------------------------------------------------
// --------------- draw the user ------------------------
function drawUser() {
user.style.left = userCurrentPos[0] + "px";
user.style.bottom = userCurrentPos[1] + "px";
}
// -------------- move the user -------------------------
function moveUser(e) {
switch (e.key) {
case "ArrowLeft":
if (userCurrentPos[0] > 0) {
userCurrentPos[0] -= 10;
drawUser();
}
break;
case "ArrowRight":
if (userCurrentPos[0] < boardWidth - blockWidth) {
userCurrentPos[0] += 10;
drawUser();
}
break;
}
}
document.addEventListener("keydown", moveUser);
// ------------------------------------------------------
// ---------------- draw the ball -----------------------
function drawBall() {
ball.style.left = ballCurrentPos[0] + "px";
ball.style.bottom = ballCurrentPos[1] + "px";
}
//-------------------------------------------------------
// ----------------- add a ball -------------------------
const ball = document.createElement("div");
ball.classList.add("ball");
drawBall();
grid.appendChild(ball);
// ------------------------------------------------------
// --------------- move the ball ------------------------
function moveBall() {
ballCurrentPos[0] += xDirection;
ballCurrentPos[1] += yDirection;
drawBall();
checkForCollisions();
}
function startGame() {
ballTimerID = setInterval(moveBall, 20);
}
startButton.addEventListener("click", startGame);
//let ballTimerID = setInterval(moveBall, 20);
// ------------- check for collisions ------------------
function checkForCollisions() {
// check for block collisions
for (let i = 0; i < blocks.length; i++) {
if (
ballCurrentPos[0] > blocks[i].bottomLeft[0] &&
ballCurrentPos[0] < blocks[i].bottomRight[0] &&
ballCurrentPos[1] + ballDiameter > blocks[i].bottomLeft[1] &&
ballCurrentPos[1] < blocks[i].topLeft[1]
) {
const allBlocks = Array.from(document.querySelectorAll(".block"));
allBlocks[i].classList.remove("block");
blocks.splice(i, 1);
changeDirection();
score++;
scoreDisplay.innerHTML = score;
// check for win
if (blocks.length === 0) {
scoreDisplay.innerHTML = "You won!";
clearInterval(ballTimerID);
document.removeEventListener("keydown", moveUser);
}
}
}
// check for user collisions
if (
ballCurrentPos[0] > userCurrentPos[0] &&
ballCurrentPos[0] < userCurrentPos[0] + blockWidth &&
ballCurrentPos[1] > userCurrentPos[1] &&
ballCurrentPos[1] < userCurrentPos[1] + blockHeight
) {
changeDirection();
}
// check for wall collisions
if (
ballCurrentPos[0] > boardWidth - ballDiameter ||
ballCurrentPos[1] > boardHeight - ballDiameter ||
ballCurrentPos[0] <= 0
) {
changeDirection();
}
// check for game over
if (ballCurrentPos[1] <= 0) {
clearInterval(ballTimerID);
scoreDisplay.innerHTML = "You lost";
document.removeEventListener("keydown", moveUser);
}
}
// ---------- change direction of the ball -------------
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;
}
}