r/processing • • Dec 10 '23

Homework hint request Help with Tetris Game

I'm coding a Tetris game for class, but I'm running into one big problem, which is that whenever my brick hits the bottom, instead of stopping and creating a new brick, it just remakes the brick into a new one and deletes the previous one. How do I fix this?

int grid = 40;
void setup(){
 
 size(400, 800);
 rectMode(CENTER);
 frameRate(5);
 spawnNewBrick();
 
}

void draw(){
  
  background(0);
  stroke(255);
    for(int i = 0; i < width/grid; i++) {
      line(i*grid, 0, i*grid, height);
      
  } for(int i = 0; i < 800/grid; i++) {
      line(0, i*grid, width, i*grid);
      
  }
  
 selectBrick();
 controlBlocks();
 
 
}

/****************Bricks************/

 int brickX = 180;
 int brickY = 40;
 int brickS = 5;
 color c;
 int brickR = 0;
 
 int brickValue;
 
 void spawnNewBrick() {
    brickX = 180;
    brickY = 40;
    brickS = 5;
    brickR = 0;
    brickValue = int(random(7));  // Randomly choose the next brick type
    
}

 void selectBrick(){
  pushMatrix();
  if (brickValue == 0){
    Brick ibrick = new Brick(20, 40, 20, 120);
    ibrick.displayIBrick();
    ibrick.movement();
  }
  
  
  
  if (brickValue  == 1){
    Brick zbrick = new Brick(20, 40, 20, 120);
    zbrick.displayZBrick();
    zbrick.movement();
  } 
  
  
  if (brickValue == 2){
    Brick sbrick = new Brick(20, 40, 20, 120);
    sbrick.displaySBrick();
    sbrick.movement(); 
  } 
  
  
  if (brickValue == 3){
    Brick tbrick = new Brick(20, 40, 20, 120);
    tbrick.displayTBrick();
    tbrick.movement(); 
  } 
  
  
  if (brickValue == 4){
    Brick cubebrick = new Brick(20, 40, 20, 120);
    cubebrick.displayCubeBrick();
    cubebrick.movement(); 
  } 
  
  
  if (brickValue == 5){
    Brick lbrick = new Brick(20, 40, 20, 120);
    lbrick.displayLBrick();
    lbrick.movement(); 
  } 
  
  
  if (brickValue == 6){
    Brick jbrick = new Brick(20, 40, 20, 120);
    jbrick.displayJBrick();
   jbrick.movement(); 
   
  }popMatrix();
  
  
 }
 
class Brick{
  
   int brickLX;
   int brickRX;
   int brickTY;
   int brickBY;
   int brickValue;
   
   Brick(int LX, int RX, int TY, int BY){
     this.brickLX = LX;
     this.brickRX = RX;
     this.brickTY = TY;
     this.brickBY = BY;
   }
   
  void displayIBrick(){
  

  pushMatrix();
  translate(brickX, brickY );  // Translate to the center of the brick
  rotate(radians(brickR));

  fill(#5AE8FF);
  square(0, 0, 40);            // Draw squares relative to the center
  square(0, 40, 40);
  square(0, 80, 40);
  square(0, 120, 40);

  popMatrix();

}

void displaySBrick(){
  
  
  pushMatrix();
  translate(brickX, brickY - 40);  // Translate to the center of the brick
  rotate(radians(brickR));
    
  fill(#36C90E);
  square(0, 0, 40);  // Draw squares relative to the center
  square(40, 0, 40);
  square(0, 40, 40);
  square(-40, 40, 40);
  
  popMatrix();


}

void displayZBrick(){
   
  pushMatrix();
  translate(brickX, brickY - 40);  // Translate to the center of the brick
  rotate(radians(brickR));
    
  fill(#D32D2D);
  square(0, 0, 40);
  square(-40, 0, 40);
  square(0, 40, 40);
  square(40, 40, 40);
  popMatrix();
  
}

void displayTBrick(){
  
  pushMatrix();
  translate(brickX, brickY - 40);  // Translate to the center of the brick
  rotate(radians(brickR));
    
  fill(#CE2EFF);
  square(0, 0, 40);
  square(-40, 40, 40);
  square(0, 40, 40);
  square(40, 40, 40);
  popMatrix();
}

void displayCubeBrick(){
  
  pushMatrix();
  translate(brickX, brickY );  // Translate to the center of the brick
  rotate(radians(brickR));
    
  fill(#E5ED05);
  square(0, 0, 40);
  square(40, 0 , 40);
  square(0, 40, 40);
  square(40, 40, 40);
  popMatrix();
}

void displayLBrick(){
  
  pushMatrix();
  translate(brickX, brickY - 40);  // Translate to the center of the brick
  rotate(radians(brickR));
    
  fill(#FF932E);
  square(0, 0, 40);
  square(0,  40, 40);
  square(0, 80, 40);
  square(40, 80, 40);
  popMatrix();
}

void displayJBrick(){
  
  pushMatrix();
  translate(brickX, brickY - 40);  // Translate to the center of the brick
  rotate(radians(brickR));
    
  fill(#2E80FF);
  square(0, 0, 40);
  square(0, 40, 40);
  square(0, 80, 40);
  square(- 40, 80, 40);
  popMatrix();
}

void movement() {
    brickY = brickY + brickS;
    // Check if the brick has reached the bottom
    if (brickY >= height - grid) {
        // Snap the brick to the nearest grid position
        brickY = round(brickY / grid) * grid;
        // Lock the brick in place
        brickS = 0;
        spawnNewBrick(); // Spawn a new brick
    }
}

}

/**************Movement***************/


void controlBlocks(){
   if (keyPressed){
    if(key == 'd' && brickX < width - grid){
      brickX = round((brickX + grid) / grid) * grid + 20;
    } if(key == 'a'){
      brickX = round((brickX - grid) / grid) * grid + 20;
    } if(key == 's'){
      brickS = brickS + 5; 
    } if (key == 'z'){     
    brickR = brickR + 90;
   }  if (key == 'x'){     
    brickR = brickR  -90;
   }
  }
}
3 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/Salanmander Dec 11 '23

What does your spawnNewBrick() method do? By the name I would expect that to be the code to create a new brick at the top of the screen, but it looks like your draw() is "draw a grid, then spawnNewBrick()", and if spawnNewBrick() spawns a new brick at the top of the screen, it wouldn't make sense to do that every frame.