Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

Committer:
AppleJuice
Date:
Wed Apr 29 23:45:08 2015 +0000
Revision:
12:9ba4b21f8220
Parent:
11:c6c88617f7e7
Child:
13:7071a14b2fab
record entry screen complete;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AppleJuice 0:c2c1df1163f1 1 #include "mbed.h"
AppleJuice 0:c2c1df1163f1 2 #include "N5110.h"
AppleJuice 0:c2c1df1163f1 3 #include "GameScreen.h"
AppleJuice 0:c2c1df1163f1 4
AppleJuice 5:d395a7134278 5
AppleJuice 5:d395a7134278 6
AppleJuice 1:3305d7e44880 7 //need to extend on initialization to set some variables
AppleJuice 8:ebddb721f1ee 8 void GameScreen::Initialize()
AppleJuice 1:3305d7e44880 9 {
AppleJuice 1:3305d7e44880 10 init();
AppleJuice 5:d395a7134278 11
AppleJuice 8:ebddb721f1ee 12 playerBall.x = maxX_/2;
AppleJuice 8:ebddb721f1ee 13 playerBall.y = maxY_ - (int)(maxY_/3 * 2) - 5;
AppleJuice 8:ebddb721f1ee 14
AppleJuice 8:ebddb721f1ee 15 createAllPlatforms();
AppleJuice 1:3305d7e44880 16 }
AppleJuice 1:3305d7e44880 17
AppleJuice 1:3305d7e44880 18 //draw platform
AppleJuice 1:3305d7e44880 19 // ___________ ________ < y
AppleJuice 1:3305d7e44880 20 // ^ x
AppleJuice 1:3305d7e44880 21 void GameScreen::drawPlatform(int x,int y)
AppleJuice 2:d4402bc3dd45 22 {
AppleJuice 1:3305d7e44880 23 for (int a = 0; a < 48; a ++)
AppleJuice 1:3305d7e44880 24 {
AppleJuice 2:d4402bc3dd45 25 for (int b = y; b < (y+platThickness_); b++)
AppleJuice 1:3305d7e44880 26 {
AppleJuice 2:d4402bc3dd45 27 //skip pixels of gap
AppleJuice 2:d4402bc3dd45 28 if (a > x && a < (x + platGapSize_))
AppleJuice 2:d4402bc3dd45 29 break;
AppleJuice 2:d4402bc3dd45 30
AppleJuice 2:d4402bc3dd45 31
AppleJuice 2:d4402bc3dd45 32
AppleJuice 2:d4402bc3dd45 33 //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
AppleJuice 2:d4402bc3dd45 34 if (b > (maxY_ - 1))
AppleJuice 2:d4402bc3dd45 35 break;
AppleJuice 2:d4402bc3dd45 36
AppleJuice 1:3305d7e44880 37 setPixel(b,a);
AppleJuice 1:3305d7e44880 38 }
AppleJuice 1:3305d7e44880 39 }
AppleJuice 2:d4402bc3dd45 40 }
AppleJuice 2:d4402bc3dd45 41
AppleJuice 2:d4402bc3dd45 42 //erase platform
AppleJuice 2:d4402bc3dd45 43 void GameScreen::erasePlatform(int y)
AppleJuice 2:d4402bc3dd45 44 {
AppleJuice 2:d4402bc3dd45 45 for (int a = 0; a < 48; a ++)
AppleJuice 2:d4402bc3dd45 46 {
AppleJuice 2:d4402bc3dd45 47 for (int b = y; b < (y+platThickness_); b++)
AppleJuice 2:d4402bc3dd45 48 {
AppleJuice 2:d4402bc3dd45 49 //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
AppleJuice 2:d4402bc3dd45 50 if (b > (maxY_ - 1))
AppleJuice 2:d4402bc3dd45 51 break;
AppleJuice 2:d4402bc3dd45 52
AppleJuice 2:d4402bc3dd45 53 clearPixel(b,a);
AppleJuice 2:d4402bc3dd45 54 }
AppleJuice 2:d4402bc3dd45 55 }
AppleJuice 2:d4402bc3dd45 56 }
AppleJuice 2:d4402bc3dd45 57
AppleJuice 2:d4402bc3dd45 58 //draw the player ball where (x,y) is top right corner.
AppleJuice 2:d4402bc3dd45 59 // XXP <-- P(x,y)
AppleJuice 2:d4402bc3dd45 60 // XXXX
AppleJuice 2:d4402bc3dd45 61 // XXXX
AppleJuice 2:d4402bc3dd45 62 // XX
AppleJuice 5:d395a7134278 63 void GameScreen::drawBall()
AppleJuice 2:d4402bc3dd45 64 {
AppleJuice 2:d4402bc3dd45 65 //more elegant ways to do this...but this is most efficient
AppleJuice 5:d395a7134278 66 setPixel(playerBall.y,playerBall.x+1);
AppleJuice 5:d395a7134278 67 setPixel(playerBall.y,playerBall.x+2);
AppleJuice 5:d395a7134278 68 setPixel(playerBall.y+1,playerBall.x);
AppleJuice 5:d395a7134278 69 setPixel(playerBall.y+1,playerBall.x+1);
AppleJuice 5:d395a7134278 70 setPixel(playerBall.y+1,playerBall.x+2);
AppleJuice 5:d395a7134278 71 setPixel(playerBall.y+1,playerBall.x+3);
AppleJuice 5:d395a7134278 72 setPixel(playerBall.y+2,playerBall.x);
AppleJuice 5:d395a7134278 73 setPixel(playerBall.y+2,playerBall.x+1);
AppleJuice 5:d395a7134278 74 setPixel(playerBall.y+2,playerBall.x+2);
AppleJuice 5:d395a7134278 75 setPixel(playerBall.y+2,playerBall.x+3);
AppleJuice 5:d395a7134278 76 setPixel(playerBall.y+3,playerBall.x+1);
AppleJuice 5:d395a7134278 77 setPixel(playerBall.y+3,playerBall.x+2);
AppleJuice 2:d4402bc3dd45 78 }
AppleJuice 2:d4402bc3dd45 79
AppleJuice 2:d4402bc3dd45 80 //draw the player ball where (x,y) is top right corner.
AppleJuice 5:d395a7134278 81 void GameScreen::eraseBall()
AppleJuice 2:d4402bc3dd45 82 {
AppleJuice 5:d395a7134278 83 clearPixel(playerBall.y,playerBall.x+1);
AppleJuice 5:d395a7134278 84 clearPixel(playerBall.y,playerBall.x+2);
AppleJuice 5:d395a7134278 85 clearPixel(playerBall.y+1,playerBall.x);
AppleJuice 5:d395a7134278 86 clearPixel(playerBall.y+1,playerBall.x+1);
AppleJuice 5:d395a7134278 87 clearPixel(playerBall.y+1,playerBall.x+2);
AppleJuice 5:d395a7134278 88 clearPixel(playerBall.y+1,playerBall.x+3);
AppleJuice 5:d395a7134278 89 clearPixel(playerBall.y+2,playerBall.x);
AppleJuice 5:d395a7134278 90 clearPixel(playerBall.y+2,playerBall.x+1);
AppleJuice 5:d395a7134278 91 clearPixel(playerBall.y+2,playerBall.x+2);
AppleJuice 5:d395a7134278 92 clearPixel(playerBall.y+2,playerBall.x+3);
AppleJuice 5:d395a7134278 93 clearPixel(playerBall.y+3,playerBall.x+1);
AppleJuice 5:d395a7134278 94 clearPixel(playerBall.y+3,playerBall.x+2);
AppleJuice 2:d4402bc3dd45 95 }
AppleJuice 2:d4402bc3dd45 96
AppleJuice 2:d4402bc3dd45 97 void GameScreen::createAllPlatforms()
AppleJuice 2:d4402bc3dd45 98 {
AppleJuice 2:d4402bc3dd45 99 for (int n = 0; n < numPlatforms_ ; n++)
AppleJuice 2:d4402bc3dd45 100 {
AppleJuice 2:d4402bc3dd45 101 Platform *newPlatform = new Platform;
AppleJuice 2:d4402bc3dd45 102 newPlatform->id = n;
AppleJuice 2:d4402bc3dd45 103 newPlatform->x = rand() % (maxX_-platGapSize_ + 1) - 1; // 'randomlly' place gap somewhere on platform *NOTE: need RTC for this game to be unpredictable...
AppleJuice 2:d4402bc3dd45 104 newPlatform->y = maxY_ - n*platSpacing_ - platThickness_ + 1;
AppleJuice 2:d4402bc3dd45 105 allPlatforms[n] = newPlatform;
AppleJuice 2:d4402bc3dd45 106 }
AppleJuice 2:d4402bc3dd45 107 }
AppleJuice 2:d4402bc3dd45 108
AppleJuice 2:d4402bc3dd45 109 void GameScreen::freeAllPlatforms()
AppleJuice 2:d4402bc3dd45 110 {
AppleJuice 2:d4402bc3dd45 111 for (int n = 0; n < numPlatforms_ ; n++)
AppleJuice 2:d4402bc3dd45 112 {
AppleJuice 2:d4402bc3dd45 113 delete allPlatforms[n];
AppleJuice 2:d4402bc3dd45 114 }
AppleJuice 2:d4402bc3dd45 115 }
AppleJuice 2:d4402bc3dd45 116
AppleJuice 2:d4402bc3dd45 117 void GameScreen::drawAllPlatforms()
AppleJuice 2:d4402bc3dd45 118 {
AppleJuice 2:d4402bc3dd45 119 for (int n = 0; n < numPlatforms_ ; n++)
AppleJuice 2:d4402bc3dd45 120 {
AppleJuice 2:d4402bc3dd45 121 drawPlatform (allPlatforms[n]->x,allPlatforms[n]->y);
AppleJuice 2:d4402bc3dd45 122 }
AppleJuice 2:d4402bc3dd45 123 }
AppleJuice 2:d4402bc3dd45 124
AppleJuice 2:d4402bc3dd45 125 void GameScreen::eraseAllPlatforms()
AppleJuice 2:d4402bc3dd45 126 {
AppleJuice 2:d4402bc3dd45 127 for (int n = 0; n < numPlatforms_ ; n++)
AppleJuice 2:d4402bc3dd45 128 {
AppleJuice 2:d4402bc3dd45 129 erasePlatform (allPlatforms[n]->y);
AppleJuice 2:d4402bc3dd45 130 }
AppleJuice 2:d4402bc3dd45 131 }
AppleJuice 2:d4402bc3dd45 132
AppleJuice 4:f8d04c073730 133 void GameScreen::shiftAllPlatforms()
AppleJuice 2:d4402bc3dd45 134 {
AppleJuice 2:d4402bc3dd45 135 for (int n = 0; n < numPlatforms_ ; n++)
AppleJuice 2:d4402bc3dd45 136 {
AppleJuice 11:c6c88617f7e7 137 if (allPlatforms[n]->y > (platThickness_+6))
AppleJuice 2:d4402bc3dd45 138 allPlatforms[n]->y = allPlatforms[n]->y - 1;
AppleJuice 2:d4402bc3dd45 139 else
AppleJuice 3:00c0f63f4408 140 {
AppleJuice 11:c6c88617f7e7 141 allPlatforms[n]->y = maxY_ - platThickness_ + 1+6; //send back to bottom.
AppleJuice 10:d8ef8633bd98 142 allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap!
AppleJuice 5:d395a7134278 143
AppleJuice 3:00c0f63f4408 144 }
AppleJuice 2:d4402bc3dd45 145 }
AppleJuice 4:f8d04c073730 146 }
AppleJuice 4:f8d04c073730 147
AppleJuice 5:d395a7134278 148 Platform GameScreen::nextClosestPlatform(int y)
AppleJuice 5:d395a7134278 149 {
AppleJuice 10:d8ef8633bd98 150 Serial serial(USBTX,USBRX);
AppleJuice 10:d8ef8633bd98 151 int closestY = 100;
AppleJuice 5:d395a7134278 152 Platform closestPlatform;
AppleJuice 5:d395a7134278 153
AppleJuice 5:d395a7134278 154 for (int i = 0; i < numPlatforms_; i++)
AppleJuice 5:d395a7134278 155 {
AppleJuice 10:d8ef8633bd98 156
AppleJuice 10:d8ef8633bd98 157 if (((allPlatforms[i]->y - y) < closestY) && ((allPlatforms[i]->y - y) > -1))
AppleJuice 5:d395a7134278 158 {
AppleJuice 10:d8ef8633bd98 159 closestY = allPlatforms[i]->y-y;
AppleJuice 5:d395a7134278 160 closestPlatform = *allPlatforms[i];
AppleJuice 5:d395a7134278 161 }
AppleJuice 5:d395a7134278 162 }
AppleJuice 5:d395a7134278 163 return closestPlatform;
AppleJuice 5:d395a7134278 164 }
AppleJuice 4:f8d04c073730 165
AppleJuice 6:b1c54f8b28fe 166 //@Override of base class function
AppleJuice 6:b1c54f8b28fe 167 //had to override and rewrite since N5110 is written to write horizontal this needs vertical
AppleJuice 6:b1c54f8b28fe 168 // this function reads existing font5x7 and rotate matrix to become font7x5 matrix.
AppleJuice 6:b1c54f8b28fe 169 //
AppleJuice 6:b1c54f8b28fe 170 // for example the number 4 font5x7 = 0x18,0x14,0x12,0x7F,0x10
AppleJuice 6:b1c54f8b28fe 171 // font7x5 = 0x02,0x06,0x10,0x12,0x1E,0x2,0x2
AppleJuice 6:b1c54f8b28fe 172 //then it is printed!
AppleJuice 11:c6c88617f7e7 173 void GameScreen::printString(const char * str,int x,int y,bool invert)
AppleJuice 6:b1c54f8b28fe 174 {
AppleJuice 12:9ba4b21f8220 175 //set all pixels in rows to black
AppleJuice 11:c6c88617f7e7 176 if (invert){
AppleJuice 11:c6c88617f7e7 177 for (int a = 0; a < 48; a ++)
AppleJuice 11:c6c88617f7e7 178 {
AppleJuice 11:c6c88617f7e7 179 for (int b = y; b < (y+9); b++)
AppleJuice 11:c6c88617f7e7 180 {
AppleJuice 11:c6c88617f7e7 181 setPixel(b,a);
AppleJuice 11:c6c88617f7e7 182 }
AppleJuice 11:c6c88617f7e7 183 }
AppleJuice 11:c6c88617f7e7 184 }
AppleJuice 11:c6c88617f7e7 185
AppleJuice 6:b1c54f8b28fe 186 while (*str)
AppleJuice 6:b1c54f8b28fe 187 {
AppleJuice 11:c6c88617f7e7 188
AppleJuice 6:b1c54f8b28fe 189 //each byte in row
AppleJuice 6:b1c54f8b28fe 190 for (int i = 0; i < 5; i++)
AppleJuice 6:b1c54f8b28fe 191 {
AppleJuice 6:b1c54f8b28fe 192 //each bit in byte
AppleJuice 8:ebddb721f1ee 193 for (int b = 0; b < 7; b++)
AppleJuice 11:c6c88617f7e7 194 {
AppleJuice 11:c6c88617f7e7 195 if (invert)
AppleJuice 11:c6c88617f7e7 196 setPixel(y+b+1,x-i+1);
AppleJuice 11:c6c88617f7e7 197
AppleJuice 8:ebddb721f1ee 198 if (font5x7[(*str - 32)*5 + i] & (1<<b)) //bitwise comparison to mask at desired pixel
AppleJuice 8:ebddb721f1ee 199 {
AppleJuice 11:c6c88617f7e7 200 setPixel(y+b+1,x-i+1);
AppleJuice 11:c6c88617f7e7 201 if (invert)
AppleJuice 11:c6c88617f7e7 202 clearPixel(y+b+1,x-i+1);
AppleJuice 6:b1c54f8b28fe 203 }
AppleJuice 6:b1c54f8b28fe 204 else
AppleJuice 8:ebddb721f1ee 205 {
AppleJuice 11:c6c88617f7e7 206 if (!invert)
AppleJuice 11:c6c88617f7e7 207 clearPixel(y+b+1,x-i+1);
AppleJuice 8:ebddb721f1ee 208 }
AppleJuice 6:b1c54f8b28fe 209 }
AppleJuice 8:ebddb721f1ee 210 }
AppleJuice 8:ebddb721f1ee 211 str ++;
AppleJuice 8:ebddb721f1ee 212 x -= 6;
AppleJuice 8:ebddb721f1ee 213 }
AppleJuice 11:c6c88617f7e7 214
AppleJuice 11:c6c88617f7e7 215
AppleJuice 8:ebddb721f1ee 216 refresh();
AppleJuice 8:ebddb721f1ee 217 }
AppleJuice 8:ebddb721f1ee 218
AppleJuice 8:ebddb721f1ee 219 void GameScreen::displayStartScreen()
AppleJuice 8:ebddb721f1ee 220 {
AppleJuice 8:ebddb721f1ee 221 clear();
AppleJuice 8:ebddb721f1ee 222 printString("LET",maxX_ - 14,-1);
AppleJuice 8:ebddb721f1ee 223 printString("THE BALL",maxX_ - 2,7);
AppleJuice 8:ebddb721f1ee 224 printString("DROP!",maxX_ - 12,16);
AppleJuice 8:ebddb721f1ee 225 printString("This way",maxX_ - 2,30);
AppleJuice 8:ebddb721f1ee 226 printString("------->",maxX_ - 2,37);
AppleJuice 8:ebddb721f1ee 227 printString("TO PLAY!",maxX_ - 2,45);
AppleJuice 8:ebddb721f1ee 228 printString("by: ",maxX_ - 2,61);
AppleJuice 8:ebddb721f1ee 229 printString("Thomas",maxX_ - 9,69);
AppleJuice 8:ebddb721f1ee 230 printString("Davies",maxX_ - 9,76);
AppleJuice 8:ebddb721f1ee 231 refresh();
AppleJuice 8:ebddb721f1ee 232 }
AppleJuice 8:ebddb721f1ee 233
AppleJuice 8:ebddb721f1ee 234 void GameScreen::displayInstructionScreen()
AppleJuice 8:ebddb721f1ee 235 {
AppleJuice 8:ebddb721f1ee 236 clear();
AppleJuice 8:ebddb721f1ee 237 printString("Hi Ball,",maxX_ - 2,-1);
AppleJuice 8:ebddb721f1ee 238 printString("You are",maxX_ - 2,7);
AppleJuice 8:ebddb721f1ee 239 printString("TRAPPED!",maxX_ - 2,15);
AppleJuice 8:ebddb721f1ee 240 printString("Use the ",maxX_ - 2,23);
AppleJuice 8:ebddb721f1ee 241 printString("joystick",maxX_ - 2,32);
AppleJuice 8:ebddb721f1ee 242 printString("to move.",maxX_ - 2,39);
AppleJuice 8:ebddb721f1ee 243 printString("Don't",maxX_ - 2,47);
AppleJuice 8:ebddb721f1ee 244 printString("hit the",maxX_ - 2,55);
AppleJuice 8:ebddb721f1ee 245 printString("roof!",maxX_ - 2,63);
AppleJuice 8:ebddb721f1ee 246 printString("------->",maxX_ - 2,75);
AppleJuice 8:ebddb721f1ee 247 refresh();
AppleJuice 8:ebddb721f1ee 248 }
AppleJuice 8:ebddb721f1ee 249
AppleJuice 8:ebddb721f1ee 250 void GameScreen::displayCountdown()
AppleJuice 8:ebddb721f1ee 251 {
AppleJuice 8:ebddb721f1ee 252 clear();
AppleJuice 8:ebddb721f1ee 253 Timer countdown;
AppleJuice 8:ebddb721f1ee 254 char buffer[10];
AppleJuice 8:ebddb721f1ee 255 countdown.start();
AppleJuice 8:ebddb721f1ee 256 printString("Survive!",maxX_ - 2,10);
AppleJuice 8:ebddb721f1ee 257
AppleJuice 8:ebddb721f1ee 258
AppleJuice 8:ebddb721f1ee 259 int prevTime = -1;
AppleJuice 8:ebddb721f1ee 260 while (countdown.read() < 5)
AppleJuice 8:ebddb721f1ee 261 {
AppleJuice 8:ebddb721f1ee 262 if (floor(countdown.read()) > prevTime)
AppleJuice 8:ebddb721f1ee 263 {
AppleJuice 8:ebddb721f1ee 264 sprintf(buffer,"..%1.0f..",(5 - floor(countdown.read())));
AppleJuice 8:ebddb721f1ee 265 printString(buffer,maxX_ - 10,20 + 10*countdown.read());
AppleJuice 8:ebddb721f1ee 266 prevTime = countdown.read();
AppleJuice 6:b1c54f8b28fe 267 }
AppleJuice 6:b1c54f8b28fe 268 }
AppleJuice 8:ebddb721f1ee 269 refresh();
AppleJuice 8:ebddb721f1ee 270 countdown.stop();
AppleJuice 6:b1c54f8b28fe 271 }
AppleJuice 5:d395a7134278 272
AppleJuice 11:c6c88617f7e7 273 void GameScreen::displayEndScreen(int lvl,int *cursor)
AppleJuice 11:c6c88617f7e7 274 {
AppleJuice 11:c6c88617f7e7 275 clear();
AppleJuice 11:c6c88617f7e7 276 char buffer [10];
AppleJuice 11:c6c88617f7e7 277 sprintf(buffer,"lvl: %d",lvl);
AppleJuice 11:c6c88617f7e7 278 printString("GameOver",maxX_ - 2,-1);
AppleJuice 11:c6c88617f7e7 279 printString(" :( ",maxX_ - 2,7);
AppleJuice 11:c6c88617f7e7 280 printString(buffer,maxX_ - 2,23);
AppleJuice 11:c6c88617f7e7 281 printString("Reached!",maxX_ - 2,32);
AppleJuice 5:d395a7134278 282
AppleJuice 12:9ba4b21f8220 283 //Options.
AppleJuice 11:c6c88617f7e7 284 printString("Record",maxX_ - 8,55,cursor[0]);
AppleJuice 11:c6c88617f7e7 285 printString("View",maxX_ - 15,63,cursor[1]);
AppleJuice 12:9ba4b21f8220 286 printString("Restart",maxX_ - 5,71,cursor[2]);
AppleJuice 11:c6c88617f7e7 287 refresh();
AppleJuice 11:c6c88617f7e7 288 }
AppleJuice 11:c6c88617f7e7 289
AppleJuice 12:9ba4b21f8220 290 void GameScreen::displayRecordScreen(int *cursor,char ltr1,char ltr2,char ltr3)
AppleJuice 12:9ba4b21f8220 291 {
AppleJuice 12:9ba4b21f8220 292 clear();
AppleJuice 12:9ba4b21f8220 293 printString("Enter",maxX_ - 2,-1);
AppleJuice 12:9ba4b21f8220 294 printString("Initials",maxX_ - 2,7);
AppleJuice 12:9ba4b21f8220 295
AppleJuice 12:9ba4b21f8220 296 //letter entry
AppleJuice 12:9ba4b21f8220 297 char buffer[2];
AppleJuice 12:9ba4b21f8220 298 sprintf(buffer,"%c",ltr1);
AppleJuice 12:9ba4b21f8220 299 printString(buffer,maxX_ - 23,20,cursor[0]);
AppleJuice 12:9ba4b21f8220 300 sprintf(buffer,"%c",ltr2);
AppleJuice 12:9ba4b21f8220 301 printString(buffer,maxX_ - 23,32,cursor[1]);
AppleJuice 12:9ba4b21f8220 302 sprintf(buffer,"%c",ltr3);
AppleJuice 12:9ba4b21f8220 303 printString(buffer,maxX_ - 23,44,cursor[2]);
AppleJuice 12:9ba4b21f8220 304
AppleJuice 12:9ba4b21f8220 305 printString("Click",maxX_ - 2,59);
AppleJuice 12:9ba4b21f8220 306 printString("when",maxX_ - 2,67);
AppleJuice 12:9ba4b21f8220 307 printString("finished",maxX_ - 2,75);
AppleJuice 12:9ba4b21f8220 308 refresh();
AppleJuice 12:9ba4b21f8220 309
AppleJuice 12:9ba4b21f8220 310 }
AppleJuice 12:9ba4b21f8220 311