-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
319 lines (281 loc) · 8.68 KB
/
game.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import IPiece from './ipiece.js';
import JPiece from './jpiece.js';
import LPiece from './lpiece.js';
import OPiece from './opiece.js';
import SPiece from './spiece.js';
import TPiece from './tpiece.js';
import ZPiece from './zpiece.js';
import BorderSq from './bordersq.js';
import Dialog from './dialog.js';
import {INITIAL_SPEED,
SQWIDTH,
NUM_PIECE_SQS,
SCORE_PANEL_HEIGHT,
NEW_PIECE_X,
NEW_PIECE_Y,
CHARCOAL_GRAY,
LARGE_FONT_SIZE,
MEDIUM_FONT_SIZE,
SMALL_FONT_SIZE,
FONT_FAMILY,
JKEY,
KKEY,
LKEY,
PKEY,
COMMAKEY,
SPACEBAR,
BLACK,
NORMAL_CLEAR_PTS,
NUM_FOR_TETRIS,
TETRIS_CLEAR_PTS,
DOUBLE_TETRIS_CLEAR_PTS,
SPEED_UP_AMT,
TOP_SPEED,
RED,
TEAL} from './constants.js';
export default class Game {
constructor() {
this.scoreCounter = 0;
this.dialog = new Dialog();
this.go = false;
this.isLostGame = false;
this.isFreshGame = true;
this.tetris = false;
this.board = [];
this.speediness = INITIAL_SPEED; // changes with game play
}
init() {
const canvas = document.querySelector('#mainCanvas');
let board = this.board;
if (!canvas.getContext) {
return false;
}
this.ctx = canvas.getContext('2d');
// keeping everything contingent on that which cannot be set here (canvas dimensions)
this.panelWidth = canvas.width;
this.gamePanelHeight = canvas.height - SCORE_PANEL_HEIGHT;
this.rows = this.gamePanelHeight/SQWIDTH + 1;
this.cols = this.panelWidth/SQWIDTH;
//game panel
this.ctx.fillStyle = BLACK;
this.ctx.fillRect(0, 0, this.panelWidth, this.gamePanelHeight);
//make the pieces array. It's empty until pieces start sticking
for (let j = 0; j < this.rows; j++) {
board[j] = [];
}
for (let y = 0; y < this.rows; y++) {
board[0][y] = new BorderSq();
board[0][y].setLocation(0,y);
board[this.cols - 1][y] = new BorderSq();
board[this.cols - 1][y].setLocation(this.cols - 1, y);
}
for (let x = 0; x < this.cols; x++) {
board[x][0] = new BorderSq();
board[x][this.rows - 1] = new BorderSq();
board[x][0].setLocation(x, 0);
board[x][this.rows - 1].setLocation(x, this.rows - 1);
}
this.currPiece = this.pieceFactory(this);
this.currPiece.setLocation(NEW_PIECE_X, NEW_PIECE_Y);
this.drawBoard();
this.drawWelcomeDialog();
this.setupKeyListeners();
this.setupClickListeners();
return true;
}
drawWelcomeDialog() {
this.ctx.fillStyle = this.dialog.color;
this.ctx.fillRect(this.dialog.x, this.dialog.y, this.dialog.width, this.dialog.height);
this.ctx.fillStyle = BLACK;
this.ctx.font = `${MEDIUM_FONT_SIZE} ${FONT_FAMILY}`;
this.ctx.fillText('Click to Make it Rain Pieces!', this.dialog.x + 10, this.dialog.y + 40, 280);
this.ctx.font = `${SMALL_FONT_SIZE} ${FONT_FAMILY}`;
this.ctx.fillText('Left = J-Key, Right = L-key', this.dialog.x + 35, this.dialog.y + 70, 230);
this.ctx.fillText('Rotate = K-key, Down = Comma-key', this.dialog.x + 35, this.dialog.y + 90, 230);
this.ctx.fillText('Drop = Space', this.dialog.x + 35, this.dialog.y + 110, 230);
this.ctx.fillText('Pause = P-key', this.dialog.x + 35, this.dialog.y + 130, 230);
}
drawBoard() {
const board = this.board;
for (let r = 0; r < this.rows; r++) {
for (let c = 0; c < this.cols; c++) {
if (board[c][r]){
board[c][r].draw()
}
}
}
//score panel
this.ctx.fillStyle = CHARCOAL_GRAY;
this.ctx.fillRect(0, this.gamePanelHeight, this.panelWidth, SCORE_PANEL_HEIGHT);
this.ctx.font = `${LARGE_FONT_SIZE} ${FONT_FAMILY}`;
this.ctx.fillStyle = RED;
this.ctx.fillText('Score: '+ this.scoreCounter, 20, this.gamePanelHeight + 40, this.panelWidth - 20);
}
setupKeyListeners() {
document.addEventListener('keydown', (e) => {
//if game is in play, J-key moves falling piece left
if (e.keyCode === JKEY) {
if (this.go){
e.preventDefault();
this.currPiece.moveLeft()
}
}
//if game is in play, L-key moves piece right
if (e.keyCode === LKEY) {
if (this.go){
e.preventDefault();
this.currPiece.moveRight()
}
}
//if game is in play, K-key rotates the piece
if (e.keyCode === KKEY) {
if (this.go) {
e.preventDefault();
this.currPiece.rotate();
}
}
//if game is in play, comma-key moves piece down a row
if (e.keyCode === COMMAKEY) {
if (this.go){
e.preventDefault();
this.currPiece.moveDown()
}
}
//if game is in play, space bar drops the piece
if (e.keyCode === SPACEBAR) {
if (this.go) {
e.preventDefault();
this.currPiece.drop();
}
}
//p for pause
if (e.keyCode === PKEY) {
e.preventDefault();
this.go = !this.go;
}
});
}
setupClickListeners() {
mainCanvas.addEventListener('click', (e) => {
if (this.isFreshGame) {
this.isFreshGame = false;
}
if (!this.go){
this.go = true;
}
if (this.isLostGame) {
clearInterval(this.game_loop);
this.game_loop = start();
this.isLostGame = false
this.go = false;
this.isFreshGame = true;
}
});
}
//DRAWING updates
draw() {
if (this.go) {
//game panel
this.ctx.fillStyle = BLACK;
this.ctx.fillRect(0, 0, this.panelWidth, this.gamePanelHeight);
this.drawBoard();
this.currPiece.draw();
} else {
// game is paused
if(!this.isLostGame && !this.isFreshGame) {
this.ctx.fillStyle = this.dialog.color;
this.ctx.fillRect(this.dialog.x, this.dialog.y, this.dialog.width, this.dialog.height);
this.ctx.fillStyle = BLACK;
this.ctx.font = `${MEDIUM_FONT_SIZE} ${FONT_FAMILY}`;
this.ctx.fillText('Game Paused.', this.dialog.x + 65, this.dialog.y+80, 280);
}
// game is over
else if (this.isLostGame && !this.isFreshGame) {
this.ctx.fillStyle= this.dialog.color;
this.ctx.fillRect(this.dialog.x, this.dialog.y, this.dialog.width, this.dialog.height);
this.ctx.fillStyle = BLACK;
this.ctx.font = `${MEDIUM_FONT_SIZE} ${FONT_FAMILY}`;
this.ctx.fillText('Sorry! Game Over :(', this.dialog.x + 30, this.dialog.y + 70, 280);
this.ctx.font = `${SMALL_FONT_SIZE} ${FONT_FAMILY}`;
this.ctx.fillText('Click to start a new game.', this.dialog.x + 55, this.dialog.y + 95, 230);
return;
}
}
}
pieceFactory(game) {
switch (Math.floor(Math.random() * 7)) {
case 0:
return new IPiece(game);
case 1:
return new JPiece(game);
case 2:
return new LPiece(game);
case 3:
return new OPiece(game);
case 4:
return new SPiece(game);
case 5:
return new TPiece(game);
case 6:
return new ZPiece(game);
}
}
makeNewPiece() {
this.currPiece = this.pieceFactory(this);
if (!this.isLostGame) {
this.currPiece.setLocation(NEW_PIECE_X, NEW_PIECE_Y);
}
}
checkLines() {
let board = this.board;
let numCleared = 0;
for (let j = 1; j < this.rows - 1; j++) {
let numFull = 0;
for (let i = 1; i < this.cols - 1; i++) {
if (!board[i][j]) {
break;
} else {
numFull++;
}
}
if (numFull === this.cols - 2) {
numCleared++;
for (let p = j; p > 2; p--) {
//cols
for (let q = 1; q < this.cols - 1; q++) {
board[q][p] = board[q][p - 1]
if (board[q][p]){
board[q][p].setLocation(q, p);
this.draw();
}
}
}
if (this.speediness > TOP_SPEED) {
this.speediness = this.speediness - SPEED_UP_AMT;
}
this.scoreCounter = this.scoreCounter + NORMAL_CLEAR_PTS;
//one tetris
if (numCleared === NUM_FOR_TETRIS && !this.tetris) {
this.scoreCounter = this.scoreCounter + TETRIS_CLEAR_PTS;
this.tetris = true;
}
//back to back tetrises!!!
else if (numCleared === NUM_FOR_TETRIS && this.tetris) {
this.scoreCounter = this.scoreCounter + DOUBLE_TETRIS_CLEAR_PTS;
}
else {
this.tetris = false;
}
}
}
}
checkLoss() {
for (let s = 0; s < NUM_PIECE_SQS; s++) {
if (this.board[this.currPiece.sqArray[s][0]][this.currPiece.sqArray[s][1]]){
this.go = false;
this.isLostGame = true;
this.draw()
}
}
}
};