-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetromino.js
39 lines (34 loc) · 911 Bytes
/
tetromino.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
class Tetromino {
constructor(ctx) {
this.ctx = ctx;
this.spawn();
}
draw() {
this.ctx.fillStyle = this.color;
this.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value > 0) {
this.ctx.fillRect(this.x + x, this.y + y, 1, 1);
}
});
});
}
move(p) {
field.checkLevel();
this.x = p.x;
this.y = p.y;
}
randomizeTetrominoType(noOfTypes) {
return Math.floor(Math.random() * noOfTypes + 1);
}
spawn() {
this.typeId = this.randomizeTetrominoType(COLORS.length - 1);
this.shape = SHAPES[this.typeId];
this.color = COLORS[this.typeId];
this.x = 0;
this.y = 0;
}
setStartPosition() {
this.x = this.typeId === 4 ? 4 : 3;
}
}