superluig164

Untitled

Nov 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.54 KB | None | 0 0
  1. /* Written by Tommy Sebesyen
  2.  * Purpose: This class handles the "real" x and y positions of
  3.  * objects on the playfield.  It uses blockSize, gameWidth, and
  4.  * gameHeight to calculate the real playfield size based on the
  5.  * window size.
  6.  */
  7.  
  8. class Field {
  9.  
  10.   float sizex; // Width of the playfield in blocks.
  11.   float sizey; // Height of the playfield in blocks.
  12.   float sizeb; // Diameter of each block.
  13.   ArrayList<Integer> shapes = new ArrayList<Integer>(); // Array for calculating shapes, to ensure you never get the same shape twice (as per Tetris standard)
  14.   ArrayList<Block> blocks = new ArrayList<Block>(); // Array of Blocks that make up the game field.
  15.   int moveDir; // Direction and distance to move a falling shape
  16.   int linesCleared = 0; // How many lines have been cleared
  17.  
  18.   float centerx; // Center constants
  19.   float centery; // Center constants
  20.  
  21.   // Various color constants
  22.   final color purple = color(184, 2, 253);
  23.   final color red = color(254, 16, 60);
  24.   final color green = color(102, 253, 0);
  25.   final color yellow = color(255, 222, 0);
  26.   final color orange = color(255, 115, 8);
  27.   final color dblue = color(24, 1, 255);
  28.   final color lblue = color(0, 230, 254);
  29.  
  30.   // Default constructor (should not be used)
  31.   Field() {
  32.     sizex = 10;
  33.     sizey = 20;
  34.     sizeb = 5;
  35.     println("ERROR: Something called an uninitialized Field.  This should not happen.");
  36.   }
  37.  
  38.   // x - the desired playfield width, in blocks
  39.   // y - the desired playfield height, in blocks
  40.   // b - the desired block size, in pixels
  41.   Field(float x, float y, float b) {
  42.     // Set internal fields to constructor parameters
  43.     sizex = x;
  44.     sizey = y;
  45.     sizeb = b;
  46.  
  47.     // Set the center constants
  48.     centerx = x/2;
  49.     centery = y/2;
  50.  
  51.     // Log
  52.     println("New Field initialized");
  53.   }
  54.  
  55.   // Initalize a new arrangement of blocks based on Tetris standards
  56.   // t - the type of formation desired to be created
  57.   void initializeShape(String t) {
  58.     for (int i=blocks.size()-1; i>=0; i--) {
  59.       if (blocks.get(i).bx==centerx && blocks.get(i).by==0) return;
  60.     }
  61.  
  62.     // Choose between arrangements
  63.     switch(t) {
  64.     default:
  65.     case "t": // T is the default formation
  66.       blocks.add(new Block(purple, centerx, 0));
  67.       blocks.add(new Block(purple, centerx, 1));
  68.       blocks.add(new Block(purple, centerx-1, 1));
  69.       blocks.add(new Block(purple, centerx+1, 1));
  70.       print("T");
  71.       break;
  72.     case "s":
  73.       blocks.add(new Block(red, centerx, 0));
  74.       blocks.add(new Block(red, centerx+1, 0));
  75.       blocks.add(new Block(red, centerx+2, 0));
  76.       blocks.add(new Block(red, centerx-1, 0));
  77.       print("S");
  78.       break;
  79.     case "z":
  80.       blocks.add(new Block(green, centerx, 0));
  81.       blocks.add(new Block(green, centerx, 1));
  82.       blocks.add(new Block(green, centerx+1, 1));
  83.       blocks.add(new Block(green, centerx-1, 0));
  84.       print("Z");
  85.       break;
  86.     case "i":
  87.       blocks.add(new Block(yellow, centerx, 0));
  88.       blocks.add(new Block(yellow, centerx, 1));
  89.       blocks.add(new Block(yellow, centerx, 2));
  90.       blocks.add(new Block(yellow, centerx, 3));
  91.       print("I");
  92.       break;
  93.     case "l":
  94.       blocks.add(new Block(orange, centerx, 0));
  95.       blocks.add(new Block(orange, centerx, 1));
  96.       blocks.add(new Block(orange, centerx, 2));
  97.       blocks.add(new Block(orange, centerx+1, 2));
  98.       print("L");
  99.       break;
  100.     case "j":
  101.       blocks.add(new Block(dblue, centerx, 0));
  102.       blocks.add(new Block(dblue, centerx, 1));
  103.       blocks.add(new Block(dblue, centerx, 2));
  104.       blocks.add(new Block(dblue, centerx-1, 2));
  105.       print("J");
  106.       break;
  107.     case "o":
  108.       blocks.add(new Block(lblue, centerx, 0));
  109.       blocks.add(new Block(lblue, centerx, 1));
  110.       blocks.add(new Block(lblue, centerx+1, 0));
  111.       blocks.add(new Block(lblue, centerx+1, 1));
  112.       print("O");
  113.       break;
  114.     }
  115.  
  116.     // Log
  117.     println(" Shape created.");
  118.   }
  119.  
  120.   void display() {
  121.     // Use the CENTER mode for easy calculation
  122.     rectMode(CENTER);
  123.  
  124.     // Fill colour to white
  125.     fill(color(255, 255, 255));
  126.  
  127.     // Draw a rectangle
  128.     // The center of the rectangle is in the center of the screen
  129.     // The width is (block size+3)*horizontal size of the board
  130.     // The height is (block size+3)*vertical size of the board
  131.     float w = (sizeb+3)*(sizex+1);
  132.     float h = (sizeb+3)*(sizey+2);
  133.     rect(width/2, height/2, w, h, 5);
  134.  
  135.     rectMode(CORNER);
  136.  
  137.     // Iterate through the blocks in the field
  138.     for (int i=0; i<blocks.size(); i++) {
  139.       // Set a few local variables to reduce confusion
  140.       float x = blocks.get(i).bx;
  141.       float y = blocks.get(i).by;
  142.       float s = sizeb;
  143.       color c = blocks.get(i).col;
  144.  
  145.       // Do some preliminary calculations
  146.       x = ((width/2)-(w/2))+(s+3)*x;
  147.       y = ((height/2)-(h/2))+(s+3)*y;
  148.  
  149.       fill(c);
  150.       rect(x, y, s, s);
  151.     }
  152.   }
  153.  
  154.   // Update the position of any blocks that need their position updated
  155.   void update() {
  156.     // Logic for dropping blocks ------------------------
  157.     // Amount of blocks that are falling
  158.     int falling=0;
  159.     boolean clearingLine=false;
  160.  
  161.     // Iterate through the blocks in the field
  162.     for (int i=blocks.size()-1; i>=0; i--) {
  163.       print("Block #"+i+" ");
  164.       print("x-"+blocks.get(i).bx+" y-"+blocks.get(i).by+" ");
  165.       // Whether to stop the block
  166.       boolean stop = false;
  167.  
  168.       // If the block is falling
  169.       if (blocks.get(i).fall) {
  170.  
  171.         // Add 1 to our falling counter
  172.         falling++;
  173.         print("falling ");
  174.  
  175.         // Check if the block is below the end of the screen
  176.         if (blocks.get(i).by>=sizey) stop=true;
  177.  
  178.         // Check if the block has a block below it
  179.         // Iterate through all blocks
  180.         for (int j=blocks.size()-1; j>=0; j--) {
  181.           // If the y value of j is below i
  182.           if (blocks.get(j).by==blocks.get(i).by+1) {
  183.             // If the x values are the same
  184.             if (blocks.get(j).bx==blocks.get(i).bx) {
  185.               // If j is not falling
  186.               if (!blocks.get(j).fall) {
  187.                 // Stop i from falling
  188.                 stop=true;
  189.                 print("below ");
  190.                 break;
  191.               }
  192.             }
  193.           }
  194.         }
  195.  
  196.         // Stop falling if stop is true
  197.         if (stop) blocks.get(i).fall = false;
  198.  
  199.         // Push the block's position down
  200.         if (!stop) blocks.get(i).by++;
  201.       }
  202.       println("");
  203.     }
  204.     moveDir = 0;
  205.  
  206.     // Check for lines to be cleared
  207.     if (falling>0)
  208.       clearingLine=true;
  209.  
  210.     // Logic for clearing lines --------------------
  211.     // If no blocks are falling
  212.     if (clearingLine) {
  213.       // Iterate through each line
  214.       for (int i=int(sizey); i>0; i--) {
  215.         print("Line #"+i+" ");
  216.         // How many blocks are on this line
  217.         int howMany=0;
  218.  
  219.         // Iterate through the blocks on the field
  220.         for (int j=0; j<blocks.size(); j++) {
  221.           // If the block is at this line, count it
  222.           if (blocks.get(j).by==i) howMany++;
  223.         }
  224.         print(howMany+" ");
  225.         // If there are more blocks on this line than the width of the field, clear this line
  226.         if (howMany>sizex) {
  227.           removeLine(i);
  228.           print("removed #"+i+" ");
  229.         }
  230.         //else if (falling==0) clearingLine = false;
  231.         println("");
  232.       }
  233.     }
  234.     // If no blocks are falling and we aren't clearing a line, add a new shape
  235.     if (!clearingLine && falling==0) initializeShape(calculateNextShape());
  236.   }
  237.  
  238.   // Remove a line of blocks from the field and update the blocks above to fall.
  239.   void removeLine(float line) {
  240.     linesCleared++;
  241.     // Iterate through all lines before the one to be removed
  242.     for (int i=0; i<line; i++) {
  243.       print("Line #"+i+" ");
  244.       // Iterate through the blocks on the field
  245.       for (int j=blocks.size()-1; j>=0; j--) {
  246.         // If the block is at the current line
  247.         if (blocks.get(j).by==i) {
  248.           int under=0;
  249.           // Iterate through the blocks on the field
  250.           for (int k=blocks.size()-1; k>=0; k--) {
  251.             under=0;
  252.             // If there is a block under this one
  253.             if (blocks.get(j).bx==blocks.get(k).bx &&
  254.               blocks.get(j).by==blocks.get(k).by-1) under++;
  255.           }
  256.           // If there are no blocks under this one, drop it
  257.           if (under<=0) blocks.get(j).fall=true;
  258.           print("drop #"+j+" ");
  259.         }
  260.       }
  261.       println("");
  262.     }
  263.  
  264.     // Iterate through the blocks on the field
  265.     for (int i=blocks.size()-1; i>=0; i--) {
  266.       // Remove blocks on the line to be removed
  267.       if (blocks.get(i).by==line) blocks.remove(i);
  268.     }
  269.   }
  270.  
  271.   // Select a random shape out of the available shapes.
  272.   String calculateNextShape() {
  273.     int chosenShape=0;
  274.     // If there are no shapes left in the array, re-initialize it
  275.     if (shapes.size()<=0) {
  276.       for (int i=1; i<=7; i++) {
  277.         shapes.add(i);
  278.       }
  279.     }
  280.     // Choose a random index in the array
  281.     chosenShape = int(random(float(shapes.size()-1)));
  282.  
  283.     // What to return based on the shape chosen
  284.     switch(shapes.get(chosenShape)) {
  285.     case 1:
  286.       return "t";
  287.     case 2:
  288.       return "s";
  289.     case 3:
  290.       return "z";
  291.     case 4:
  292.       return "i";
  293.     case 5:
  294.       return "l";
  295.     case 6:
  296.       return "j";
  297.     case 7:
  298.       return "o";
  299.     default:
  300.       return "";
  301.     }
  302.   }
  303.  
  304.   // Move the shape formation
  305.   // dir - the direction the shape should move
  306.   void moveShape(int dir) {
  307.     // Amount of blocks that are falling
  308.  
  309.     int falling=0;
  310.     // Iterate through the blocks in the field
  311.     for (int i=blocks.size()-1; i>=0; i--) {
  312.  
  313.       // If the block is falling, increment the counter
  314.       if (blocks.get(i).fall) falling++;
  315.     }
  316.  
  317.     // If there are 4 blocks falling (i.e a shape)
  318.     if (falling==4) {
  319.       // Iterate through the blocks in the field
  320.       for (int i=blocks.size()-1; i>=0; i--) {
  321.         // If the block is falling
  322.         if (blocks.get(i).fall) {
  323.           // If the block is outside the field
  324.           if (blocks.get(i).bx>=sizex) {
  325.             // Stop moving the block
  326.             moveDir=-1;
  327.           }
  328.           if (blocks.get(i).bx<=0) {
  329.             // Stop moving the block
  330.             moveDir=1;
  331.           }
  332.         }
  333.       }
  334.       // Move the falling blocks on the field
  335.       moveDir+=dir;
  336.       for (int i=blocks.size()-1; i>0; i--) {
  337.         // If the block should move horizontally, move it
  338.         if (moveDir!=0 && blocks.get(i).fall) blocks.get(i).bx+=moveDir;
  339.       }
  340.     }
  341.   }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment