Control the Falling Blocks
- 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.
- 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
- randomly choose a block
- randomly generates a starting X coordinate
The Y coordinate always starts from 0, because this is where the block
begins to fall down.
- Inside the infinite loop, use
getch() to detect whether the player
presses a key.
- When the player presses the LEFT key, decrement the X coordinate by 1.
- When the player presses the RIGHT key, increment the X coordinate by 1.
- 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.)
- When the player presses 'q", the program ends.
- 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.
- Draw a border to indicate the region where the player can move a
block.
- 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.
- Each shape has different width, so you may wish to allocate
an array max_y[7] to calculate the width of each shape.