Tetris in Processing


We will summarize what we have learned so far by creating tetris in processing. Below is the code after Friday’s meeting. Some of the logic changed and the ‘spawn’ function and arrays were implemnted. Make sure you try implementing those yourself before copy-pasting the code.

/*int blockX = 125;
int blockY = 125;
int blockSz = 50;
int defaultBlockSpeed = 1;
int blockSpeed = 1;
boolean canMoveY = true;
boolean canMoveLeft = true;
boolean canMoveRight = true;
*/

//The 'final' keyword means that whatever value is assigned is constant, so the variable cannot be modified

//General Block Variables
final int NUM_BLK_ATTRIBS = 8;
final int BLK_DEFAULT_SZ = 50;
//Block Struct Indices
final int BLK_SZ = 0;
final int BLK_X = 1;
final int BLK_Y = 2;
final int BLK_DEFAULT_SPEED = 3;
final int BLK_SPEED = 4;
final int BLK_Y_STATE = 5;
final int BLK_LEFT_STATE = 6;
final int BLK_RIGHT_STATE = 7;
//Game Loop Block Counter
int ACTIVE_BLOCK = 0;
//Array of game blocks
int[][] blocks = new int[2][NUM_BLK_ATTRIBS];

//Function that creates formatted block data given spawn data
int[] spawn_block(int size, int xPos, int yPos, int defaultSpeed)
{
  int[] blockData = new int[8];
  //Set block dimensions
  blockData[BLK_SZ] = size;
  blockData[BLK_X] = xPos;
  blockData[BLK_Y] = yPos;
  //Set block's active speed to its default speed
  blockData[BLK_DEFAULT_SPEED] = defaultSpeed;
  blockData[BLK_SPEED] = defaultSpeed;
  //Set block's movement states to 1 (movement allowed)
  blockData[BLK_Y_STATE] = 1;
  blockData[BLK_LEFT_STATE] = 1;
  blockData[BLK_RIGHT_STATE] = 1;
  return blockData;
}

void setup()
{
  size(600, 800);
  frameRate(24);
  //Give game 2 blocks to work with
  blocks[0] = spawn_block(20, 100, 100, 1);
  blocks[1] = spawn_block(100, 300, 100, 1);
}

void draw()
{
  //Make sure active block index is valid
  if (ACTIVE_BLOCK < 0 || ACTIVE_BLOCK == blocks.length)
    exit();
  
  background(100, 100, 100);
  
  //Get active block based on active index and generated data
  int[] activeBlock = blocks[ACTIVE_BLOCK];
  
  //Align block to grid based on its size
  if (activeBlock[BLK_X] % activeBlock[BLK_SZ] != 0)
    activeBlock[BLK_X] += activeBlock[BLK_X] % activeBlock[BLK_SZ];
  
  //Set the block movement states based on its position
  activeBlock[BLK_Y_STATE] = (activeBlock[BLK_Y] + activeBlock[BLK_SZ] < height) ? 1 : 0;
  activeBlock[BLK_LEFT_STATE] = (activeBlock[BLK_X] > 0 && activeBlock[BLK_Y_STATE] == 1) ? 1 : 0;
  activeBlock[BLK_RIGHT_STATE] = (activeBlock[BLK_X] + activeBlock[BLK_SZ] < width && activeBlock[BLK_Y_STATE] == 1) ? 1 : 0;
  
  //Check User Input
  if (keyPressed == true)
  {
    //Move block left on left arrow press, if possible
    if (keyCode == LEFT && activeBlock[BLK_LEFT_STATE] == 1)
      activeBlock[BLK_X] -= activeBlock[BLK_SZ];
    //Move block right on right arrow press, if possible
    else if (keyCode == RIGHT && activeBlock[BLK_RIGHT_STATE] == 1)
      activeBlock[BLK_X] += activeBlock[BLK_SZ];
  
    //Move block down quickly on down arrow press, if possible
    if (keyCode == DOWN && activeBlock[BLK_Y_STATE] == 1)
      activeBlock[BLK_SPEED] = activeBlock[BLK_DEFAULT_SPEED] + activeBlock[BLK_SZ];
  }
  //Revert speed changes if no user input
  else
    activeBlock[BLK_SPEED] = activeBlock[BLK_DEFAULT_SPEED];
  
  //Move block down continuously if not at the bottom
  if (activeBlock[BLK_Y_STATE] == 1)
    activeBlock[BLK_Y] += activeBlock[BLK_SPEED];
  //Handle block at the bottom
  else
  {
    //Move index onto next block for next iteration of game loop
    ACTIVE_BLOCK++;
    
    //Position block correctly at bottom if necessary
    if (activeBlock[BLK_Y] + activeBlock[BLK_SZ] > height)
      activeBlock[BLK_Y] = height - activeBlock[BLK_SPEED];
  }
  
  //Draw the block
  fill(255);
  rect(activeBlock[BLK_X], activeBlock[BLK_Y], activeBlock[BLK_SZ], activeBlock[BLK_SZ]);   

  //Code for block without structured data and 'spawn' method
  /*
  if (blockX % blockSz != 0)
    blockX += blockX % blockSz;
  
  canMoveY = (blockY + blockSz < height);
  canMoveLeft = (blockX > 0 && canMoveY);
  canMoveRight = (blockX + blockSz < width && canMoveY);
  
  if (keyPressed == true)
  {
    if (keyCode == LEFT && canMoveLeft)
      blockX -= blockSz;
    else if (keyCode == RIGHT && canMoveRight)
      blockX += blockSz;
      
    if (keyCode == DOWN && canMoveY)
      blockSpeed = defaultBlockSpeed + blockSz;
  }
  else
    blockSpeed = defaultBlockSpeed;
    
  if (canMoveY)
    blockY += blockSpeed;
  else if (blockY + blockSz > height)
    blockY = height - blockSz;
    
  rect(blockX, blockY, blockSz, blockSz);
  */
}