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