Library to control the Tetris game area
Diff: Tetris.cpp
- Revision:
- 3:af0a7a4464e5
- Parent:
- 2:56dc50270349
- Child:
- 4:e4952be370d3
--- a/Tetris.cpp Thu Apr 07 10:38:15 2016 +0000 +++ b/Tetris.cpp Thu Apr 07 15:42:35 2016 +0000 @@ -9,11 +9,78 @@ #include "Tetris.h" N5110 screen (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); - +int tetrominoe[7][4] = {{0x00F0,0x4444,0x0F00,0x2222},{0x0071,0x0226,0x0470,0x0322},{0x0074,0x0622,0x0170,0x0223},{0x0033,0x0033,0x0033,0x0033},{0x0072,0x0262,0x0270,0x0232},{0x0036,0x0462,0x0360,0x0231},{0x0063,0x0264,0x0630,0x0132}}; + Tetris::Tetris(){ pieceposition[0] = 4; pieceposition[1] = 0; orientation = 0; + shapebagcounter = 0; + currentshape = 3; + + score = 0; + completedlines = 0; + level = 1; + gamespeed = 0.8; + + musiccounter = 0; +} + +void Tetris::gameInfo(int score,int level,int next) +{ + + //outputs the level + char buffer[6]; + int length = sprintf(buffer,"%d",level); + if(length <=6) { + screen.printString(" ",0,1); + screen.printString(buffer,0,1); + } + //outputs the score + length = sprintf(buffer,"%d",score); + if(length <=6) { + screen.printString(" ",0,3); + screen.printString(buffer,0,3); + } + + for(int i = 0; i <4; i++) { //clears the next piece shape before the new one is displayed + for(int j = 0; j <2; j++) { + clearGamePixel(-6+i,12+j); + } + } + piecePlace(-6,12,tetrominoe[next][0]) ; +} + + + +void Tetris::gameSetup() +{ + + //drawRect(int x0,int y0,int width,int height,int fill) + screen.printString("Level:",0,0); + screen.printString("Score:",0,2); + screen.printString("0",0,1); + screen.printString("Next",0,4); + + + //creats the game boarder + screen.drawRect(40,0,33,47,0); + screen.drawRect(41,1,31,46,0); + screen.refresh(); + +//fill gamearea add the walls + + for(int i = 0; i<12; i++) { + for(int j = 0; j<16; j++) { + gamearea[i][j] = 1; + } + } + + for(int i = 1; i<11; i++) { + for(int j = 0; j<15; j++) { + gamearea[i][j] = 0; + } + } } void Tetris::gamePixel(int x,int y){ @@ -75,3 +142,221 @@ } } } + +bool Tetris::movePossible(int xpos, int ypos, int orientation) +{ + + if (orientation > 3) { + orientation = 0; + } + int count = 0; + + for(int y = 0; y < 4; y++) { + for(int x = 0; x < 4; x++) { + + int bit = tetrominoe[currentshape][orientation] & (1<<count); //find the bit of each pixel + + count ++; + + if ((bit !=0) && (gamearea[xpos+x+1][ypos+y]!=0)) { + return false; + } + } + } + + return true; +} + +void Tetris::newpiece() +{ + orientation = 0; //set the orientation back to zero + pieceposition[0] = 4; //sets the position back to the top + pieceposition[1] = 0; + + //clearGame(); + //setGameArea(); + + currentshape = nextshape[shapebagcounter]; + piecePlace(pieceposition[0],pieceposition[1],currentshape);//places the new piece + + + if(shapebagcounter > 5) { //when counter becomes 6 + newShapeBag(); //a new bag is created + shapebagcounter = 0; + nextpiece = nextshape[0]; + } else { + nextpiece = nextshape[shapebagcounter+1]; + shapebagcounter++; //incrimenst the counter + } + + gameInfo(score,level,nextpiece); +} + +void Tetris::newShapeBag() +{ + + bool match = false; + int newrandomnumber; + + nextshape[0] = rand()%7; //creates first random number + + for(int i = 1; i < 7; i++) { + + do { + newrandomnumber = rand()%7; + + match = false; //set to false to end the do while if no match is found + + for(int j = 0; j < i; j++) { + + if (newrandomnumber == nextshape[j]) { //checks to see if number has already been chosen + match = true;// continue do while if a match is found + break; //end the for loop that is checking for a match + } + } + } while(match); + + nextshape[i] = newrandomnumber; //set new found number into the newshape array + } +} + +void Tetris::checkCompleteLine() +{ + + int rowscomplete[4] = {0,0,0,0}; //max of 4 rows can be completed with a single block + int rowscompletecounter = 0; //counter to place complete row in the array + int sum = 0; + bool rowtoremove = false; + for(int y = 0; y <15; y++) { //searches through all the rows + + for(int x = 1; x<11; x++) { + if(gamearea[x][y] == 1) { + sum++; //counts how many filled cells + } + } + if(sum == 10) { //if all 10 cells are full + //pc.printf("%d complete \n",y); + rowscomplete[rowscompletecounter] = y; //sets it as a completed row + rowscompletecounter++; + rowtoremove = true; + } + sum = 0; + } + + + if(rowtoremove) {//runs if a row is complete + + /* + int prevlevel = level; + level = int((completedlines/10)+1); + + + if (prevlevel != level) { + gamespeed = 0.8606*exp(-0.073*level); + + ticker.detach(); + setTicker(gamespeed); + } + */ + removeCompleteLines(rowscomplete); + } + completedlines += rowscompletecounter; + if(rowscompletecounter == 1) { + score += 100; + } else if(rowscompletecounter == 2) { + score += 250; + } else if(rowscompletecounter == 3) { + score += 500; + } else if(rowscompletecounter == 4) { + score += 1000; + } +} + +void Tetris::removeCompleteLines(int rowscomplete[4]) +{ + for(int i = 0; i<4; i++) { //loops throught the max 4 lines that have been completed + if(rowscomplete[i] >0) { + + for(int j = 1; j<11; j++) { //delete complete row + gamearea[j][rowscomplete[i]] = 0; + } + + for(int j = rowscomplete[i]; j>0; j--) { //runs through all the rows to copy, j starts at deleted row + for(int k = 1; k<11; k++) { //runs through all the numbers in the row + gamearea[k][j] = gamearea[k][j-1]; + } + } + for(int j = 1; j<11; j++) { //add zeros into the top line + gamearea[j][0] = 0; + } + + } + } + setGameArea(); +} + + +void Tetris::pieceToGameArray() +{ + int count = 0; + for(int y = 0; y<4; y++) { + for(int x = 0; x<4; x++) { + int bit = tetrominoe[currentshape][orientation] & (1<<count); + + if (bit) { + gamearea[pieceposition[0]+x+1][pieceposition[1]+y]=1; + } + count++; + } + } + +} + +void Tetris::setGameArea() +{ + clearGame();//clears the area + for(int x = 1; x<11; x++) { + for(int y = 0; y<15; y++) { + if(gamearea[x][y] == 1) { + gamePixel(x-1,y); //x-1 because the gamearea array has 1's around the outside + } + } + } +} + +void Tetris::movePieceRight() +{ + if(movePossible(pieceposition[0]-1,pieceposition[1],orientation)) { + movePiece(1, 0,0); + } +} + +void Tetris::movePieceLeft() +{ + if(movePossible(pieceposition[0]+1,pieceposition[1],orientation)) { + movePiece(-1, 0,0); + } +} +void Tetris::spinPiece() +{ + if(movePossible(pieceposition[0],pieceposition[1],orientation+1)) { + + movePiece(0, 0,1); + } +} + +void Tetris::movePieceDown() +{ + movePiece(0, 1,0); +} + +void Tetris::movePiece(int x, int y,int spin){ + pieceClear(pieceposition[0],pieceposition[1],tetrominoe[currentshape][orientation]); + pieceposition[0]= pieceposition[0] - x; + pieceposition[1]= pieceposition[1] + y; + orientation = orientation + spin; + if (orientation > 3){ + orientation = 0; + } + piecePlace(pieceposition[0],pieceposition[1],tetrominoe[currentshape][orientation]); + } \ No newline at end of file