Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GameScreen.cpp Source File

GameScreen.cpp

00001 #include "mbed.h"
00002 #include "N5110.h"
00003 #include "GameScreen.h"
00004 
00005 //initialise function, sets ball pos and creates platforms
00006 void GameScreen::Initialize()
00007 {
00008     init();         //power up screen and set brightness
00009     setBrightness(1.0);
00010     //set ball pos
00011     playerBall.x = maxX_/2;
00012     playerBall.y = maxY_ - (int)(maxY_/3 * 2) - 5;
00013 
00014     createAllPlatforms();
00015 }
00016 
00017 //draw platform
00018 //  ___________  ________  < y
00019 //               ^ x
00020 void GameScreen::drawPlatform(int x,int y)
00021 {
00022     for (int a = 0; a < 48; a ++) {
00023         for (int b = y; b < (y+platThickness_); b++) {
00024             //skip pixels of gap
00025             if (a > x && a < (x + platGapSize_))
00026                 break;
00027 
00028             //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
00029             if (b > (maxY_ - 1))
00030                 break;
00031 
00032             setPixel(b,a);
00033         }
00034     }
00035 }
00036 
00037 //erase platform
00038 void GameScreen::erasePlatform(int y)
00039 {
00040     for (int a = 0; a < 48; a ++) {
00041         for (int b = y; b < (y+platThickness_); b++) {
00042             //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
00043             if (b > (maxY_ - 1))
00044                 break;
00045 
00046             clearPixel(b,a);
00047         }
00048     }
00049 }
00050 
00051 //draw the player ball where (x,y) is top right corner.
00052 //      XXP <-- P(x,y)
00053 //     XXXX
00054 //     XXXX
00055 //      XX
00056 void GameScreen::drawBall()
00057 {
00058     //more elegant ways to do this...but this is most efficient
00059     setPixel(playerBall.y,playerBall.x+1);
00060     setPixel(playerBall.y,playerBall.x+2);
00061     setPixel(playerBall.y+1,playerBall.x);
00062     setPixel(playerBall.y+1,playerBall.x+1);
00063     setPixel(playerBall.y+1,playerBall.x+2);
00064     setPixel(playerBall.y+1,playerBall.x+3);
00065     setPixel(playerBall.y+2,playerBall.x);
00066     setPixel(playerBall.y+2,playerBall.x+1);
00067     setPixel(playerBall.y+2,playerBall.x+2);
00068     setPixel(playerBall.y+2,playerBall.x+3);
00069     setPixel(playerBall.y+3,playerBall.x+1);
00070     setPixel(playerBall.y+3,playerBall.x+2);
00071 }
00072 
00073 //draw the player ball where (x,y) is top right corner.
00074 void GameScreen::eraseBall()
00075 {
00076     clearPixel(playerBall.y,playerBall.x+1);
00077     clearPixel(playerBall.y,playerBall.x+2);
00078     clearPixel(playerBall.y+1,playerBall.x);
00079     clearPixel(playerBall.y+1,playerBall.x+1);
00080     clearPixel(playerBall.y+1,playerBall.x+2);
00081     clearPixel(playerBall.y+1,playerBall.x+3);
00082     clearPixel(playerBall.y+2,playerBall.x);
00083     clearPixel(playerBall.y+2,playerBall.x+1);
00084     clearPixel(playerBall.y+2,playerBall.x+2);
00085     clearPixel(playerBall.y+2,playerBall.x+3);
00086     clearPixel(playerBall.y+3,playerBall.x+1);
00087     clearPixel(playerBall.y+3,playerBall.x+2);
00088 }
00089 
00090 void GameScreen::createAllPlatforms()
00091 {
00092     for (int n = 0; n < numPlatforms_ ; n++) {
00093         //create new platform and add to allPlatform array.
00094         Platform *newPlatform = new Platform;
00095         newPlatform->id = n;
00096         newPlatform->x = rand() % (maxX_-platGapSize_ + 1) - 1;         //randomlly select platform location
00097         newPlatform->y = maxY_ - n*platSpacing_ - platThickness_ + 1;
00098         allPlatforms[n] = newPlatform;
00099     }
00100 }
00101 
00102 //Garbage cleanup, free all the platforms!
00103 void GameScreen::freeAllPlatforms()
00104 {
00105     for (int n = 0; n < numPlatforms_ ; n++) {
00106         delete allPlatforms[n];
00107     }
00108 }
00109 
00110 void GameScreen::drawAllPlatforms()
00111 {
00112     for (int n = 0; n < numPlatforms_ ; n++) {
00113         drawPlatform (allPlatforms[n]->x,allPlatforms[n]->y);
00114     }
00115 }
00116 
00117 void GameScreen::eraseAllPlatforms()
00118 {
00119     for (int n = 0; n < numPlatforms_ ; n++) {
00120         erasePlatform (allPlatforms[n]->y);
00121     }
00122 }
00123 
00124 void GameScreen::shiftAllPlatforms(int n )
00125 {
00126     for (int i = 0; i < numPlatforms_ ; i++) {
00127         if (allPlatforms[i]->y > (platThickness_+5+n))
00128             allPlatforms[i]->y = allPlatforms[i]->y - n;
00129         else {
00130             allPlatforms[i]->y =  maxY_ - platThickness_ + 1+6; //send back to bottom.
00131             allPlatforms[i]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap!
00132 
00133         }
00134     }
00135 }
00136 
00137 Platform GameScreen::nextClosestPlatform(int y)
00138 {
00139     int closestY = 100;
00140     Platform closestPlatform;
00141     //scan each platform and determin which is closest.
00142     for (int i = 0; i < numPlatforms_; i++) {
00143 
00144         if (((allPlatforms[i]->y - y) < closestY) && ((allPlatforms[i]->y - y) >  -1)) {
00145             closestY = allPlatforms[i]->y-y;
00146             closestPlatform = *allPlatforms[i];
00147         }
00148     }
00149     return closestPlatform;
00150 }
00151 
00152 //@Override of base class function
00153 //had to override and rewrite since N5110 is written to write horizontal this needs vertical
00154 // this function reads existing font5x7 and rotate matrix to become font7x5 matrix.
00155 //
00156 // for example the number 4 font5x7 = 0x18,0x14,0x12,0x7F,0x10
00157 //                          font7x5 = 0x02,0x06,0x10,0x12,0x1E,0x2,0x2
00158 //then it is printed!
00159 void GameScreen::printString(const char * str,int x,int y,bool invert)
00160 {
00161     //set all pixels in rows to black
00162     if (invert) {
00163         for (int a = 0; a < 48; a ++) {
00164             for (int b = y; b < (y+9); b++) {
00165                 setPixel(b,a);
00166             }
00167         }
00168     }
00169 
00170     //scan through string, rotating font5x7 array and printing charector.
00171     while (*str) {
00172 
00173         //each byte in row
00174         for (int i = 0; i < 5; i++) {
00175             //each bit in byte
00176             for (int b = 0; b < 7; b++) {
00177                 if (invert)
00178                     setPixel(y+b+1,x-i+1);
00179 
00180                 if (font5x7[(*str - 32)*5 + i] & (1<<b)) { //bitwise comparison to mask at desired pixel
00181                     setPixel(y+b+1,x-i+1);
00182                     if (invert)
00183                         clearPixel(y+b+1,x-i+1);
00184                 } else {
00185                     if (!invert)
00186                         clearPixel(y+b+1,x-i+1);
00187                 }
00188             }
00189         }
00190         str ++; //next char in string
00191         x -= 6;
00192     }
00193 }
00194 
00195 void GameScreen::displayStartScreen()
00196 {
00197     clear();
00198     printString("LET",maxX_ - 14,-1);
00199     printString("THE BALL",maxX_ - 2,7);
00200     printString("DROP!",maxX_ - 12,16);
00201     printString("This way",maxX_ - 2,30);
00202     printString("------->",maxX_ - 2,37);
00203     printString("TO PLAY!",maxX_ - 2,45);
00204     printString("by: ",maxX_ - 2,61);
00205     printString("Thomas",maxX_ - 9,69);
00206     printString("Davies",maxX_ - 9,76);
00207     refresh();
00208 }
00209 
00210 void GameScreen::displayInstructionScreen()
00211 {
00212     clear();
00213     printString("Hi Ball,",maxX_ - 2,-1);
00214     printString("You are",maxX_ - 2,7);
00215     printString("TRAPPED!",maxX_ - 2,15);
00216     printString("Hit the",maxX_ - 2,23);
00217     printString("roof and",maxX_ - 2,32);
00218     printString("you'll",maxX_ - 2,39);
00219     printString("surely",maxX_ - 2,47);
00220     printString("DIE!!",maxX_ - 2,55);
00221     printString("------->",maxX_ - 2,75);
00222     refresh();
00223 }
00224 
00225 void GameScreen::displayInstructionScreen2()
00226 {
00227     clear();
00228     printString("CONTROLS",maxX_ - 2,-1);
00229     printString("Use the",maxX_ - 2,15);
00230     printString("joystick",maxX_ - 2,23);
00231     printString("to move",maxX_ - 2,32);
00232     printString("left or",maxX_ - 2,39);
00233     printString("right.",maxX_ - 2,47);
00234     printString("Click to",maxX_ - 2,55);
00235     printString("Pause!",maxX_ - 2,63);
00236     printString("------->",maxX_ - 2,75);
00237     refresh();
00238 }
00239 
00240 void GameScreen::displayCountdown()
00241 {
00242     clear();
00243     Timer countdown;        //timer for countdown
00244     char buffer[10];
00245     countdown.start();
00246     printString("Survive!",maxX_ - 2,10);
00247     refresh();
00248 
00249     int prevTime = -1;
00250     //countdown from 5 to 1, in seconds.
00251     while (countdown.read() < 5) {
00252         if (floor(countdown.read()) > prevTime) {
00253             sprintf(buffer,"..%1.0f..",(5 - floor(countdown.read())));
00254             printString(buffer,maxX_ - 10,20 + 10*countdown.read());
00255             refresh();
00256             prevTime = countdown.read();
00257         }
00258     }
00259     countdown.stop();
00260 }
00261 
00262 void GameScreen::displayEndScreen(int lvl,int *cursor, bool isRecord)
00263 {
00264     clear();
00265     char buffer [10];
00266     sprintf(buffer,"lvl: %d",lvl);
00267     printString("GameOver",maxX_ - 2,-1);
00268     printString("   :(  ",maxX_ - 2,7);
00269     printString(buffer,maxX_ - 2,23);
00270     printString("Reached!",maxX_ - 2,32);
00271 
00272     //only display record option if player achieved record!
00273     if (isRecord)
00274         printString("Record",maxX_ - 8,55,cursor[0]);
00275 
00276     printString("View",maxX_ - 15,63,cursor[1]);
00277     printString("Restart",maxX_ - 5,71,cursor[2]);
00278     refresh();
00279 }
00280 
00281 void GameScreen::displayRecordScreen(int *cursor,char ltr1,char ltr2,char ltr3)
00282 {
00283     clear();
00284     printString("Enter",maxX_ - 2,-1);
00285     printString("Initials",maxX_ - 2,7);
00286 
00287     //letter entry
00288     char buffer[2];
00289     sprintf(buffer,"%c",ltr1);
00290     printString(buffer,maxX_ - 23,20,cursor[0]);
00291     sprintf(buffer,"%c",ltr2);
00292     printString(buffer,maxX_ - 23,32,cursor[1]);
00293     sprintf(buffer,"%c",ltr3);
00294     printString(buffer,maxX_ - 23,44,cursor[2]);
00295 
00296     printString("Click",maxX_ - 2,59);
00297     printString("when",maxX_ - 2,67);
00298     printString("finished",maxX_ - 2,75);
00299     refresh();
00300 
00301 }
00302 
00303 void GameScreen::displayHighScores(char *s_hs0,char *s_hs1,char *s_hs2,int *d_hs)
00304 {
00305     clear();
00306     printString("SCORES",maxX_ - 2,-1);
00307 
00308     char buffer[10];
00309 
00310     sprintf(buffer,"%s %d",s_hs0,d_hs[0]);
00311     printString(buffer,maxX_ - 2,15);
00312 
00313     sprintf(buffer,"%s %d",s_hs1,d_hs[1]);
00314     printString(buffer,maxX_ - 2,33);
00315 
00316     sprintf(buffer,"%s %d",s_hs2,d_hs[2]);
00317     printString(buffer,maxX_ - 2,51);
00318 
00319     printString("Click to",maxX_ - 2,68);
00320     printString("reset!",maxX_ - 2,76);
00321     refresh();
00322 }
00323 
00324 void GameScreen::displayPauseScreen(int *cursor,char isSound)
00325 {
00326     clear();
00327     printString("PAUSED",maxX_ - 2,7);
00328 
00329     char buffer[10];
00330     sprintf(buffer,"Sound? %c",isSound);
00331     printString(buffer,maxX_ - 2,30,cursor[0]);
00332     printString("Restart",maxX_ - 2,45,cursor[1]);
00333     printString("Return",maxX_ - 2,61,cursor[2]);
00334     refresh();
00335 }
00336