I’m attempting to make a Tetris-like game, where the game’s pieces are all made up of smaller pieces that share properties.
Currently I have:
export class SquareTetromino {
[x: string]: any;
constructor(x, y, w, h) {
...
}
show(p5) {
p5.push();
p5.translate(this.posX, this.posY);
p5.fill("#D8B6FF")
p5.rect(0,0,this.w, this.h);
p5.pop();
}
...
}
and:
export class BlockTetromino {
[x: string]: any;
constructor(x, y, w, h) {
...
}
test(p5) {
this.testArray.push(new SquareTetromino(this.posX,this.posY,this.w,this.h));
this.testArray.push(new SquareTetromino(this.posX - 50,this.posY,this.w,this.h));
this.testArray.push(new SquareTetromino(this.posX - 50,this.posY + 50,this.w,this.h));
this.testArray.push(new SquareTetromino(this.posX,this.posY + 50,this.w,this.h));
}
show(p5) {
p5.push();
this.testArray.forEach((block) => {
block.show(p5)
})
p5.pop();
}
}
And from my main component:
s.setup = () => {
...
bodies.push(new BlockTetromino(200,-50,50,50))
bodies[0].test(s);
...
}
s.draw = () => {
...
for (let i = 0; i < bodies.length; i++) {
bodies[i].show(s)
}
I’d like to be able to have a class Block which draws a small block, and then call that Block within a class Square, which draws 4 small blocks. Then, by instantiating Square I’ll have 4 blocks chained together as one object.
I think I’m missing a for loop somewhere.