Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Update the position of any blocks that need their position updated
- void update() {
- // Logic for dropping blocks ------------------------
- // Amount of blocks that are falling
- int falling=0;
- boolean clearingLine=false;
- // Iterate through the blocks in the field
- for (int i=blocks.size()-1; i>=0; i--) {
- // Whether to stop the block
- boolean stop = false;
- // If the block is falling
- if (blocks.get(i).fall) {
- // Add 1 to our falling counter
- falling++;
- // Check if the block is below the end of the screen
- if (blocks.get(i).by>=sizey) stop=true;
- // Check if the block has a block below it
- // Iterate through all blocks
- for (int j=blocks.size()-1; j>=0; j--) {
- // If the y value of j is below i
- if (blocks.get(j).by==blocks.get(i).by+1) {
- // If the x values are the same
- if (blocks.get(j).bx==blocks.get(i).bx) {
- // If j is not falling
- if (!blocks.get(j).fall) {
- // Stop i from falling
- stop=true;
- break;
- }
- }
- }
- }
- // Stop falling if stop is true
- if (stop) blocks.get(i).fall = false;
- // Push the block's position down
- if (!stop) blocks.get(i).by++;
- // If the block should move horizontally, move it
- if (moveDir!=0 && !stop) blocks.get(i).bx+=moveDir;
- }
- }
- moveDir = 0;
- // Logic for clearing lines --------------------
- // If no blocks are falling
- if (falling>0) {
- clearingLine=true;
- // Iterate through each line
- for (int i=0; i<sizey; i++) {
- // How many blocks are on this line
- int howMany=0;
- // Iterate through the blocks on the field
- for (int j=blocks.size()-1; j>=0; j--) {
- // If the block is at this line, count it
- if (blocks.get(j).by==i) howMany++;
- }
- // If there are more blocks on this line than the width of the field, clear this line
- if (howMany>=sizex) removeLine(i);
- }
- }
- // If no blocks are falling and we aren't clearing a line, add a new shape
- if (!clearingLine) initializeShape(calculateNextShape());
- }
- // Remove a line of blocks from the field and update the blocks above to fall.
- void removeLine(float line) {
- // Iterate through all lines before the one to be removed
- for (int i=0; i<line-1; i++) {
- // Iterate through the blocks on the field
- for (int j=blocks.size()-1; j>=0; j--) {
- // If the block is at the current line, make it fall.
- if (blocks.get(j).by==i) blocks.get(j).fall=true;
- }
- }
- // Iterate through the blocks on the field
- for (int i=blocks.size()-1; i>=0; i--) {
- if (blocks.get(i).by==line) blocks.remove(i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment