Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
401 lines (324 loc) · 8.79 KB
/
Copy pathscript.js
File metadata and controls
401 lines (324 loc) · 8.79 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
constcvs=document.getElementById("game");
constctx=cvs.getContext("2d");
constheight=cvs.height;
constwidth=cvs.width;
constrow=20;
constcol=10;
constsq=height/(24+1/3);
constvacant="white";
letrowCount=0;
letlevel=0;
letscore=0;
letpaddingHalf=.5*sq;
letpaddingThird=1/3*sq;
letpadding175=1.75*sq;
letboardHeight=20*sq;
letboardWidth=10*sq;
letuiHeight=3*sq-1/3*sq;
letuiWidth2=2*sq;
letuiWidth3=3*sq;
letdisplays=[];
constcenterX=width/2-col*sq/2;
constcenterY=height/2-row*sq/2;
//draw a square
functiondrawSquare(x,y,color,factor=1){
ctx.fillStyle=color;
ctx.fillRect(x,y,factor*sq,factor*sq);
ctx.strokeStyle="BLACK";
ctx.lineWidth=2;
ctx.strokeRect(x,y,factor*sq,factor*sq)
// checks to see if square is above the board
// only draws if below top of board
//if(newY >= height/2 - sq * row /2){
};
//create game board
letboard=[];
for(r=0;r<row;r++){
board[r]=[];
for(c=0;c<col;c++){
board[r][c]=vacant;
}
}
//draw game board
functiondrawBoard(){
for(r=0;r<row;r++){
for(c=0;c<col;c++){
letnewX=c*sq+padding175;
letnewY=r*sq+paddingHalf+uiHeight+paddingThird;
drawSquare(newX,newY,board[r][c]);
};
};
};
drawBoard();
// create a new display
functiondisplay(text="",coordX,width){
this.text=text
this.x=coordX;
this.y=paddingHalf;
this.width=width;
this.height=uiHeight;
this.color="rgba(0, 0, 0, 0.25)";
this.centerX=this.x+this.width/2;
this.centerY=this.y+this.height/2;
};
// draw display
display.prototype.draw=function(){
ctx.clearRect(this.x,this.y,this.width,this.height);
ctx.fillStyle=this.color;
ctx.fillRect(this.x,this.y,this.width,this.height);
ctx.strokeStyle="BLACK";
ctx.lineWidth=2;
ctx.strokeRect(this.x,this.y,this.width,this.height)
if(this.text){
ctx.font="bold 12px verdana";
ctx.textBaseline="top";
ctx.textAlign="center";
ctx.fillStyle="black";
ctx.fillText(this.text,this.centerX,this.y+paddingHalf*.2);
};
};
//let boardDisplay = new display( "BOARD", 0, 14, 5, 3);
letnextDisplay=newdisplay("NEXT",padding175,uiWidth3);
letrowDisplay=newdisplay("ROWS",padding175+uiWidth3+paddingThird,uiWidth2);
letlevelDisplay=newdisplay("LEVEL",padding175+uiWidth3+paddingThird+uiWidth2+paddingThird,uiWidth2);
letscoreDisplay=newdisplay("SCORE",padding175+uiWidth3+paddingThird+uiWidth2+paddingThird+uiWidth2+paddingThird,uiWidth2);
//boardDisplay.draw();
nextDisplay.draw();
rowDisplay.draw();
levelDisplay.draw();
scoreDisplay.draw();
//the pieces and their colors
constpieces=[
[z,"orangered"],
[s,"gold"],
[t,"chartreuse"],
[l,"hotpink"],
[j,"royalblue"],
[i,"cyan"],
[o,"darkviolet"]
];
//generate random tetromino
functionrandomPiece(){
letr=Math.floor(Math.random()*pieces.length);
returnnewpiece(pieces[r][0],pieces[r][1])
};
letp=randomPiece();
letnext=randomPiece();
//create piece object
functionpiece(tetromino,color){
this.tetromino=tetromino;
this.color=color;
this.tetrominoRotation=0;
this.activeTetromino=this.tetromino[this.tetrominoRotation]
// starting point
this.x=(this.activeTetromino.length>2 ? 3 : 4);
this.y=-(this.activeTetromino.length);
};
//create fill function
piece.prototype.fill=function(color){
for(r=0;r<this.activeTetromino.length;r++){
for(c=0;c<this.activeTetromino.length;c++){
if(this.activeTetromino[r][c]){
letnewX=(this.x+c)*sq+padding175;
letnewY=(this.y+r)*sq+paddingHalf+uiHeight+paddingThird;
if(newY>=paddingHalf+uiHeight){
drawSquare(newX,newY,color);
};
};
};
};
};
//draw a piece to the board
piece.prototype.draw=function(){
this.fill(this.color);
};
// undraw a piece
piece.prototype.undraw=function(){
this.fill(vacant);
};
// display next piece
// TRY THIS
// let nextDisplay
display.prototype.drawNext=function(nextPiece){
nextDisplay.draw();
letfactor=.5;
letnewSq=factor*sq;
letlength=nextPiece.activeTetromino.length
for(r=0;r<length;r++){
for(c=0;c<length;c++){
if(nextPiece.activeTetromino[r][c]){
letnewX=(c*newSq)+nextDisplay.centerX-(length/2*newSq);
letnewY=(r*newSq)+nextDisplay.centerY-(length/2*newSq);
drawSquare(newX,newY,nextPiece.color,factor);
};
};
};
};
//move down the piece
piece.prototype.moveDown=function(){
if(!this.collision(0,1,this.activeTetromino)){
this.undraw();
this.y++;
this.draw();
secondTimer=0;
}else{
//lock the piece and generate a new one
this.lock();
p=next;
next=randomPiece();
nextDisplay.drawNext(next);
};
};
//move the piece right
piece.prototype.moveRight=function(){
if(!this.collision(1,0,this.activeTetromino)){
this.undraw();
this.x++;
this.draw();
};
};
//move the piece left
piece.prototype.moveLeft=function(){
if(!this.collision(-1,0,this.activeTetromino)){
this.undraw();
this.x--;
this.draw();
};
};
//rotate the piece
piece.prototype.rotate=function(){
letnextRotation=this.tetromino[(this.tetrominoRotation+1)%this.tetromino.length];
letkick=0;
if(this.collision(0,0,nextRotation)){
//checks to see if collision is right or left wall
if(this.x>col/2){
//it's the right wall
kick=-1;
}else{
//its the left wall
kick=1;
};
};
if(!this.collision(kick,0,nextRotation)){
this.undraw();
this.x+=kick;
this.tetrominoRotation=(this.tetrominoRotation+1)%this.tetromino.length;
this.activeTetromino=this.tetromino[this.tetrominoRotation];
this.draw()
};
};
// lock the piece
piece.prototype.lock=function(){
for(r=0;r<this.activeTetromino.length;r++){
for(c=0;c<this.activeTetromino.length;c++){
// skip vacant square
if(!this.activeTetromino[r][c]){
continue;
};
//lock on top gameover
if(this.y+r<0){
alert("Game Over");
gameOver=true;
break;
};
// we lock the piece
board[this.y+r][this.x+c]=this.color;
};
};
// remove full rows
for(r=0;r<row;r++){
letisRowFull=true;
for(c=0;c<col;c++){
isRowFull=isRowFull&&(board[r][c]!=vacant);
};
if(isRowFull){
// if row is full
// move down all rows above it
for(y=r;y>1;y--){
for(c=0;c<col;c++){
board[y][c]=board[y-1][c]
};
};
// the top row has no row above it
for(c=0;c<col;c++){
board[0][c]=vacant;
};
// increment row count
rowCount++;
// increment level every five rows
if(rowCount%5===0){
level++
};
};
};
drawBoard();
};
// collision function
piece.prototype.collision=function(x,y,piece){
for(r=0;r<piece.length;r++){
for(c=0;c<piece.length;c++){
//check if square is vacant
if(!piece[r][c]){
continue;
};
// coordinates of piece after movement
letnewX=this.x+c+x;
letnewY=this.y+r+y;
// conditions
if(newX<0||newX>=col||newY>=row){
returntrue;
};
// skip newY < 0; board[-1] will crash game
if(newY<0){
continue;
};
//check if there is a locked piece alrready in place
if(board[newY][newX]!=vacant){
returntrue;
};
};
};
returnfalse;
};
//control the piece
document.addEventListener("keydown",control);
constkeys=[37,38,39,40,65,87,68,83]
functioncontrol(event){
letkeyPress=event.keyCode;
if(keys.includes(keyPress)){
p.resetTimer();
};
if(keyPress===37||keyPress===65){
p.moveLeft();
}elseif(keyPress===38||keyPress===87){
p.rotate();
}elseif(keyPress===39||keyPress===68){
p.moveRight();
}elseif(keyPress===40||keyPress===83){
p.moveDown();
};
};
//drop the piece down every 1 second
letdropStart=Date.now();
letgameOver=false;
functiondrop(){
letnow=Date.now();
letdelta=now-dropStart;
if(delta>1000){
p.moveDown();
dropStart=Date.now();
};
if(!gameOver){
requestAnimationFrame(drop)
};
};
letsecondTimer;
piece.prototype.resetTimer=function(){
if(!secondTimer){
dropStart=Date.now();
secondTimer=1;
};
};
p.draw();
nextDisplay.drawNext(next);
drop();