Skip to content

Commit

Permalink
rotation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Sira-Ndiaye committed Jan 18, 2024
1 parent 70b1a5e commit c25b975
Showing 1 changed file with 145 additions and 38 deletions.
183 changes: 145 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,51 @@ class Square extends Shape {
rotate(){}
}

class Line extends Shape {
constructor(x, y, index) {
let blocks = [];
let color = "blue"
blocks.push(new Block(new Position((x * grid), (y * grid)), color));
blocks.push(new Block(new Position(((x - 1) * grid), (y * grid)), color));
blocks.push(new Block(new Position(((x + 1) * grid), (y * grid)), color));
blocks.push(new Block(new Position(((x + 2) * grid), (y * grid)), color));
super(blocks,false, index);
this.position = 0;
}

rotate() {
for (const block of this.blocks) {
block.undrawBlock();
}
if (this.position==0) {
let block = this.blocks[1];
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y + (2*grid))
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y + (3*grid))
this.position = 1;
}else if(this.position==1){
let block = this.blocks[1];
this.blocks[0].setPosition(block.getPosition().x + grid , block.getPosition().y)
this.blocks[2].setPosition(block.getPosition().x + (2*grid), block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x + (3*grid), block.getPosition().y)
this.position = 0;
}
this.init()
}

getNextPosition() {
return (this.position + 1) % 2;
}
}

class LShape extends Shape {
constructor(x, y, index) {
let blocks = [];
let color = "red";
blocks.push(new Block(new Position(x * grid, y * grid), color));
blocks.push(new Block(new Position((x - 1) * grid, y * grid), color ));
blocks.push(new Block(new Position(x * grid, (y + 1) * grid), color ));
blocks.push(new Block(new Position((x + 1) * grid, y * grid), color));
blocks.push(new Block(new Position((x + 1) * grid, (y + 1) * grid), color));
blocks.push(new Block(new Position((x + 2) * grid, y * grid), color));
super(blocks,false, index);
this.position = 0;
}
Expand All @@ -176,28 +213,51 @@ class LShape extends Shape {
block.undrawBlock();
}
if (this.position==0) {
let block = this.blocks[1];
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y -16)
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y - 32)
let block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y)
this.blocks[0].setPosition(block.getPosition().x + grid, block.getPosition().y)
block = this.blocks[0];
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y + (2*grid))
this.position=1;
}else if(this.position==1){
let block = this.blocks[1];
this.blocks[2].setPosition(block.getPosition().x + 32, block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x + 32, block.getPosition().y + 16)
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y + grid)
block = this.blocks[3];
this.blocks[2].setPosition(block.getPosition().x + grid, block.getPosition().y)
this.blocks[1].setPosition(block.getPosition().x + (2*grid), block.getPosition().y)
this.blocks[0].setPosition(block.getPosition().x + (2*grid), block.getPosition().y - grid)
this.position=2;
}else if(this.position==2){
let block = this.blocks[3];
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y - grid)
block = this.blocks[3];
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y + (2*grid))
this.blocks[0].setPosition(block.getPosition().x + grid, block.getPosition().y + (2*grid))
this.position=3;
}else if(this.position==3){
console.log("here")
let block = this.blocks[3];
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y)
block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[2].setPosition(block.getPosition().x + grid, block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x + (2*grid), block.getPosition().y)
this.position=0;
}
this.init()
}
}

class Line extends Shape {
class JShape extends Shape {
constructor(x, y, index) {
let blocks = [];
let color = "blue"
blocks.push(new Block(new Position((x * grid), (y * grid)), color));
blocks.push(new Block(new Position(((x - 1) * grid), (y * grid)), color));
blocks.push(new Block(new Position(((x + 1) * grid), (y * grid)), color));
blocks.push(new Block(new Position(((x + 2) * grid), (y * grid)), color));
let color = "red";
blocks.push(new Block(new Position(x * grid, y * grid), color));
blocks.push(new Block(new Position((x + 1) * grid, y * grid), color ));
blocks.push(new Block(new Position((x + 2) * grid, y * grid), color));
blocks.push(new Block(new Position((x + 2) * grid, (y + 1) * grid), color));
super(blocks,false, index);
this.position = 0;
}
Expand All @@ -207,34 +267,49 @@ class Line extends Shape {
block.undrawBlock();
}
if (this.position==0) {
let block = this.blocks[2];
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y - 16)
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y - 32)
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y + 16)
this.position = 1;
let block = this.blocks[0];
this.blocks[0].setPosition(block.getPosition().x + grid, block.getPosition().y)
block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y + (2*grid))
this.blocks[3].setPosition(block.getPosition().x - grid, block.getPosition().y + (2*grid))
this.position=1;
}else if(this.position==1){
let block = this.blocks[3];
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[1].setPosition(block.getPosition().x + grid, block.getPosition().y + grid)
this.blocks[0].setPosition(block.getPosition().x + (2*grid), block.getPosition().y + grid)
this.position=2;
}else if(this.position==2){
let block = this.blocks[3];
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y)
block = this.blocks[2];
this.blocks[3].setPosition(block.getPosition().x + grid, block.getPosition().y)
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y + (2*grid))
this.position=3;
}else if(this.position==3){
let block = this.blocks[2];
this.blocks[1].setPosition(block.getPosition().x -16 , block.getPosition().y)
this.blocks[0].setPosition(block.getPosition().x - 32, block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x + 16, block.getPosition().y)
this.position = 0;
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y)
block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x + grid, block.getPosition().y)
this.blocks[2].setPosition(block.getPosition().x + (2*grid), block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x + (2*grid), block.getPosition().y + grid)
this.position=0;
}
this.init()
}

getNextPosition() {
return (this.position + 1) % 2;
}
}


class TShape extends Shape {
constructor(x, y, index) {
let blocks = [];
let color = "brown";
blocks.push(new Block(new Position( x * grid, y * grid ), color));
blocks.push(new Block(new Position( (x + 1) * grid, y * grid ), color));
blocks.push(new Block(new Position( x * grid, (y - 1) * grid ), color));
blocks.push(new Block(new Position( x * grid, (y + 1) * grid ), color));
blocks.push(new Block(new Position( (x + 1) * grid, (y + 1) * grid ), color));
blocks.push(new Block(new Position( x * grid, (y + 2) * grid ), color));
super(blocks,false, index);
this.position = 0;
}
Expand All @@ -244,29 +319,40 @@ class TShape extends Shape {
block.undrawBlock();
}
if (this.position==0) {
let block = this.blocks[1];
this.blocks[3].setPosition(block.getPosition().x - 32, block.getPosition().y )
let block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x + grid, block.getPosition().y )
this.blocks[3].setPosition(block.getPosition().x + (2*grid), block.getPosition().y )
this.position=1;
}else if (this.position==1) {
let block = this.blocks[0];
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y +16)
this.blocks[1].setPosition(block.getPosition().x - 16, block.getPosition().y)
let block = this.blocks[2];
this.blocks[2].setPosition(block.getPosition().x - grid, block.getPosition().y)
block = this.blocks[1];
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y + (2*grid))
this.position=2;
}
else if (this.position==2) {
let block = this.blocks[0];
this.blocks[3].setPosition(block.getPosition().x + 16, block.getPosition().y)
let block = this.blocks[3];
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y - grid)
this.blocks[0].setPosition(block.getPosition().x - grid, block.getPosition().y)
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x + grid, block.getPosition().y)
this.position=3;
}
else if (this.position==3) {
console.log("here");
let block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y)
this.blocks[2].setPosition(block.getPosition().x + grid, block.getPosition().y)
this.blocks[3].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y - grid)
this.position=0;
}
this.init()
}
}

class ZShape extends Shape {
class SShape extends Shape {
constructor(x, y, index) {
let blocks = [];
let color = "green"
Expand All @@ -279,6 +365,24 @@ class ZShape extends Shape {
}

rotate() {
for (const block of this.blocks) {
block.undrawBlock();
}
if (this.position==0) {
let block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x + grid, block.getPosition().y )
this.blocks[2].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.blocks[3].setPosition(block.getPosition().x - grid, block.getPosition().y + grid)
this.position=1;
}else if (this.position==1) {
let block = this.blocks[0];
this.blocks[1].setPosition(block.getPosition().x, block.getPosition().y )
this.blocks[2].setPosition(block.getPosition().x + grid, block.getPosition().y + grid)
this.blocks[3].setPosition(block.getPosition().x + grid, block.getPosition().y + (2*grid))
this.blocks[0].setPosition(block.getPosition().x, block.getPosition().y + grid)
this.position=0;
}
this.init()
}
}

Expand Down Expand Up @@ -412,7 +516,7 @@ class Tetris{
generateRandomShape() {
let shape = null;
this.increaseIndex();
switch (this.getRandomRange(0, 4)) {
switch (this.getRandomRange(0, 5)) {
case 0:
shape = new Line(5,0,this.index);
break;
Expand All @@ -423,9 +527,12 @@ class Tetris{
shape = new LShape(5,0,this.index);
break;
case 3:
shape = new ZShape(5,0,this.index);
shape = new JShape(5,0,this.index);
break;
case 4:
shape = new SShape(5,0,this.index);
break;
case 5:
shape = new TShape(5,0,this.index);
break;
}
Expand Down

0 comments on commit c25b975

Please sign in to comment.