Tetris

edited May 2016 in How To...

Hi, I am making a tetris game in processing but I don't know how to let the new block stop on the older block which has already fallen on the ground. the new block just goes through the older and stops on the ground. Can someone help me with this please? thanks.

Tagged:

Answers

  • Post your code and we'll take a look at it and give you some help!

  • edited May 2016

    okay thank you here is the code: `int cols = 11; int rows = 21;

    int grid[][] = new int[cols][rows];

    int lineSpace = 40; int verticalAmount = 11; int horizontalAmount = 21; int blockWidth = 40; int randomNr = (int)random(2); int teller; float angle;

    int kleur1 = color(255,0,0); int kleur2 = color(0,0,255);

    Blocks block1;

    Blocks[] block = new Blocks[0]; void setup(){ size(400,800); block1 = new Blocks(width/2,0,blockWidth,blockWidth);

    }

    void draw(){ background(112); strokeWeight(2);

    teller++; if(teller%400==0){ block = (Blocks[])append(block,new Blocks(width/2,0,blockWidth,blockWidth)); }

    block1.move(); block1.display();

    //block1.rotation(angle);

    for(int i = 0; i<block.length; i++){ block[i].move(); block[i].display(); // block[i].rotation(angle); }

    for(int i = 0; i < cols; i++){ for(int j = 0; j < rows; j++){ line(lineSpacei,0, lineSpacei,height); line(0,lineSpacej,width, lineSpacej); } }

    }

    void keyPressed(){ for(int i = 0; i<block.length; i++){ block[i].keyMove(); } block1.keyMove(); }

    class Blocks{

    float m_posX; float m_posY; float m_width; float m_height; float m_counter; float angle; color m_kleur; int randomNr;

    float oldBlock; float newBlock;

    int kleur1 = color(255,0,0); int kleur2 = color(0,0,255);

    Blocks(float x, float y, float w, float h){

    m_posX = x; m_posY = y; m_width = w; m_height = h; randomNr = (int)random(2); }

    void move(){

    m_counter++;
    m_counter%=20;
    if(m_counter == 0){
     m_posY+=40; 
    }
    if(m_posY+m_height >= height){
      m_counter=0;
      //m_posY = oldBlock;
    }
    
    // niewe block staat op oude block maar gaat terug langs boven naar beneden
    
    //if(m_counter == 20){
     //m_posY = newBlock; 
    //}
    

    }

    void display(){

    rectMode(CENTER);

    switch(randomNr){ case 0: fill(kleur1); rect(m_posX,m_posY,m_width2,m_height2); if(m_posX <= 0+40){ m_posX = 0+40; } if(m_posX >= width-40){ m_posX = width-40; } break;

     case 1: fill(kleur2);
     rect(m_posX,m_posY+20,m_width*4,m_height);
      if(m_posX <= 0+80){
       m_posX = 0+80; 
     }
       if(m_posX >= width-80){
        m_posX = width-80; 
      }
    }
    

    }

    void keyMove(){ if(m_posY+m_height < height){ if(keyCode == LEFT){ m_posX-=40; } if(keyCode == RIGHT){ m_posX+=40; } //if(keyCode == UP){ //angle += PI/2; //} if(keyCode == DOWN){ m_posY = height-40; m_counter = 0; } } }

    float getWidth(){ return m_posX + m_width; }

    float getHeight(){ return m_posY + m_height; }

    }`

  • Edit your post. Select the code. Press Ctrl+o.

  • Basically when they fall in a grid (2d array) check the fields right under the falling stone

Sign In or Register to comment.