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@5:d395a7134278, 2015-03-09 (annotated)
- Committer:
- AppleJuice
- Date:
- Mon Mar 09 21:14:47 2015 +0000
- Revision:
- 5:d395a7134278
- Parent:
- 4:f8d04c073730
- Child:
- 6:b1c54f8b28fe
ball collision detection working;
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 | 5:d395a7134278 | 8 | void GameScreen::Initialize(Ball ball) |
| AppleJuice | 1:3305d7e44880 | 9 | { |
| AppleJuice | 1:3305d7e44880 | 10 | init(); |
| AppleJuice | 5:d395a7134278 | 11 | |
| AppleJuice | 5:d395a7134278 | 12 | playerBall = ball; |
| AppleJuice | 2:d4402bc3dd45 | 13 | |
| AppleJuice | 1:3305d7e44880 | 14 | } |
| AppleJuice | 1:3305d7e44880 | 15 | |
| AppleJuice | 1:3305d7e44880 | 16 | //draw platform |
| AppleJuice | 1:3305d7e44880 | 17 | // ___________ ________ < y |
| AppleJuice | 1:3305d7e44880 | 18 | // ^ x |
| AppleJuice | 1:3305d7e44880 | 19 | void GameScreen::drawPlatform(int x,int y) |
| AppleJuice | 2:d4402bc3dd45 | 20 | { |
| AppleJuice | 1:3305d7e44880 | 21 | for (int a = 0; a < 48; a ++) |
| AppleJuice | 1:3305d7e44880 | 22 | { |
| AppleJuice | 2:d4402bc3dd45 | 23 | for (int b = y; b < (y+platThickness_); b++) |
| AppleJuice | 1:3305d7e44880 | 24 | { |
| AppleJuice | 2:d4402bc3dd45 | 25 | //skip pixels of gap |
| AppleJuice | 2:d4402bc3dd45 | 26 | if (a > x && a < (x + platGapSize_)) |
| AppleJuice | 2:d4402bc3dd45 | 27 | break; |
| AppleJuice | 2:d4402bc3dd45 | 28 | |
| AppleJuice | 2:d4402bc3dd45 | 29 | |
| AppleJuice | 2:d4402bc3dd45 | 30 | |
| AppleJuice | 2:d4402bc3dd45 | 31 | //skip pixels below maxY (lets platforms 'emerge' instead of 'appear') |
| AppleJuice | 2:d4402bc3dd45 | 32 | if (b > (maxY_ - 1)) |
| AppleJuice | 2:d4402bc3dd45 | 33 | break; |
| AppleJuice | 2:d4402bc3dd45 | 34 | |
| AppleJuice | 1:3305d7e44880 | 35 | setPixel(b,a); |
| AppleJuice | 1:3305d7e44880 | 36 | } |
| AppleJuice | 1:3305d7e44880 | 37 | } |
| AppleJuice | 2:d4402bc3dd45 | 38 | } |
| AppleJuice | 2:d4402bc3dd45 | 39 | |
| AppleJuice | 2:d4402bc3dd45 | 40 | //erase platform |
| AppleJuice | 2:d4402bc3dd45 | 41 | void GameScreen::erasePlatform(int y) |
| AppleJuice | 2:d4402bc3dd45 | 42 | { |
| AppleJuice | 2:d4402bc3dd45 | 43 | for (int a = 0; a < 48; a ++) |
| AppleJuice | 2:d4402bc3dd45 | 44 | { |
| AppleJuice | 2:d4402bc3dd45 | 45 | for (int b = y; b < (y+platThickness_); b++) |
| AppleJuice | 2:d4402bc3dd45 | 46 | { |
| AppleJuice | 2:d4402bc3dd45 | 47 | //skip pixels below maxY (lets platforms 'emerge' instead of 'appear') |
| AppleJuice | 2:d4402bc3dd45 | 48 | if (b > (maxY_ - 1)) |
| AppleJuice | 2:d4402bc3dd45 | 49 | break; |
| AppleJuice | 2:d4402bc3dd45 | 50 | |
| AppleJuice | 2:d4402bc3dd45 | 51 | clearPixel(b,a); |
| AppleJuice | 2:d4402bc3dd45 | 52 | } |
| AppleJuice | 2:d4402bc3dd45 | 53 | } |
| AppleJuice | 2:d4402bc3dd45 | 54 | } |
| AppleJuice | 2:d4402bc3dd45 | 55 | |
| AppleJuice | 2:d4402bc3dd45 | 56 | //draw the player ball where (x,y) is top right corner. |
| AppleJuice | 2:d4402bc3dd45 | 57 | // XXP <-- P(x,y) |
| AppleJuice | 2:d4402bc3dd45 | 58 | // XXXX |
| AppleJuice | 2:d4402bc3dd45 | 59 | // XXXX |
| AppleJuice | 2:d4402bc3dd45 | 60 | // XX |
| AppleJuice | 5:d395a7134278 | 61 | void GameScreen::drawBall() |
| AppleJuice | 2:d4402bc3dd45 | 62 | { |
| AppleJuice | 2:d4402bc3dd45 | 63 | //more elegant ways to do this...but this is most efficient |
| AppleJuice | 5:d395a7134278 | 64 | setPixel(playerBall.y,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 65 | setPixel(playerBall.y,playerBall.x+2); |
| AppleJuice | 5:d395a7134278 | 66 | setPixel(playerBall.y+1,playerBall.x); |
| AppleJuice | 5:d395a7134278 | 67 | setPixel(playerBall.y+1,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 68 | setPixel(playerBall.y+1,playerBall.x+2); |
| AppleJuice | 5:d395a7134278 | 69 | setPixel(playerBall.y+1,playerBall.x+3); |
| AppleJuice | 5:d395a7134278 | 70 | setPixel(playerBall.y+2,playerBall.x); |
| AppleJuice | 5:d395a7134278 | 71 | setPixel(playerBall.y+2,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 72 | setPixel(playerBall.y+2,playerBall.x+2); |
| AppleJuice | 5:d395a7134278 | 73 | setPixel(playerBall.y+2,playerBall.x+3); |
| AppleJuice | 5:d395a7134278 | 74 | setPixel(playerBall.y+3,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 75 | setPixel(playerBall.y+3,playerBall.x+2); |
| AppleJuice | 2:d4402bc3dd45 | 76 | } |
| AppleJuice | 2:d4402bc3dd45 | 77 | |
| AppleJuice | 2:d4402bc3dd45 | 78 | //draw the player ball where (x,y) is top right corner. |
| AppleJuice | 5:d395a7134278 | 79 | void GameScreen::eraseBall() |
| AppleJuice | 2:d4402bc3dd45 | 80 | { |
| AppleJuice | 5:d395a7134278 | 81 | clearPixel(playerBall.y,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 82 | clearPixel(playerBall.y,playerBall.x+2); |
| AppleJuice | 5:d395a7134278 | 83 | clearPixel(playerBall.y+1,playerBall.x); |
| AppleJuice | 5:d395a7134278 | 84 | clearPixel(playerBall.y+1,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 85 | clearPixel(playerBall.y+1,playerBall.x+2); |
| AppleJuice | 5:d395a7134278 | 86 | clearPixel(playerBall.y+1,playerBall.x+3); |
| AppleJuice | 5:d395a7134278 | 87 | clearPixel(playerBall.y+2,playerBall.x); |
| AppleJuice | 5:d395a7134278 | 88 | clearPixel(playerBall.y+2,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 89 | clearPixel(playerBall.y+2,playerBall.x+2); |
| AppleJuice | 5:d395a7134278 | 90 | clearPixel(playerBall.y+2,playerBall.x+3); |
| AppleJuice | 5:d395a7134278 | 91 | clearPixel(playerBall.y+3,playerBall.x+1); |
| AppleJuice | 5:d395a7134278 | 92 | clearPixel(playerBall.y+3,playerBall.x+2); |
| AppleJuice | 2:d4402bc3dd45 | 93 | } |
| AppleJuice | 2:d4402bc3dd45 | 94 | |
| AppleJuice | 2:d4402bc3dd45 | 95 | void GameScreen::createAllPlatforms() |
| AppleJuice | 2:d4402bc3dd45 | 96 | { |
| AppleJuice | 2:d4402bc3dd45 | 97 | for (int n = 0; n < numPlatforms_ ; n++) |
| AppleJuice | 2:d4402bc3dd45 | 98 | { |
| AppleJuice | 2:d4402bc3dd45 | 99 | Platform *newPlatform = new Platform; |
| AppleJuice | 2:d4402bc3dd45 | 100 | newPlatform->id = n; |
| AppleJuice | 2:d4402bc3dd45 | 101 | 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 | 102 | newPlatform->y = maxY_ - n*platSpacing_ - platThickness_ + 1; |
| AppleJuice | 2:d4402bc3dd45 | 103 | allPlatforms[n] = newPlatform; |
| AppleJuice | 2:d4402bc3dd45 | 104 | } |
| AppleJuice | 2:d4402bc3dd45 | 105 | } |
| AppleJuice | 2:d4402bc3dd45 | 106 | |
| AppleJuice | 2:d4402bc3dd45 | 107 | void GameScreen::freeAllPlatforms() |
| AppleJuice | 2:d4402bc3dd45 | 108 | { |
| AppleJuice | 2:d4402bc3dd45 | 109 | for (int n = 0; n < numPlatforms_ ; n++) |
| AppleJuice | 2:d4402bc3dd45 | 110 | { |
| AppleJuice | 2:d4402bc3dd45 | 111 | delete allPlatforms[n]; |
| AppleJuice | 2:d4402bc3dd45 | 112 | } |
| AppleJuice | 2:d4402bc3dd45 | 113 | } |
| AppleJuice | 2:d4402bc3dd45 | 114 | |
| AppleJuice | 2:d4402bc3dd45 | 115 | void GameScreen::drawAllPlatforms() |
| AppleJuice | 2:d4402bc3dd45 | 116 | { |
| AppleJuice | 2:d4402bc3dd45 | 117 | for (int n = 0; n < numPlatforms_ ; n++) |
| AppleJuice | 2:d4402bc3dd45 | 118 | { |
| AppleJuice | 2:d4402bc3dd45 | 119 | drawPlatform (allPlatforms[n]->x,allPlatforms[n]->y); |
| AppleJuice | 2:d4402bc3dd45 | 120 | } |
| AppleJuice | 2:d4402bc3dd45 | 121 | } |
| AppleJuice | 2:d4402bc3dd45 | 122 | |
| AppleJuice | 2:d4402bc3dd45 | 123 | void GameScreen::eraseAllPlatforms() |
| AppleJuice | 2:d4402bc3dd45 | 124 | { |
| AppleJuice | 2:d4402bc3dd45 | 125 | for (int n = 0; n < numPlatforms_ ; n++) |
| AppleJuice | 2:d4402bc3dd45 | 126 | { |
| AppleJuice | 2:d4402bc3dd45 | 127 | erasePlatform (allPlatforms[n]->y); |
| AppleJuice | 2:d4402bc3dd45 | 128 | } |
| AppleJuice | 2:d4402bc3dd45 | 129 | } |
| AppleJuice | 2:d4402bc3dd45 | 130 | |
| AppleJuice | 4:f8d04c073730 | 131 | void GameScreen::shiftAllPlatforms() |
| AppleJuice | 2:d4402bc3dd45 | 132 | { |
| AppleJuice | 2:d4402bc3dd45 | 133 | for (int n = 0; n < numPlatforms_ ; n++) |
| AppleJuice | 2:d4402bc3dd45 | 134 | { |
| AppleJuice | 2:d4402bc3dd45 | 135 | if (allPlatforms[n]->y > (platThickness_)) |
| AppleJuice | 2:d4402bc3dd45 | 136 | allPlatforms[n]->y = allPlatforms[n]->y - 1; |
| AppleJuice | 2:d4402bc3dd45 | 137 | else |
| AppleJuice | 3:00c0f63f4408 | 138 | { |
| AppleJuice | 2:d4402bc3dd45 | 139 | allPlatforms[n]->y = maxY_ - platThickness_ + 1; //send back to bottom. |
| AppleJuice | 5:d395a7134278 | 140 | //allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap! |
| AppleJuice | 5:d395a7134278 | 141 | |
| AppleJuice | 3:00c0f63f4408 | 142 | } |
| AppleJuice | 2:d4402bc3dd45 | 143 | } |
| AppleJuice | 4:f8d04c073730 | 144 | } |
| AppleJuice | 4:f8d04c073730 | 145 | |
| AppleJuice | 5:d395a7134278 | 146 | Platform GameScreen::nextClosestPlatform(int y) |
| AppleJuice | 5:d395a7134278 | 147 | { |
| AppleJuice | 5:d395a7134278 | 148 | int closestY = 15; |
| AppleJuice | 5:d395a7134278 | 149 | Platform closestPlatform; |
| AppleJuice | 5:d395a7134278 | 150 | |
| AppleJuice | 5:d395a7134278 | 151 | for (int i = 0; i < numPlatforms_; i++) |
| AppleJuice | 5:d395a7134278 | 152 | { |
| AppleJuice | 5:d395a7134278 | 153 | if ((allPlatforms[i]->y - y) < closestY && (allPlatforms[i]->y - y) >= -1) |
| AppleJuice | 5:d395a7134278 | 154 | { |
| AppleJuice | 5:d395a7134278 | 155 | closestY = allPlatforms[i]->y; |
| AppleJuice | 5:d395a7134278 | 156 | closestPlatform = *allPlatforms[i]; |
| AppleJuice | 5:d395a7134278 | 157 | } |
| AppleJuice | 5:d395a7134278 | 158 | } |
| AppleJuice | 5:d395a7134278 | 159 | return closestPlatform; |
| AppleJuice | 5:d395a7134278 | 160 | } |
| AppleJuice | 4:f8d04c073730 | 161 | |
| AppleJuice | 5:d395a7134278 | 162 | |
| AppleJuice | 5:d395a7134278 | 163 |