Tetris Game Report - SUN - LI
Tetris Game Report - SUN - LI
LAB #:
Name:
Final Project
Instructor Section
Jianfeng Sun
..
..
Person # 1: 37068562
..
Person # 2: 50002578
5/9/15
5/12/15
Total: . /100
Introduction:
The goal of the project is to implement the basic game Tetris. Tetris is a classic tile
matching game originally released in 1984 which including the random sequence of game
pieces falling down the playing field. The game starts with an empty board drawn. And the
board is typically 10x20 squares. A randomly chosen Tetris piece from the seven possible
shapes is drawn at the top of the board.
The piece starts falling at regular intervals-one square at a time. Dropping a piece
means that the piece will fall down until it can no longer move and also no longer to rotate and
move it in any other direction. There are several rules that we had to satisfied, such as when a
piece hits another piece, it stops moving and a new piece appears at the top of the board, if a
new piece can no longer be placed at the top of the board, the game ends and a Game Over
message should be displayed the on LED display, whenever 10 rows are cleared, the level
should increment and the pieces should start falling faster, etc. We set the arrow keys to move
and rotate the pieces-left, right by 1 square respectively. And up key for rotating the
piece, down for accelerating the piece, and a RESET key separately. There are some specific
designs we have to complete and there are some extra credit steps to make the game more
interesting and better operating.
Solution&Results
The following figure is the flowchart of the project
Flow chart
Initiate board
Before starting the game, there are some parameters we have to define and some variable we have to
declare for future use. The initiate board step declares and defines all the matrixes, variable for different
purpose, also draws the border line of the game to separate the board from gaming screen and score,
next game piece screen.
Game Pieces&Forms
In Tetris, there are total of seven shapes, and there are different form of a game piece to make the
game more interesting. The following figure shows all the shapes in Tetris. In this project, we have used
all seven game pieces in the game. We done so by putting each game piece and the corresponding form
of the piece in two 4X4 matrix that indicates the x and y axis coordinates. A drawBlock() function draws
a 16X16 pixels square on the screen to show all game pieces and different forms.
block or bottom. By pressing left, right, top, down on the joystick performs move left, move right, rotate
and falling faster correspondingly. When a row with all 10 blocks filled, that row disappears and all the
blocks above it fall, score increments by 10. When score is a multiple of 100, level increases and game
pieces start to fall faster. When a new piece can no longer appeal normally on the top of the screen,
game stopped and Game Over is displayed at the center of the screen along with the score and level.
By pushing the third button below the screen, game restarts.
C code
There many user defined function in our code to perform some specific task in this project. In
sampleSetup.c, we dont have any variables but called user defined function to start the game. And
perform some task when an interrupt occurs. All our functions are defined on Cris_Utils.c
These are the global variable that will be used in the entire program. Most of the variables are defined
type int and will hold a value that indicates shape, nextShape, color or value of row, column, etc.
void initiateBoard()
{
GLCD_Init();
GLCD_Clear(Black);
drawNext();
drawLine();
GLCD_SetTextColor(Blue);
GLCD_SetBackColor(Black);
The initiateBoard() function draws the boundary line which separates the game board and score. Also
display the started score=0 and level=1 and nextShape.
void drawBlock( int x0, int y0)
{
int x,y;
for (x=x0*16;x<=x0*16+15;x++)
{
for (y=y0*16;y<=y0*16+15;y++)
{
GLCD_PutPixel(x, y);
}
}
for (x=x0*16;x<=x0*16+15;x++)
{
for (y=y0*16;y<=y0*16+15;y++)
{
if(y==y0*16+15||x==x0*16+15||y==y0*16+1||x==x0*16+1)
{
GLCD_SetTextColor(White);
GLCD_PutPixel(x, y);
}
}
}
}
The game board is 20x10, therefore when setting the indicated x and y value, we need to multiply by 16
to get the exact point at the board since the board itself is 320x160 pixels. Needed to transfer 320x160
to 20x10. By calling the drawBlock(x,y) function we can draw a 16x16 square at a specific x,y point
int boardMatrix[20][11]={{0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,1},
int boardColor[20][10]={{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
The boardMatrix is a 20x11 matrix initiated all zeros except last column 1 to indicate the right most
boundary. The use of this matrix is to hold a value of 1 when there is a block in that specific spot and
boardColor is a 20x10 matrix holding the value of the blocks corresponding color.
int tianR[4][4] = {{1,1,0,0},{1,1,0,0},{1,1,0,0},{1,1,0,0}};
int tianC[4][4] = {{0,1,1,0},{0,1,1,0},{0,1,1,0},{0,1,1,0}};
Thess 4x4 matrix holds the x and y value of how to form a game piece and its form. R[][] and C[][]
should be corresponded, and all four values should be drawn when creating game piece.
void updateShape(int id)
{
int x,y;
if (id == 0)
{
for(x=0;x<=3;x++)
{
for(y=0;y<=3;y++)
{
row[x][y] = tianR[x][y];
col[x][y] = tianC[x][y];
}
}
}
There are 7 cases in this updateShape function. Id is a random generated number from 0 to
6 to create different shape each time. When id changes, we call this function to change
the shape by storing the x,y combination of a shape to row[][],col[][] matrix.
The drawnext() function draws the next shape at the upper right corner of the screen.
void startGame()
{
int delay;
for (delay=0;delay<delayTime;delay++);
clearShape();
y=y-1;
drawShape();
delayTime = 5000000--timeChange;
if (y==0||checkdown(x,y,shape,shapeForm)==1){
{
updateMatrix();
gameOver();
checkLine();
shape=nextShape;
This is the function that is called in main. This function sets the game time and creates
game pieces. Under certain condition, this game piece will fall row by row by calling
clearShape() function and decrement y, redraw the shape by calling drawShape function.
Clearshape function is similar to drawshape function except it changes the color to black
so we can erase the shape by drawing black. delayTime = 5000000--timechange. Timechange
was originally defined as 0. When level goes up, timeChange increases and delayTime
decreases to make the game more difficult. Under certain condition, the falling piece
will stop and check if a row has filled or game is over. If not, Reset y=19 and x=3 so
that an updated shape id and color will generated and new game piece will fall from the
starting spot.
The fastDown function was called when pressing down on the joystick. This basically sets
delayTime =100000 whenever down is pressing. Once its released, the delayTime variable
will redefine back to 5000000, so the falling speed goes back to normal.
This checks if all 10 columns of a row is filled by declaring a valuable called numBlock
and initiated to 0 every time we check on a different row. If a column on that row has
drawn on the board, increment numBlock. When numBlock==10, we know that this row is
filled. Therefore we need to clear the row and update the score.
for (j=Y;j<20;j++){
for (i=0;i<10;i++){
boardColor[j][i]=boardColor[j+1][i];
boardMatrix[j][i]=boardMatrix[j+1][i];}}
for (col=0;col<10;col++){
boardMatrix[19][col]=0;}
These code are to update the matrix. When a row is cleared, all the elements above this
row move down one row. And add a new line of 0s at row[19].
void drawMatrix()
{
int row,col;
for (row=0;row<19;row++){
for (col=0;col<10;col++){
if (boardMatrix[row][col]==1)
{
changeColor(boardColor[row][col]);
drawBlock(row,col);
}
}
}
}
void clearBoard()
{
int row,col;
for (row=0;row<19;row++){
for (col=0;col<10;col++){
if (boardMatrix[row][col]==1)
{
drawBlack(row,col);
}
}
}
}
These two function is to clear the board when a row has been cleared and redraw all the
blocks that has a value of 1 in the boardmatrix. One important of this drawmatrix is that
when drawing the blocks, the corresponding color will be used to change color.
void gameOver()
{
if (boardMatrix[19][3]==1)
{
gameover=1;
GLCD_SetBackColor(Black);
GLCD_SetTextColor(Red);
GLCD_DisplayString(4,4, "Game Over!!");
while(gameover)
{}
}
}
When there is a block drawn at boardMatirx[20][3], the game is over. Game over will
display at the center of the game and hold for ever until the reset button has pressed.
When the condition boardMatrix[19][3] holds true, the valuable gameover sets to 1, and
while this gameover holds a value 1, this loop goes forever.
void resetGame()
{
int row,col;
shape=0,color=0,y0 = 8, x0 = 13,shapeForm=0,nextColor=1,nextShape=1,y = 20, x =
3,delayTime=5000000,timeChange=0;
score = 0,level=1,numLine=0;
for (row=0;row<20;row++)
{for(col=0;col<10;col++){
boardMatrix[row][col]=0;
boardColor[row][col]=0;}}
gameover=0;
initiateBoard();
}
This resetGame() function was called in buttonpressed interrupt. This changes the
valuable gameover to 0 and reinitialize all the global variables as well as the board and
color matrix. Since the variable gameover now has a value of 0, the gameover() function
exits the loop and main continues looping.
Division of work
Me and my partner work together on all the steps to complete this project. From
game logic, code writing and the report, etc.
Conclsion
In this project, we intended to implement a Tetris game based on LPC 1768, and displayed on
the LED screen. Not only we set the control of movement of the pieces by the arrow keys left and
right, accelerate by down arrow key and change the shapes by 90 degree by up key, a separate
key for RESET, but also made the pieces with different colors to easy to recognized. We learned how
to use matrix to help us on the design and to make detections.