CD5250 - Objektorienterad programutveckling med C++, 5p
Projektuppgift - Tetris
``Tetris is a game designed by the Russians to slow down research
in the west"
First, read through the material on ObjectWindows. Secondly, define
what classes you have, (roughly) what data each class holds, and how its
interface looks. Do this before you code. Part of your design process
should include a lot of hand simulation; that is, stepping through your
design to make sure that it deals with every last detail. Remember to look
for classes that share attributes, behaviors, or interfaces and make a
superclass for them. Try to avoid unnecessary switch-statements
by exploiting polymorphism and virtual methods. Once you are satisfied
with your design, make sure to show it to the assistant during lab hours.
Unless you've been marooned on an island for the last ten years, you've
probably played (or seen someone play) Tetris. If you haven't, try the
demo version in the class directory to get a good feel for how the game
In Tetris, there are seven different pieces that can be rotated, dropped,
or moved to the left or right.
To rotate a piece, you can apply the following formula to each of its squares:
x' = xc + yc - y
y' = yc - xc + x
where (xc, yc) are the pivot coordinates.
The board is 10 squares wide and usually around 25 squares high. Pieces
are generated randomly at the top and fall down towards the bottom one
square at the time. While falling, the player can use the keys "J", "K",
"L" and space to move the piece left, rotate it, move it right, or drop
After a while, pieces begin to accumulate down at the bottom. Whenever
a row is full, it is cleared, and the squares above ``fall down''. Given
that the longest piece is four squares, the maximum number of rows you
can clear with one piece is four. The game ends when the next piece can't
fit on the board.
There's no standard way of counting score, as far as I know, so you
may implement your own method. In the demo version, each piece is alloted
one point, plus one point for every cleared row.
Your assignment is to write an incredibly addictive Tetris game, using
ObjectWindows, without getting addicted yourself. Some of the things you
will have to consider:
Start small. First, get one piece to simply fall down the board.
Then, get it to shift left and right, and finally, make it rotate and drop.
Once you have all of this working for one piece, it should be fairly simple
to get the other pieces to exhibit the same behaviors. Finally, implement
the detection and removal of horizontal rows, the updating of the board,
score keeping, and the end of the game scenario.
How to randomly create different types of pieces;
How each piece will fall, rotate, move to the left or right;
How each piece will draw itself;
How each piece will keep track of its current location;
How each piece will check if its desired move is legal, that is, not moving
in to an already occupied square and within the edges of the board;
How to handle user input;
How the board will keep track of where the pieces have fallen;
How the board will check for horizontal rows;
How to update the board after a horizontal row disappears;
How to check for the end of the game.
When implementing the data structure for the board, it is a good idea
to make extra room for "padding"; that is, to store the floor and walls
of the board in the array as well. These extra squares eliminate the need
for boundary checking in the 2-D array because they are always occupied.
Of course, the padding shouldn't be shown on the screen. One thing you
will have to think about is how thick the padding should be.
Also remember that you need to redraw your window if your application
receives the event EV_WM_PAINT, for instance if your window gets
partially obscured and then re-exposed.
Bells & Whistles.
If you have extra time, here are some extra features you might want
Make the pieces drop progressively faster;
Allow the player to pause and unpause the game;
Allow the player to restart the game;
Allow the player to save and load games;
Allow the player to choose HELP in the menu bar.
Use different colors for the pieces;
Maintain a high-score list.
What to turn in.
Schedule a time with your assistant, preferably during lab hours, to
demo your game. Hand in a complete report describing your work and a program
listing (stapled) with your name on it.