Tetris Game Report_SUN_LI

Published on December 2016 | Categories: Documents | Downloads: 26 | Comments: 0 | Views: 111
of 11
Download PDF   Embed   Report

Comments

Content

University at Buffalo

Department of Electrical Engineering
Spring 2015 Semester

Course: EE 379 – Embedded Systems and Applications
Instructor: Dr. Praveen Meduri

Title of the Lab: Tetris

LAB #:
Name:

Final Project

Instructor Section

Jianfeng Sun

………………..

Lab partner’s Name: Jiawen Li

………………..

Person # 1: 37068562

………………..

Person # 2: 50002578

Lab performed Date:
Report Submission Date:

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.

Figure of game 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.

The requirements are:

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.

Fig. Tetris game pieces
Movement
The movement of the game pieces are performed by controlling the joystick on the board. By pushing
up the joystick, the form of the piece changes; pressing it to the right moves the piece to the right, and
pressing left the game piece moves to the left. Joystick is a hardware interrupt that performs certain
task when it is pressed. Performing movement in the game does not stop the loop of the software.
Showing pieces on the screen is like painting, there is no erasing pixels, but by putting the same shape in
the same spot with the color same as the background gives the effect of erasing the shape. Therefore,
the logic of showing movement on the board is first to check if there is a block on one of the spot that
the next shape will locate, if not draw an identical shape at the exact same spot with the color same as
the back ground, change the coordinate and then redraw the shape. The following flowchart is an
example of moving left. Other movements or rotating are perform and detect in a similar manner.

Fig. Flowchart for moving left

ClearRow, update score, level and game speed
When all ten columns are filled in a row, this row should be cleared and all blocks above this row should
move down one row. This is perform by giving a variable which will increment by one if there is a block
on that column of the same row. If this variable ==10, we know that the blocks are filled in this row,
therefore we should clear row and update the score. Score=score+score*level. The starting level of the
game is 1, and when score has reached to a multiple of 50, level increment by 1. The speed of the game
increases when level increases. It started with an empty loop that loops 500,0000, and reduce by
100,0000 with every increased level.

Fig. Flowchart for clearing row and update score, level
Game over and reset
The game board has 20 rows and 10 columns, under the condition where the 20th row, third column has
block the game will end and display ‘Game Over’ and hold until the reset button has pressed. When the
reset button is pressed, all variable and matrix are redefine to its original value and the board will be
cleared. Game restarts.

Fig. Game over logic flowchart
When the board turns on, we see two fields on the screen. The game field that takes 2/3 of the screen
and 1/3 of the screen field which indicates the next game piece and shows the score and level at current
state. A game piece starts to appeal on the top of the game field and starts to fall until it lends on a

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 don’t 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

int shape=0,color=0,y0 = 8, x0 = 13,shapeForm=0,nextColor=1,nextShape=1,y = 20, x =
3,delayTime=5000000,timeChange=0,highestScore=0;
unsigned char s[5]={1};
unsigned char Level[5]={1};
int score = 0,level=1,numLine=0,gameover=1;

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 it’s 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.

void changeColor(int color)
{
switch (color){
case 0: GLCD_SetTextColor(Magenta);
break;
case 1: GLCD_SetTextColor(Navy);
break;
...

Color is a variable holding an integer value. It was predefined to 0 and increments
before a new shape generated. The function changeColor takes an integer value as argument
and use this value to switch to different case to change the color of the upcoming new
game piece.
void drawNext()
{
int index;
updateShape(shape);
for (index=0;index<=3;index++)
{
drawBlack(15+row[0][index],11+col[0][index]);

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 it’s released, the delayTime variable
will redefine back to 5000000, so the falling speed goes back to normal.

int checkLeft(int x,int y,int Shape,int shapeForm)
{
int index;
for (index=0;index<=3;index++)
{
if( boardMatrix[y+row[shapeForm][index]][x-­­1+col[shapeForm][index]]==1)
return 1;
void moveLeft()
{
if (x!=0&&checkLeft(x,y,shape,shapeForm)==0){
clearShape();
x=x-­­1;
drawShape();
}
Function checkLeft() was called in function moveLeft. Checkleft() function checks in the
boardMatrix if all four blocks of this shape one column unit to the left has a value of 1.
If yes, return 1 to indicate that this game piece cannot move left, or return a 0 when
this is doable. Other moveRight and rotate are working in a similar manner. Except that
rotateShape will check the spots of the new shape instead of moving the game piece left
or right.
void checkLine()
{
int numBlock,col,j,i,X,Y,loop;
Y=y;
for (loop=0;loop<4;loop++){
numBlock=0;
for (col=0;col<10;col++){
if (boardMatrix[Y][col]==1){
numBlock++;}
if (numBlock==10){

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 0’s 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.

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close