-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
196 lines (183 loc) · 4.98 KB
/
Copy pathgame.js
File metadata and controls
196 lines (183 loc) · 4.98 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
const grid = document.querySelector('.grid');
const scoreDisplay = document.querySelector('#score');
const blockWidth = 100;
const blockHeight = 20;
const boardWidth = 560;
const boardHeight = 300;
let timerId;
let score = 0;
const ballDiameter = 20;
let xDirection = 2;
let yDirection = 2;
// user position
const userStart = [230,10];
let currentPosition = userStart;
// ball position
const ballStart = [270,40];
let ballCurrentPosition = ballStart;
// create block
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 my blocks
const blocks = [
// 1st row
new Block(10,270),
new Block(120,270),
new Block(230,270),
new Block(340,270),
new Block(450,270),
//2nd row
new Block(10,240),
new Block(120,240),
new Block(230,240),
new Block(340,240),
new Block(450,240),
//3rd row
new Block(10,210),
new Block(120,210),
new Block(230,210),
new Block(340,210),
new Block(450,210),
//4th row
new Block(10,180),
new Block(120,180),
new Block(230,180),
new Block(340,180),
new Block(450,180),
]
// draw my block
function addBlocks() {
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);
}
}
addBlocks();
// add user
const user = document.createElement('div');
user.classList.add('user');
drawUser()
grid.appendChild(user);
// move user
function moveUser(e) {
switch (e.key) {
case 'ArrowLeft':
if(currentPosition[0] > 0){
currentPosition[0] -= 10;
drawUser();
}
break;
case 'ArrowRight':
if(currentPosition[0] < (boardWidth - blockWidth)){
currentPosition[0] += 10;
drawUser();
}
break;
default:
break;
}
}
// move ball
function moveBall() {
ballCurrentPosition[0] += xDirection;
ballCurrentPosition[1] += yDirection;
drawBall();
checkForCollisions();
}
timerId = setInterval(moveBall,20);
function checkForCollisions() {
// check for block collisions
for(let i=0; i<blocks.length;i++){
if(
(
ballCurrentPosition[0] > blocks[i].bottomLeft[0] &&
ballCurrentPosition[0] < blocks[i].bottomRight[0]
)
&&
(
(ballCurrentPosition[1] + ballDiameter) > blocks[i].bottomLeft[1] &&
ballCurrentPosition[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;
if(blocks.length === 0){
scoreDisplay.innerHTML = 'YOU WON';
clearInterval(timerId);
document.removeEventListener('keydown',moveUser);
}
}
}
// check for use collisions
if(
(ballCurrentPosition[0] > currentPosition[0] && ballCurrentPosition[0] < currentPosition[0] + blockWidth)
&&
(ballCurrentPosition[1] > currentPosition[1] && ballCurrentPosition[1] < currentPosition[1] + blockHeight)
){
changeDirection()
}
// check for wall collisions
if(
ballCurrentPosition[0] >= (boardWidth - ballDiameter)
||
ballCurrentPosition[1] >= (boardHeight - ballDiameter)
||
ballCurrentPosition[0] <= 0
){
changeDirection();
}
// check for game over
if(ballCurrentPosition[1] <= 0){
clearInterval(timerId);
scoreDisplay.innerHTML = 'You lose';
document.removeEventListener('keydown',moveUser);
}
}
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;
}
}
// draw the user
function drawUser() {
user.style.left = currentPosition[0] + 'px';
user.style.bottom = currentPosition[1] + 'px';
}
// draw the ball
function drawBall() {
ball.style.left = ballCurrentPosition[0] + 'px';
ball.style.bottom = ballCurrentPosition[1] + 'px';
}
// detect the keypress
document.addEventListener('keydown',moveUser)
// add ball
const ball = document.createElement('div');
ball.classList.add('ball');
drawBall();
grid.appendChild(ball);