superluig164

Untitled

Nov 10th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1.   // Update the position of any blocks that need their position updated
  2.   void update() {
  3.     // Logic for dropping blocks ------------------------
  4.     // Amount of blocks that are falling
  5.     int falling=0;
  6.     boolean clearingLine=false;
  7.  
  8.     // Iterate through the blocks in the field
  9.     for (int i=blocks.size()-1; i>=0; i--) {
  10.       // Whether to stop the block
  11.       boolean stop = false;
  12.  
  13.       // If the block is falling
  14.       if (blocks.get(i).fall) {
  15.  
  16.         // Add 1 to our falling counter
  17.         falling++;
  18.  
  19.         // Check if the block is below the end of the screen
  20.         if (blocks.get(i).by>=sizey) stop=true;
  21.  
  22.         // Check if the block has a block below it
  23.         // Iterate through all blocks
  24.         for (int j=blocks.size()-1; j>=0; j--) {
  25.           // If the y value of j is below i
  26.           if (blocks.get(j).by==blocks.get(i).by+1) {
  27.             // If the x values are the same
  28.             if (blocks.get(j).bx==blocks.get(i).bx) {
  29.               // If j is not falling
  30.               if (!blocks.get(j).fall) {
  31.                 // Stop i from falling
  32.                 stop=true;
  33.                 break;
  34.               }
  35.             }
  36.           }
  37.         }
  38.  
  39.         // Stop falling if stop is true
  40.         if (stop) blocks.get(i).fall = false;
  41.  
  42.         // Push the block's position down
  43.         if (!stop) blocks.get(i).by++;
  44.  
  45.         // If the block should move horizontally, move it
  46.         if (moveDir!=0 && !stop) blocks.get(i).bx+=moveDir;
  47.       }
  48.     }
  49.     moveDir = 0;
  50.  
  51.     // Logic for clearing lines --------------------
  52.     // If no blocks are falling
  53.     if (falling>0) {
  54.       clearingLine=true;
  55.       // Iterate through each line
  56.       for (int i=0; i<sizey; i++) {
  57.         // How many blocks are on this line
  58.         int howMany=0;
  59.  
  60.         // Iterate through the blocks on the field
  61.         for (int j=blocks.size()-1; j>=0; j--) {
  62.           // If the block is at this line, count it
  63.           if (blocks.get(j).by==i) howMany++;
  64.         }
  65.         // If there are more blocks on this line than the width of the field, clear this line
  66.         if (howMany>=sizex) removeLine(i);
  67.       }
  68.     }
  69.     // If no blocks are falling and we aren't clearing a line, add a new shape
  70.     if (!clearingLine) initializeShape(calculateNextShape());
  71.   }
  72.  
  73.   // Remove a line of blocks from the field and update the blocks above to fall.
  74.   void removeLine(float line) {
  75.     // Iterate through all lines before the one to be removed
  76.     for (int i=0; i<line-1; i++) {
  77.       // Iterate through the blocks on the field
  78.       for (int j=blocks.size()-1; j>=0; j--) {
  79.         // If the block is at the current line, make it fall.
  80.         if (blocks.get(j).by==i) blocks.get(j).fall=true;
  81.       }
  82.     }
  83.  
  84.     // Iterate through the blocks on the field
  85.     for (int i=blocks.size()-1; i>=0; i--) {
  86.       if (blocks.get(i).by==line) blocks.remove(i);
  87.     }
  88.   }
Advertisement
Add Comment
Please, Sign In to add comment