-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
188 lines (164 loc) · 4.52 KB
/
script.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
class Item {
constructor() {
this.element = document.createElement("div");
}
draw() {
document.body.appendChild(this.element);
this.element.style.width = this.width + 'px';
this.element.style.height = this.height + 'px';
this.element.style.left = this.x + 'px';
this.element.style.top = this.y + 'px';
}
}
class Brick extends Item {
constructor(r, c) {
super();
this.height = winH * 0.07;
this.width = (winW-(col+1)*gapCol) / col;
this.x = c*this.width + (c+1)*gapCol;
this.y = r*this.height + (r+1)*gapRow;
this.shield = Math.floor(Math.random()*4 + 1);
}
update() {
if(this.shield <= 0) {
this.width = 0;
this.height = 0;
}
this.element.style['background-color'] = colors[this.shield];
}
}
class Stick extends Item {
constructor() {
super();
this.height = winH * 0.03;
this.width = winW * 0.1;
this.x = (winW-this.width) / 2;
this.y = winH * 0.9
this.goRight = false;
this.goLeft = false;
}
update() {
if(this.goRight && this.x+this.width<winW) this.x += winW/1000;
if(this.goLeft && this.x>0) this.x -= winW/1000;
}
}
class Ball extends Item {
constructor() {
super();
this.height = winW * 0.015;
this.width = winW * 0.015;
this.x = (winW-this.width) / 2;
this.y = winH * 0.85
}
draw() {
super.draw();
this.element.style['border-radius'] = '100%';
}
update() {
this.x += dx;
this.y += dy;
// game over
if(this.y >= winH-this.height) {
clearInterval(loop);
title.innerHTML = 'game over!';
content.innerHTML = 'score: ' + score;
menu.style.visibility = 'visible';
scoreDisplay.style.visibility = 'hidden';
}
// hit broader
if(this.x<=0 || this.x>=winW-this.width) dx *= -1;
if(this.y<=0) dy *= -1;
// hit stick
if(this.x>=stick.x && this.x<=stick.x+stick.width && this.y+this.height>=stick.y) dy *= -1;
// hit bricks
for(let r = 0; r < row; r++) {
for(let c = 0; c < col; c++) {
if(brick[r][c].shield <= 0) continue;
// bottom
if(this.x>=brick[r][c].x &&
this.x<=brick[r][c].x+brick[r][c].width &&
this.y>=brick[r][c].y &&
this.y<=brick[r][c].y+brick[r][c].height) {
dy *= -1;
score += 5-brick[r][c].shield;
brick[r][c].shield--;
} // right
else if(this.x>=brick[r][c].c && this.x <= brick[r][c].x+brick[r][c].width &&
this.y>=brick[r][c].y && this.y<=brick[r][c].y+brick[r][c].height) {
dx *= -1;
score += 5-brick[r][c].shield;
brick[r][c].shield--;
} // left
else if(this.x+this.width>=brick[r][c].x && this.x+this.width<=brick[r][c].x+brick[r][c].width &&
this.y>=brick[r][c].y && this.y<=brick[r][c].y+brick[r][c].height) {
dx *= -1;
score += 5-brick[r][c].shield;
brick[r][c].shield--;
}
}
}
}
}
const winW = window.innerWidth, winH = window.innerHeight;
const col = 9, row = 3, gapCol = winW*0.004, gapRow = winH * 0.01
const colors = ['', '#F38BA8', '#F9E2AF', '#89B4FA', '#A6E3A1'];
let dx = winW*0.001*Math.sign(Math.random()-0.5);
let dy = winH*0.001*Math.sign(Math.random()-0.5);
let score = 0;
// create bricks
const brick = [];
for(let r = 0; r < row; r++) {
brick[r] = [];
for(let c = 0; c < col; c++) {
brick[r][c] = new Brick(r, c);
}
}
const stick = new Stick();
const ball = new Ball();
const menu = document.getElementById('menu');
const title = document.getElementById('title');
const content = document.getElementById('content');
const scoreDisplay = document.getElementById('scoreDisplay');
let gameStart = false;
// start moving stick
document.addEventListener('keydown', (event) => {
console.log(event.key);
if(event.key == 'h') {
stick.goLeft = true;
}
else if(event.key == 'l') {
stick.goRight = true;
}
else if(event.key=='Enter' && !gameStart) {
gameStart = true;
menu.style.visibility = 'hidden';
}
});
// stop moving stick
document.addEventListener('keyup', (event) => {
if(event.key == 'h') {
stick.goLeft = false;
}
else if(event.key == 'l') {
stick.goRight = false;
}
});
// game loop
const loop = setInterval(run, 1);
function run() {
if(gameStart) {
ball.update();
stick.update();
// update score
scoreDisplay.innerHTML = 'score: ' + score;
}
ball.draw();
stick.draw();
// update brick
for(let r = 0; r < row; r++) {
for(let c = 0; c < col; c++) {
brick[r][c].update();
brick[r][c].draw();
}
}
}