Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 mbed PowerControl
GameScreen.cpp@8:ebddb721f1ee, 2015-03-10 (annotated)
- Committer:
- AppleJuice
- Date:
- Tue Mar 10 21:17:58 2015 +0000
- Revision:
- 8:ebddb721f1ee
- Parent:
- 6:b1c54f8b28fe
- Child:
- 9:c20f11f9b310
added: start screen, instructions, and countdown to start.; ; bugs: ball sometimes passes through platforms if it traveled sideways through another above. Should be easily fixed by adding left/right movement boundaries.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:d4402bc3dd45 | 137 | if (allPlatforms[n]->y > (platThickness_)) |
AppleJuice | 2:d4402bc3dd45 | 138 | allPlatforms[n]->y = allPlatforms[n]->y - 1; |
AppleJuice | 2:d4402bc3dd45 | 139 | else |
AppleJuice | 3:00c0f63f4408 | 140 | { |
AppleJuice | 2:d4402bc3dd45 | 141 | allPlatforms[n]->y = maxY_ - platThickness_ + 1; //send back to bottom. |
AppleJuice | 5:d395a7134278 | 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 | 5:d395a7134278 | 150 | int closestY = 15; |
AppleJuice | 5:d395a7134278 | 151 | Platform closestPlatform; |
AppleJuice | 5:d395a7134278 | 152 | |
AppleJuice | 5:d395a7134278 | 153 | for (int i = 0; i < numPlatforms_; i++) |
AppleJuice | 5:d395a7134278 | 154 | { |
AppleJuice | 5:d395a7134278 | 155 | if ((allPlatforms[i]->y - y) < closestY && (allPlatforms[i]->y - y) >= -1) |
AppleJuice | 5:d395a7134278 | 156 | { |
AppleJuice | 5:d395a7134278 | 157 | closestY = allPlatforms[i]->y; |
AppleJuice | 5:d395a7134278 | 158 | closestPlatform = *allPlatforms[i]; |
AppleJuice | 5:d395a7134278 | 159 | } |
AppleJuice | 5:d395a7134278 | 160 | } |
AppleJuice | 5:d395a7134278 | 161 | return closestPlatform; |
AppleJuice | 5:d395a7134278 | 162 | } |
AppleJuice | 4:f8d04c073730 | 163 | |
AppleJuice | 6:b1c54f8b28fe | 164 | //@Override of base class function |
AppleJuice | 6:b1c54f8b28fe | 165 | //had to override and rewrite since N5110 is written to write horizontal this needs vertical |
AppleJuice | 6:b1c54f8b28fe | 166 | // this function reads existing font5x7 and rotate matrix to become font7x5 matrix. |
AppleJuice | 6:b1c54f8b28fe | 167 | // |
AppleJuice | 6:b1c54f8b28fe | 168 | // for example the number 4 font5x7 = 0x18,0x14,0x12,0x7F,0x10 |
AppleJuice | 6:b1c54f8b28fe | 169 | // font7x5 = 0x02,0x06,0x10,0x12,0x1E,0x2,0x2 |
AppleJuice | 6:b1c54f8b28fe | 170 | //then it is printed! |
AppleJuice | 6:b1c54f8b28fe | 171 | void GameScreen::printString(const char * str,int x,int y) |
AppleJuice | 6:b1c54f8b28fe | 172 | { |
AppleJuice | 6:b1c54f8b28fe | 173 | while (*str) |
AppleJuice | 6:b1c54f8b28fe | 174 | { |
AppleJuice | 6:b1c54f8b28fe | 175 | //each byte in row |
AppleJuice | 6:b1c54f8b28fe | 176 | for (int i = 0; i < 5; i++) |
AppleJuice | 6:b1c54f8b28fe | 177 | { |
AppleJuice | 6:b1c54f8b28fe | 178 | //each bit in byte |
AppleJuice | 8:ebddb721f1ee | 179 | for (int b = 0; b < 7; b++) |
AppleJuice | 6:b1c54f8b28fe | 180 | { |
AppleJuice | 8:ebddb721f1ee | 181 | |
AppleJuice | 8:ebddb721f1ee | 182 | if (font5x7[(*str - 32)*5 + i] & (1<<b)) //bitwise comparison to mask at desired pixel |
AppleJuice | 8:ebddb721f1ee | 183 | { |
AppleJuice | 8:ebddb721f1ee | 184 | setPixel(y+b+1,x-i+1); |
AppleJuice | 6:b1c54f8b28fe | 185 | } |
AppleJuice | 6:b1c54f8b28fe | 186 | else |
AppleJuice | 8:ebddb721f1ee | 187 | { |
AppleJuice | 8:ebddb721f1ee | 188 | clearPixel(y+b+1,x-i+1); |
AppleJuice | 8:ebddb721f1ee | 189 | } |
AppleJuice | 6:b1c54f8b28fe | 190 | } |
AppleJuice | 8:ebddb721f1ee | 191 | } |
AppleJuice | 8:ebddb721f1ee | 192 | str ++; |
AppleJuice | 8:ebddb721f1ee | 193 | x -= 6; |
AppleJuice | 8:ebddb721f1ee | 194 | } |
AppleJuice | 8:ebddb721f1ee | 195 | refresh(); |
AppleJuice | 8:ebddb721f1ee | 196 | } |
AppleJuice | 8:ebddb721f1ee | 197 | |
AppleJuice | 8:ebddb721f1ee | 198 | void GameScreen::displayStartScreen() |
AppleJuice | 8:ebddb721f1ee | 199 | { |
AppleJuice | 8:ebddb721f1ee | 200 | clear(); |
AppleJuice | 8:ebddb721f1ee | 201 | printString("LET",maxX_ - 14,-1); |
AppleJuice | 8:ebddb721f1ee | 202 | printString("THE BALL",maxX_ - 2,7); |
AppleJuice | 8:ebddb721f1ee | 203 | printString("DROP!",maxX_ - 12,16); |
AppleJuice | 8:ebddb721f1ee | 204 | printString("This way",maxX_ - 2,30); |
AppleJuice | 8:ebddb721f1ee | 205 | printString("------->",maxX_ - 2,37); |
AppleJuice | 8:ebddb721f1ee | 206 | printString("TO PLAY!",maxX_ - 2,45); |
AppleJuice | 8:ebddb721f1ee | 207 | printString("by: ",maxX_ - 2,61); |
AppleJuice | 8:ebddb721f1ee | 208 | printString("Thomas",maxX_ - 9,69); |
AppleJuice | 8:ebddb721f1ee | 209 | printString("Davies",maxX_ - 9,76); |
AppleJuice | 8:ebddb721f1ee | 210 | refresh(); |
AppleJuice | 8:ebddb721f1ee | 211 | } |
AppleJuice | 8:ebddb721f1ee | 212 | |
AppleJuice | 8:ebddb721f1ee | 213 | void GameScreen::displayInstructionScreen() |
AppleJuice | 8:ebddb721f1ee | 214 | { |
AppleJuice | 8:ebddb721f1ee | 215 | clear(); |
AppleJuice | 8:ebddb721f1ee | 216 | printString("Hi Ball,",maxX_ - 2,-1); |
AppleJuice | 8:ebddb721f1ee | 217 | printString("You are",maxX_ - 2,7); |
AppleJuice | 8:ebddb721f1ee | 218 | printString("TRAPPED!",maxX_ - 2,15); |
AppleJuice | 8:ebddb721f1ee | 219 | printString("Use the ",maxX_ - 2,23); |
AppleJuice | 8:ebddb721f1ee | 220 | printString("joystick",maxX_ - 2,32); |
AppleJuice | 8:ebddb721f1ee | 221 | printString("to move.",maxX_ - 2,39); |
AppleJuice | 8:ebddb721f1ee | 222 | printString("Don't",maxX_ - 2,47); |
AppleJuice | 8:ebddb721f1ee | 223 | printString("hit the",maxX_ - 2,55); |
AppleJuice | 8:ebddb721f1ee | 224 | printString("roof!",maxX_ - 2,63); |
AppleJuice | 8:ebddb721f1ee | 225 | printString("------->",maxX_ - 2,75); |
AppleJuice | 8:ebddb721f1ee | 226 | refresh(); |
AppleJuice | 8:ebddb721f1ee | 227 | } |
AppleJuice | 8:ebddb721f1ee | 228 | |
AppleJuice | 8:ebddb721f1ee | 229 | void GameScreen::displayCountdown() |
AppleJuice | 8:ebddb721f1ee | 230 | { |
AppleJuice | 8:ebddb721f1ee | 231 | clear(); |
AppleJuice | 8:ebddb721f1ee | 232 | Timer countdown; |
AppleJuice | 8:ebddb721f1ee | 233 | char buffer[10]; |
AppleJuice | 8:ebddb721f1ee | 234 | countdown.start(); |
AppleJuice | 8:ebddb721f1ee | 235 | printString("Survive!",maxX_ - 2,10); |
AppleJuice | 8:ebddb721f1ee | 236 | |
AppleJuice | 8:ebddb721f1ee | 237 | |
AppleJuice | 8:ebddb721f1ee | 238 | int prevTime = -1; |
AppleJuice | 8:ebddb721f1ee | 239 | while (countdown.read() < 5) |
AppleJuice | 8:ebddb721f1ee | 240 | { |
AppleJuice | 8:ebddb721f1ee | 241 | if (floor(countdown.read()) > prevTime) |
AppleJuice | 8:ebddb721f1ee | 242 | { |
AppleJuice | 8:ebddb721f1ee | 243 | sprintf(buffer,"..%1.0f..",(5 - floor(countdown.read()))); |
AppleJuice | 8:ebddb721f1ee | 244 | printString(buffer,maxX_ - 10,20 + 10*countdown.read()); |
AppleJuice | 8:ebddb721f1ee | 245 | prevTime = countdown.read(); |
AppleJuice | 6:b1c54f8b28fe | 246 | } |
AppleJuice | 6:b1c54f8b28fe | 247 | } |
AppleJuice | 8:ebddb721f1ee | 248 | refresh(); |
AppleJuice | 8:ebddb721f1ee | 249 | countdown.stop(); |
AppleJuice | 6:b1c54f8b28fe | 250 | } |
AppleJuice | 5:d395a7134278 | 251 | |
AppleJuice | 5:d395a7134278 | 252 |