Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

Committer:
AppleJuice
Date:
Tue Mar 10 02:32:10 2015 +0000
Revision:
6:b1c54f8b28fe
Parent:
5:d395a7134278
Child:
8:ebddb721f1ee
ball rises/falls with platforms. ; ; Now working on overrideing "printString" to allow proper orientation...

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 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 6:b1c54f8b28fe 162 //@Override of base class function
AppleJuice 6:b1c54f8b28fe 163 //had to override and rewrite since N5110 is written to write horizontal this needs vertical
AppleJuice 6:b1c54f8b28fe 164 // this function reads existing font5x7 and rotate matrix to become font7x5 matrix.
AppleJuice 6:b1c54f8b28fe 165 //
AppleJuice 6:b1c54f8b28fe 166 // for example the number 4 font5x7 = 0x18,0x14,0x12,0x7F,0x10
AppleJuice 6:b1c54f8b28fe 167 // font7x5 = 0x02,0x06,0x10,0x12,0x1E,0x2,0x2
AppleJuice 6:b1c54f8b28fe 168 //then it is printed!
AppleJuice 6:b1c54f8b28fe 169 void GameScreen::printString(const char * str,int x,int y)
AppleJuice 6:b1c54f8b28fe 170 {
AppleJuice 6:b1c54f8b28fe 171 while (*str)
AppleJuice 6:b1c54f8b28fe 172 {
AppleJuice 6:b1c54f8b28fe 173 //each byte in row
AppleJuice 6:b1c54f8b28fe 174 for (int i = 0; i < 5; i++)
AppleJuice 6:b1c54f8b28fe 175 {
AppleJuice 6:b1c54f8b28fe 176 //each bit in byte
AppleJuice 6:b1c54f8b28fe 177 for (int b = 1; i < 8; b++)
AppleJuice 6:b1c54f8b28fe 178 {
AppleJuice 6:b1c54f8b28fe 179 if (font5x7[(*str - 32)*5 + i] & (2^b)) //bitwise comparison
AppleJuice 6:b1c54f8b28fe 180 {
AppleJuice 6:b1c54f8b28fe 181
AppleJuice 6:b1c54f8b28fe 182 setPixel(y+b-1,x-i);
AppleJuice 6:b1c54f8b28fe 183 }
AppleJuice 6:b1c54f8b28fe 184 else
AppleJuice 6:b1c54f8b28fe 185 clearPixel(y+b-1,x-i);
AppleJuice 6:b1c54f8b28fe 186 }
AppleJuice 6:b1c54f8b28fe 187 }
AppleJuice 6:b1c54f8b28fe 188 str++;
AppleJuice 6:b1c54f8b28fe 189
AppleJuice 6:b1c54f8b28fe 190 }
AppleJuice 6:b1c54f8b28fe 191 refresh();
AppleJuice 6:b1c54f8b28fe 192
AppleJuice 6:b1c54f8b28fe 193
AppleJuice 6:b1c54f8b28fe 194 }
AppleJuice 5:d395a7134278 195
AppleJuice 5:d395a7134278 196