Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

Revision:
2:d4402bc3dd45
Parent:
1:3305d7e44880
Child:
3:00c0f63f4408
--- a/GameScreen.cpp	Sat Mar 07 09:58:29 2015 +0000
+++ b/GameScreen.cpp	Sat Mar 07 13:49:53 2015 +0000
@@ -6,27 +6,132 @@
 void GameScreen::Initialize()
 {
     init();
-    
-    platGapSize = 4;
-    platThickness = 2;
-    maxX_ = 48;
-    maxY_ = 84;
-    
-        
+
 }
 
 //draw platform
 //  ___________  ________  < y
 //               ^ x       
 void GameScreen::drawPlatform(int x,int y)
-{
-
-    
+{    
     for (int a = 0; a < 48; a ++)
     {
-        for (int b = y; b < (y+platThickness); b++)
+        for (int b = y; b < (y+platThickness_); b++)
         {
+            //skip pixels of gap
+            if (a > x && a < (x + platGapSize_))
+                break;
+                
+            
+            
+            //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
+            if (b > (maxY_ - 1))
+                break;
+            
             setPixel(b,a);    
         }
     }
+}
+
+//erase platform
+void GameScreen::erasePlatform(int y)
+{
+    for (int a = 0; a < 48; a ++)
+    {
+        for (int b = y; b < (y+platThickness_); b++)
+        {            
+            //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
+            if (b > (maxY_ - 1))
+                break;
+            
+            clearPixel(b,a);    
+        }
+    }
+}
+
+//draw the player ball where (x,y) is top right corner.
+//      XXP <-- P(x,y) 
+//     XXXX
+//     XXXX
+//      XX    
+void GameScreen::drawBall(int x, int y)
+{   
+    //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);    
+}
+
+//draw the player ball where (x,y) is top right corner.
+void GameScreen::eraseBall(int x, int y)
+{   
+    //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);    
+}
+
+void GameScreen::createAllPlatforms() 
+{            
+    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...
+        newPlatform->y = maxY_ - n*platSpacing_ - platThickness_ + 1;   
+        allPlatforms[n] = newPlatform;
+    }
+}
+
+void GameScreen::freeAllPlatforms()
+{
+    for (int n = 0; n < numPlatforms_ ; n++)
+    {
+        delete allPlatforms[n];
+    }    
+}
+
+void GameScreen::drawAllPlatforms()
+{
+    for (int n = 0; n < numPlatforms_ ; n++)
+    {
+        drawPlatform (allPlatforms[n]->x,allPlatforms[n]->y);
+    } 
+}
+
+void GameScreen::eraseAllPlatforms()
+{
+    for (int n = 0; n < numPlatforms_ ; n++)
+    {
+        erasePlatform (allPlatforms[n]->y);
+    } 
+}
+
+void GameScreen::shiftAllPlatformsUp()
+{
+    for (int n = 0; n < numPlatforms_ ; n++)
+    {
+        if (allPlatforms[n]->y > (platThickness_))
+            allPlatforms[n]->y = allPlatforms[n]->y - 1;
+        else
+            allPlatforms[n]->y =  maxY_ - platThickness_ + 1; //send back to bottom.
+    }
 }
\ No newline at end of file