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

Files at this revision

API Documentation at this revision

Comitter:
AppleJuice
Date:
Thu May 07 20:34:14 2015 +0000
Parent:
17:7c926de79e02
Child:
19:97e0516dd6b2
Commit message:
added collision detection for entire platform, not just top;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue May 05 13:20:23 2015 +0000
+++ b/main.cpp	Thu May 07 20:34:14 2015 +0000
@@ -1,4 +1,4 @@
-/** 
+/**
 @file main.cpp
 @brief Source file for main game containing function defintions and main loop.
 @autor Thomas Davies
@@ -10,7 +10,7 @@
 
 int main()
 {
-    lcd.Initialize();       
+    lcd.Initialize();
 
     lcd.displayStartScreen();
     while(!isRight()) {}       //wait until right button pressed
@@ -19,7 +19,7 @@
     lcd.displayInstructionScreen();
     while(!isRight()) {}    //wait until right button pressed.
 
-    lcd.displayCountdown();
+    //lcd.displayCountdown();
     lcd.clear();
 
     bool refresh = false;
@@ -28,7 +28,7 @@
     lcd.drawBall();
     lcd.refresh();
 
-    gameClock.attach(&clockCounter,0.003);  //attach the master clock! 
+    gameClock.attach(&clockCounter,0.003);  //attach the master clock!
 
     //MAIN GAME LOOP
     //multiple runs on a single loop. stop with a bool
@@ -36,13 +36,13 @@
     char buffer[10];
     while(true) {
         if (isFirstCheck) { //if first time checking this clock cycle.
-            if(clockCount %(gameSpeed) == 0) {  
+            if(clockCount %(gameSpeed) == 0) {
                 //advance platforms
-                advancePlatforms(1);
+                advancePlatforms(numPixelsToJump);
                 refresh = true;
             }
 
-            if(clockCount%10 == 0) {
+            if(clockCount%14 == 0) {
                 //ball movement
                 if (isLeft() && isBallDirClear(1)) {    //if josystick left and direction is clear
                     lcd.eraseBall();
@@ -54,30 +54,33 @@
                     lcd.setBallPos(lcd.getBallX()-2,lcd.getBallY());
                     refresh = true;
                 }
-                
-                //if the ball is in free space, animate it falling!
-                if(isBallFalling(lcd.getBallY())) { 
+            }
+
+            //if the ball is in free space, animate it falling!
+            if (clockCount%(10/numPixelsToJump) == 0) {
+                if(isBallFalling(lcd.getBallY())) {
                     lcd.eraseBall();
-                    lcd.setBallPos(lcd.getBallX(),lcd.getBallY() + 1);
+                    lcd.setBallPos(lcd.getBallX(),lcd.getBallY() + numPixelsToJump);
                     refresh = true;
                     isFirstHit = true;
                 }
             }
-            
-            if (clockCount %500 == 0)
-            {
+
+            if (clockCount %500 == 0) {
                 gameLevel++;
+
                 if (gameLevel%4 == 0)
                     if (gameLevel > 60)
                         gameSpeed -=1;
-                    else    
+                    else
                         gameSpeed -= 2;
+
             }
-            
+
             if (refresh) {
                 lcd.drawBall();
                 lcd.drawAllPlatforms();
-                
+
                 sprintf(buffer,"lvl: %d",gameLevel);
                 lcd.printString(buffer,lcd.getMaxX() - 2,-1);
                 lcd.refresh();
@@ -93,7 +96,7 @@
     }
 
     gameClock.detach();
-    
+
     lcd.clear();
 
     endScreenController();
@@ -109,7 +112,7 @@
     lcd.shiftAllPlatforms(n);
     lcd.drawAllPlatforms();
 
-    if(!isBallFalling(lcd.getBallY()-1)) {
+    if(!isBallFalling(lcd.getBallY()-n)) {
         //move ball up with platform
         if (isFirstHit) {
             //beep();
@@ -117,7 +120,7 @@
         }
 
         lcd.eraseBall();
-        lcd.setBallPos(lcd.getBallX(),lcd.getBallY() -1);
+        lcd.setBallPos(lcd.getBallX(),lcd.getBallY() -n);
     }
 
 
@@ -136,7 +139,6 @@
 
     //if bottom of ball on level of platform
     if ((bY+bR) == (closestPlatform.y)) {
-        //serial.printf ("On level of plat \n");
         if (bX > closestPlatform.x && (bX+bR-1) < (closestPlatform.x + lcd.getPlatGapSize())) { //OVER GAP
             return true;
         } else {
@@ -151,12 +153,30 @@
 
 bool isBallDirClear(int dir)
 {
-    if (dir == 1) {
-        if ((lcd.getBallX()+lcd.getBallR()) < (lcd.getMaxX())) {
-            return true;
+
+    //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(lcd.getBallY() - lcd.getBallR()+2);
+
+    int ballRight = lcd.getBallX();
+    int ballLeft = ballRight+lcd.getBallR();    
+    int ballTop = lcd.getBallY();
+    int ballBottom = ballTop + lcd.getBallR();
+
+    if (dir == 1) {        
+        if (ballBottom > closestPlatform.y)    //is ball inside a gap in platform?
+            if (ballLeft >= (closestPlatform.x+lcd.getPlatGapSize()))    //is the ball next to gap wall?
+                    return false;
+        if (ballLeft < lcd.getMaxX()) {    //is ball next to left hand wall?
+            return true;            
         }
     } else if (dir == 0) {
-        if (lcd.getBallX() > 0) {
+        if (ballBottom > closestPlatform.y)    //is ball inside a gap in platform?
+            if (ballRight <= closestPlatform.x)
+                return false;
+        
+        if (ballRight > 0) {   //right hand wall check
             return true;
         }
     }
@@ -237,7 +257,7 @@
         {0,1,0},
         {0,0,1}
     };
-    
+
     bool record = isRecord();
     lcd.displayEndScreen(gameLevel,cursorArray[cursorLoc],record);
     bool change = false;
@@ -266,9 +286,9 @@
             switch(cursorLoc) {
                 case 0:
                     //take to initial entry page! (only allow if score > top 3)
-                    wait(0.1);      
+                    wait(0.1);
                     highScoreEditor(recordEntryController(),record);
-                    
+
                     wait(1);
                     //reset once button pressed!
                     while(!joystickButton.read()) {}
@@ -351,13 +371,13 @@
 }
 
 void highScoreEditor(char *playerInitials,bool isRecord)
-{    
+{
     //check if lvl is in top 3
     FILE *fp = fopen("/local/scores.csv","r");  //open for reading.
- 
+
     char s_hs0[3],s_hs1[3],s_hs2[3];
     int d_hs[3];
-    char buffer[2];            
+    char buffer[2];
 
     fscanf(fp,"%3s,%s",s_hs0,buffer);
     d_hs[0] = atoi(buffer);
@@ -365,11 +385,11 @@
     d_hs[1] = atoi(buffer);
     fscanf(fp,"%3s,%s",s_hs2,buffer);
     d_hs[2] = atoi(buffer);
-    
+
     fclose(fp);
-    
+
     if (isRecord) {
-        
+
         if (gameLevel >= d_hs[2]) {
             d_hs[2] = gameLevel;
             strncpy(s_hs2,playerInitials,3);
@@ -386,28 +406,27 @@
             strncpy(s_hs1,s_hs0,3);
             strncpy(s_hs0,playerInitials,3);
         }
-        
+
         char buffer[40];
         sprintf(buffer,"%s,%d\n%s,%d\n%s,%d",s_hs0,d_hs[0],s_hs1,d_hs[1],s_hs2,d_hs[2]);
         fp = fopen("/local/scores.csv","w");
         fprintf(fp,buffer);
         fclose(fp);
     }
-     
-    lcd.displayHighScores(s_hs0,s_hs1,s_hs2,d_hs);    
+
+    lcd.displayHighScores(s_hs0,s_hs1,s_hs2,d_hs);
 }
 
 bool isRecord ()
 {
     //check if lvl is in top 3
     FILE *fp = fopen("/local/scores.csv","r");  //open for reading.
- 
+
     int highScore;
     char buffer[2];
     char temp[3];
-            
-    for (int i = 0;i < 3;i++)
-    {
+
+    for (int i = 0; i < 3; i++) {
         fscanf(fp,"%3s,%s",temp,buffer);
         highScore = atoi(buffer);
         if (gameLevel > highScore) {
--- a/main.h	Tue May 05 13:20:23 2015 +0000
+++ b/main.h	Thu May 07 20:34:14 2015 +0000
@@ -70,8 +70,9 @@
 int clockCount = 0;         ///< master clock counter
 bool isFirstCheck = true;   ///< first check in clock cycle?
 bool isFirstHit = true;     ///< first hit on a platform?
-int gameLevel = 1;          ///< current game level
+int gameLevel = 0;          ///< current game level
 int gameSpeed = 50;         ///< current game speed
+int numPixelsToJump = 1;    ///< number of pixels ball/plat moves per animation
 
 //#############FUNCTION PROTOTYPES##################//