
Ball drop game with menus and highscore tracking developed for ELEC2645 at the University of Leeds.
Dependencies: N5110 mbed PowerControl
Game developed for ELEC 2645.
Extremely detailed report outlining all aspects of project found below. /media/uploads/AppleJuice/projectreport.pdf
Revision 5:d395a7134278, committed 2015-03-09
- Comitter:
- AppleJuice
- Date:
- Mon Mar 09 21:14:47 2015 +0000
- Parent:
- 4:f8d04c073730
- Child:
- 6:b1c54f8b28fe
- Commit message:
- ball collision detection working;
Changed in this revision
--- a/GameScreen.cpp Sat Mar 07 18:14:26 2015 +0000 +++ b/GameScreen.cpp Mon Mar 09 21:14:47 2015 +0000 @@ -2,10 +2,14 @@ #include "N5110.h" #include "GameScreen.h" + + //need to extend on initialization to set some variables -void GameScreen::Initialize() +void GameScreen::Initialize(Ball ball) { init(); + + playerBall = ball; } @@ -54,39 +58,38 @@ // XXXX // XXXX // XX -void GameScreen::drawBall(int x, int y) +void GameScreen::drawBall() { //more elegant ways to do this...but this is most efficient - setPixel(y,x+1); - setPixel(y,x+2); - setPixel(y+1,x); - setPixel(y+1,x+1); - setPixel(y+1,x+2); - setPixel(y+1,x+3); - setPixel(y+2,x); - setPixel(y+2,x+1); - setPixel(y+2,x+2); - setPixel(y+2,x+3); - setPixel(y+3,x+1); - setPixel(y+3,x+2); + setPixel(playerBall.y,playerBall.x+1); + setPixel(playerBall.y,playerBall.x+2); + setPixel(playerBall.y+1,playerBall.x); + setPixel(playerBall.y+1,playerBall.x+1); + setPixel(playerBall.y+1,playerBall.x+2); + setPixel(playerBall.y+1,playerBall.x+3); + setPixel(playerBall.y+2,playerBall.x); + setPixel(playerBall.y+2,playerBall.x+1); + setPixel(playerBall.y+2,playerBall.x+2); + setPixel(playerBall.y+2,playerBall.x+3); + setPixel(playerBall.y+3,playerBall.x+1); + setPixel(playerBall.y+3,playerBall.x+2); } //draw the player ball where (x,y) is top right corner. -void GameScreen::eraseBall(int x, int y) +void GameScreen::eraseBall() { - //more elegant ways to do this...but this is most efficient - clearPixel(y,x+1); - clearPixel(y,x+2); - clearPixel(y+1,x); - clearPixel(y+1,x+1); - clearPixel(y+1,x+2); - clearPixel(y+1,x+3); - clearPixel(y+2,x); - clearPixel(y+2,x+1); - clearPixel(y+2,x+2); - clearPixel(y+2,x+3); - clearPixel(y+3,x+1); - clearPixel(y+3,x+2); + clearPixel(playerBall.y,playerBall.x+1); + clearPixel(playerBall.y,playerBall.x+2); + clearPixel(playerBall.y+1,playerBall.x); + clearPixel(playerBall.y+1,playerBall.x+1); + clearPixel(playerBall.y+1,playerBall.x+2); + clearPixel(playerBall.y+1,playerBall.x+3); + clearPixel(playerBall.y+2,playerBall.x); + clearPixel(playerBall.y+2,playerBall.x+1); + clearPixel(playerBall.y+2,playerBall.x+2); + clearPixel(playerBall.y+2,playerBall.x+3); + clearPixel(playerBall.y+3,playerBall.x+1); + clearPixel(playerBall.y+3,playerBall.x+2); } void GameScreen::createAllPlatforms() @@ -134,9 +137,27 @@ else { allPlatforms[n]->y = maxY_ - platThickness_ + 1; //send back to bottom. - allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap! + //allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap! + } } } +Platform GameScreen::nextClosestPlatform(int y) +{ + int closestY = 15; + Platform closestPlatform; + + for (int i = 0; i < numPlatforms_; i++) + { + if ((allPlatforms[i]->y - y) < closestY && (allPlatforms[i]->y - y) >= -1) + { + closestY = allPlatforms[i]->y; + closestPlatform = *allPlatforms[i]; + } + } + return closestPlatform; +} + +
--- a/GameScreen.h Sat Mar 07 18:14:26 2015 +0000 +++ b/GameScreen.h Mon Mar 09 21:14:47 2015 +0000 @@ -35,25 +35,27 @@ explicit GameScreen(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) :N5110(pwrPin, scePin,rstPin,dcPin,mosiPin,sclkPin,ledPin) {} //classes needed are private...dont want to steal code so we'll just inheret constructor aswell :) - void Initialize(); + void Initialize(Ball ball); //draw horizontal platform where y top pixel layer location. x hole location - void drawBall(int x, int y); - void eraseBall(int x, int y); + void drawBall(); + void eraseBall(); void createAllPlatforms(); void drawAllPlatforms(); void eraseAllPlatforms(); void shiftAllPlatforms(); //move all platforms up 1 pixel + Platform nextClosestPlatform(int y); - //Write Access + //Write Access, set void setBallPos(int x, int y) { playerBall.x = x; playerBall.y = y; } - //Read Access - Platform getPlatformData(int i) { return *allPlatforms[i]; } - int maxY() { return maxY_; } - int maxX() { return maxX_; } - int platThickness() { return platThickness_; } + //Read Access, get + Platform getPlatformData(int i) { return *allPlatforms[i]; } + int getPlatGapSize() { return platGapSize_; } + int getMaxY() { return maxY_; } + int getMaxX() { return maxX_; } + int getPlatThickness() { return platThickness_; } int getBallR() { return ballRadius_; } int getBallX() { return playerBall.x; } int getBallY() { return playerBall.y; }
--- a/main.cpp Sat Mar 07 18:14:26 2015 +0000 +++ b/main.cpp Mon Mar 09 21:14:47 2015 +0000 @@ -24,15 +24,18 @@ void advanceAndCheckPlatforms(); bool isBallFalling(); - - - int main() { - lcd.Initialize(); - lcd.createAllPlatforms(); + // move to init + Ball playerBall; + playerBall.x = lcd.getMaxX()/2; + playerBall.y = lcd.getMaxY() - (int)(lcd.getMaxY()/3 * 2) - 5; + + lcd.Initialize(playerBall); + lcd.createAllPlatforms(); //move to init leds = 1; - lcd.drawAllPlatforms(); + lcd.drawAllPlatforms(); + lcd.drawBall(); lcd.refresh(); levelTicker.attach(&levelTickerExpired,kLevelTime); @@ -44,10 +47,10 @@ if(isPlatformExpired) { advanceAndCheckPlatforms(); + isPlatformExpired = false; } - } - + } return 1; } @@ -67,38 +70,52 @@ void advanceAndCheckPlatforms() { //ADVANCE + lcd.eraseBall(); lcd.eraseAllPlatforms(); lcd.shiftAllPlatforms(); lcd.drawAllPlatforms(); - + //AND CHECK - if(isBallFalling()) - { - lcd.setBallPos(lcd.getBallX(),lcd.getBallY() - 1); - //may want to read analog joystick here to allow control while falling? - //will need to test. Might not affect. - } - + //if ball isnt falling + if(!isBallFalling()) { + //move ball up with platform + lcd.setBallPos(lcd.getBallX(),lcd.getBallY() -1); + } + else { + lcd.setBallPos(lcd.getBallX(),lcd.getBallY() + 1); + } + lcd.drawBall(); lcd.refresh(); } -//NOT COMPLETE bool isBallFalling() { - - for (int i = 0; i < 6;i++) - { - Platform tempPlatform = lcd.getPlatformData(i); + int bX = lcd.getBallX(); //ball X pos + int bR = lcd.getBallR(); //ball radius + int bY = lcd.getBallY(); //ball Y pos - //if bottom of ball is not one pixel above a platform then we be fallin' - if ( (lcd.getBallY()-lcd.getBallR()) != (tempPlatform.y - 1)) - return true; - - //if entire ball is in gap, then you be falling! - //else - // if (lcd.getBallX - - } - return 1; -} \ No newline at end of file + //find next platform lower than ball + //check Y pos to see if ball is on platform, return false if on plat + //check X pos to see if ball is over gap, return true if over gap + Platform closestPlatform = lcd.nextClosestPlatform(bY + bR); + serial.printf("ball bottom (%d,%d) \t plat #:%d (%d,%d) \n",bX,bY+bR,closestPlatform.id,closestPlatform.x,closestPlatform.y); + + //if bottom of ball on level of platform + if ((bY+bR) == closestPlatform.y+1) + { + serial.printf("Same level as Plat \n"); + if (bX >= closestPlatform.x && (bX + bR) <= (closestPlatform.x + lcd.getPlatGapSize())) //OVER GAP + { + serial.printf("Over Gap"); + return true; + } + else { + serial.printf("On plat"); + return false; + } //On platform! + } + else //FREE SPACE + return true; +} +