Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

Revision:
14:f5760f76fe83
Parent:
13:7071a14b2fab
Child:
19:97e0516dd6b2
--- a/GameScreen.cpp	Thu Apr 30 00:15:51 2015 +0000
+++ b/GameScreen.cpp	Fri May 01 16:17:27 2015 +0000
@@ -2,14 +2,13 @@
 #include "N5110.h"
 #include "GameScreen.h"
 
-
-
-//need to extend on initialization to set some variables
+//initialise function, sets ball pos and creates platforms
 void GameScreen::Initialize()
 {
-    init();
+    init();         //power up screen and set brightness
     
-    playerBall.x = maxX_/2;
+    //set ball pos
+    playerBall.x = maxX_/2; 
     playerBall.y = maxY_ - (int)(maxY_/3 * 2) - 5;
     
     createAllPlatforms(); 
@@ -26,9 +25,7 @@
         {
             //skip pixels of gap
             if (a > x && a < (x + platGapSize_))
-                break;
-                
-            
+                break;            
             
             //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
             if (b > (maxY_ - 1))
@@ -98,14 +95,16 @@
 {            
     for (int n = 0; n < numPlatforms_ ; n++)
     {
-        Platform *newPlatform = new Platform;
-        newPlatform->id = n;
-        newPlatform->x = rand() % (maxX_-platGapSize_ + 1) - 1;     // 'randomlly' place gap somewhere on platform *NOTE: need RTC for this game to be unpredictable...
+        //create new platform and add to allPlatform array.
+        Platform *newPlatform = new Platform;                           
+        newPlatform->id = n;                                
+        newPlatform->x = rand() % (maxX_-platGapSize_ + 1) - 1;         //randomlly select platform location
         newPlatform->y = maxY_ - n*platSpacing_ - platThickness_ + 1;   
         allPlatforms[n] = newPlatform;
     }
 }
 
+//Garbage cleanup, free all the platforms!
 void GameScreen::freeAllPlatforms()
 {
     for (int n = 0; n < numPlatforms_ ; n++)
@@ -130,16 +129,16 @@
     } 
 }
 
-void GameScreen::shiftAllPlatforms()
+void GameScreen::shiftAllPlatforms(int n )
 {
-    for (int n = 0; n < numPlatforms_ ; n++)
+    for (int i = 0; i < numPlatforms_ ; i++)
     {
-        if (allPlatforms[n]->y > (platThickness_+6))
-            allPlatforms[n]->y = allPlatforms[n]->y - 1;
+        if (allPlatforms[i]->y > (platThickness_+5+n))
+            allPlatforms[i]->y = allPlatforms[i]->y - n;
         else
         {
-            allPlatforms[n]->y =  maxY_ - platThickness_ + 1+6; //send back to bottom.
-            allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap!
+            allPlatforms[i]->y =  maxY_ - platThickness_ + 1+6; //send back to bottom.
+            allPlatforms[i]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap!
             
         }
     }
@@ -147,10 +146,9 @@
 
 Platform GameScreen::nextClosestPlatform(int y)
 {
-    Serial serial(USBTX,USBRX);
     int closestY = 100;
     Platform closestPlatform;
-    
+    //scan each platform and determin which is closest.
     for (int i = 0; i < numPlatforms_; i++)
     {
         
@@ -183,6 +181,7 @@
         }
     }
     
+    //scan through string, rotating font5x7 array and printing charector.
     while (*str)
     {
 
@@ -208,12 +207,9 @@
                 }   
             }    
         }        
-        str ++;
+        str ++; //next char in string
         x -= 6;
-    }
-
-        
-    refresh();     
+    }            
 }
 
 void GameScreen::displayStartScreen()
@@ -250,27 +246,28 @@
 void GameScreen::displayCountdown()
 {
     clear();
-    Timer countdown;
+    Timer countdown;        //timer for countdown
     char buffer[10];
     countdown.start();
-    printString("Survive!",maxX_ - 2,10);
-    
+    printString("Survive!",maxX_ - 2,10);  
+    refresh(); 
     
     int prevTime = -1;
+    //countdown from 5 to 1, in seconds.
     while (countdown.read() < 5)
     {
         if (floor(countdown.read()) > prevTime)
         {
             sprintf(buffer,"..%1.0f..",(5 - floor(countdown.read())));
             printString(buffer,maxX_ - 10,20 + 10*countdown.read());
+            refresh();
             prevTime = countdown.read(); 
         }
     }
-    refresh();
     countdown.stop();     
 }
 
-void GameScreen::displayEndScreen(int lvl,int *cursor)
+void GameScreen::displayEndScreen(int lvl,int *cursor, bool isRecord)
 {
     clear();
     char buffer [10];
@@ -280,8 +277,10 @@
     printString(buffer,maxX_ - 2,23);
     printString("Reached!",maxX_ - 2,32);    
 
-    //Options.
-    printString("Record",maxX_ - 8,55,cursor[0]);   
+    //only display record option if player achieved record!
+    if (isRecord)
+        printString("Record",maxX_ - 8,55,cursor[0]);
+           
     printString("View",maxX_ - 15,63,cursor[1]);
     printString("Restart",maxX_ - 5,71,cursor[2]); 
     refresh();
@@ -309,27 +308,24 @@
     
 }
 
-void GameScreen::displayHighScores(char **initials,int * lvls)
+void GameScreen::displayHighScores(char *s_hs0,char *s_hs1,char *s_hs2,int *d_hs)
 {
     clear();
     printString("SCORES",maxX_ - 2,-1);
 
-
     char buffer[10];
     
-    sprintf(buffer,"1.%s %d",initials[0],lvls[0]);
+    sprintf(buffer,"1.%s %d",s_hs0,d_hs[0]);
     printString(buffer,maxX_ - 2,15);
 
-    sprintf(buffer,"2.%s %d",initials[1],lvls[1]);    
+    sprintf(buffer,"2.%s %d",s_hs1,d_hs[1]);    
     printString(buffer,maxX_ - 2,33);
     
-    sprintf(buffer,"3.%s %d",initials[2],lvls[2]);  
-    printString(buffer,maxX_ - 2,51);
-    
+    sprintf(buffer,"3.%s %d",s_hs2,d_hs[2]);  
+    printString(buffer,maxX_ - 2,51);    
     
-    printString("Click",maxX_ - 2,68);
-    printString("to quit!",maxX_ - 2,76);
-    refresh();
- 
+    printString("Click to",maxX_ - 2,68);
+    printString("reset!",maxX_ - 2,76);
+    refresh(); 
 }