Control the Falling Blocks

  1. This homework requires you to extend your TETRIS program. With the draw_block() and erase_block() functions which you developed, you are now ready to control them while they are falling down.
  2. Re-write your main program. Instead of using a for-loop to iterate from Block 1 to Block 7, now we want to use an infinite loop in which it
    1. randomly choose a block
    2. randomly generates a starting X coordinate
    The Y coordinate always starts from 0, because this is where the block begins to fall down.
  3. Inside the infinite loop, use getch() to detect whether the player presses a key.
    1. When the player presses the LEFT key, decrement the X coordinate by 1.
    2. When the player presses the RIGHT key, increment the X coordinate by 1.
    3. When the player presses the DOWN key or SPACEBAR, run a for-loop to let the block quickly fall down to the bottom. (In this version, the block will simply disappear after it reaches the bottom. In next version, the block will remain there and later blocks will be stacked upon it.)
    4. When the player presses 'q", the program ends.
  4. You may wish to call the timeout() function in NCURSES so that if the player did not press any key, the block can still keep falling down (e.g. after 300ms). Note that in this case, you don't need the usleep() function anymore.
  5. Draw a border to indicate the region where the player can move a block.
    TETRIS (4)
  6. You need to prevent a block from being moved out of the region, so you must check whether there is still space for a block to move, when the player presses the LEFT or the RIGHT key.