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:
Sat Mar 07 18:14:26 2015 +0000
Parent:
3:00c0f63f4408
Child:
5:d395a7134278
Commit message:
platform animations complete. Began working on main routine. Timers in place for animation of platforms and gradual speed up.; ; Currently working on: isBallFalling()

Changed in this revision

GameScreen.cpp Show annotated file Show diff for this revision Revisions of this file
GameScreen.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GameScreen.cpp	Sat Mar 07 13:55:08 2015 +0000
+++ b/GameScreen.cpp	Sat Mar 07 18:14:26 2015 +0000
@@ -125,7 +125,7 @@
     } 
 }
 
-void GameScreen::shiftAllPlatformsUp()
+void GameScreen::shiftAllPlatforms()
 {
     for (int n = 0; n < numPlatforms_ ; n++)
     {
@@ -137,4 +137,6 @@
             allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap!
         }
     }
-}
\ No newline at end of file
+}
+
+
--- a/GameScreen.h	Sat Mar 07 13:55:08 2015 +0000
+++ b/GameScreen.h	Sat Mar 07 18:14:26 2015 +0000
@@ -5,7 +5,7 @@
 #include "N5110.h"
 
 //GameScreen class is an extension of the base Nokia Library Created by Craig Evans
-// This extension adds functionality relevant to 'Fall Down Game'. 
+// This extension adds functionality relevant to 'Fall Down Game'.
 // drawPlatform, drawBall, drawScore etc....
 // # of platforms defined and can be altered/
 // updates platforms at speed proportional to speedOfPlatforms_
@@ -21,36 +21,48 @@
 struct Platform {
     int id;     //for identifying.
     int x;      //col of gap
-    int y;      //row    
+    int y;      //row
+};
+
+struct Ball {
+    int x;
+    int y;
 };
 
 class GameScreen: public N5110::N5110
-{   
+{
 public:
     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 :)
-    
+        :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();
-    
+
     //draw horizontal platform where y top pixel layer location. x hole location
-    void drawPlatform(int x,int y);
-    void erasePlatform(int y);
+
     void drawBall(int x, int y);
     void eraseBall(int x, int y);
     void createAllPlatforms();
-    void freeAllPlatforms();        //garbage cleanup
     void drawAllPlatforms();
     void eraseAllPlatforms();
-    void shiftAllPlatformsUp();     //move all platforms up 1 pixel        
+    void shiftAllPlatforms();     //move all platforms up 1 pixel
     
-    //Read Only
-    int maxY(){ return maxY_; }
-    int maxX(){ return maxX_; }
-    int platThickness() {return platThickness_;}
-    int ballRadius() {return ballRadius_;}
+    //Write Access
+    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_;    }
+    int         getBallR()                      { return ballRadius_;       }
+    int         getBallX()                      { return playerBall.x;      }
+    int         getBallY()                      { return playerBall.y;      }
     
-    Platform *allPlatforms[6];      //public for testing...
-    
+private:
+    void drawPlatform(int x,int y);
+    void erasePlatform(int y);
+    void freeAllPlatforms();        //garbage cleanup
+
 private:
     static const int platGapSize_ = 8;          //standard platform gap width in pixels
     static const int platThickness_ = 2;        //platform thickness in pixels
@@ -59,7 +71,8 @@
     static const int maxY_ = 84;                // maximum vertical pixel
     static const int ballRadius_ = 4;           // size of player ball
     static const int numPlatforms_ = 6;         // total number of platforms
-    //Platform *allPlatforms[numPlatforms_];      //array used to track each platform, and refresh when needed!    
+    Ball playerBall;
+    Platform *allPlatforms[numPlatforms_];      //array used to track each platform, and refresh when needed!
 };
 
 
--- a/main.cpp	Sat Mar 07 13:55:08 2015 +0000
+++ b/main.cpp	Sat Mar 07 18:14:26 2015 +0000
@@ -2,70 +2,103 @@
 #include "N5110.h"
 #include "GameScreen.h"
 
+#define kLevelTime 10       //time on each level
+#define kStartingSpeed 0.5
+#define kBallFallSpeed 0.2  //
+
 Serial serial(USBTX,USBRX);
 GameScreen lcd(p7,p8,p9,p10,p11,p13,p21);
+BusOut leds(LED4,LED3,LED2,LED1);
 
-void platformStatus ();
+Ticker levelTicker;     //timer for when to advance difficulty of game
+int gameLevel = 1;      
+
+
+Ticker platformTicker;  //timer for when to move platforms
+bool isPlatformExpired = false;
+
+Ticker ballTicker;      // timer for when to move ball
+
+void levelTickerExpired();
+void platformTimerExpired();
+void advanceAndCheckPlatforms();
+bool isBallFalling();
+
+
+
 
 int main()
 {
     lcd.Initialize();
-    serial.printf("max Y = %d \n",lcd.maxY());
- 
-    //platform move code
-    /*
-    for (int y = (lcd.maxY() - lcd.platThickness()); y > 0; y --) {
-        lcd.drawPlatform(4,y);
-        lcd.refresh();
-        wait(0.1);
-        lcd.erasePlatform(y);        
-    }
-    
+    lcd.createAllPlatforms();
+    leds = 1;  
+    lcd.drawAllPlatforms();
+    lcd.refresh();
     
-    for(int y = (lcd.maxY() - lcd.platThickness()); y > 0; y -= 14) {
-        lcd.drawPlatform(4,y);
-    }
+    levelTicker.attach(&levelTickerExpired,kLevelTime);  
+    platformTicker.attach(&platformTimerExpired,kStartingSpeed);
     
-    for(int x = (lcd.maxX() - lcd.ballRadius()); x > 0; x --)
-    {
-        serial.printf("x = %d\n",x);
-        lcd.drawBall(x,8);
-        lcd.refresh();
-        wait(0.1);
-        lcd.eraseBall(x,8);
-    }
-    */
-    
-    lcd.createAllPlatforms();
-    
-    
+    //MAIN GAME LOOP
     while(true)
     {        
-        lcd.drawAllPlatforms();
-        //serial.printf("SUCCESS");
-        lcd.refresh();
-        wait(0.1);
-        lcd.eraseAllPlatforms();
-        lcd.shiftAllPlatformsUp();
-        platformStatus();
+        if(isPlatformExpired)
+        {
+            advanceAndCheckPlatforms();
+        }
+        
     }
-    
-    
-
-    
-    
-    serial.printf("OUT");
-    
-    
-    
-    
-    
-    
+ 
     return 1;
 }
 
-void platformStatus ()
+
+void levelTickerExpired()
+{
+    gameLevel ++;    
+    leds = gameLevel;
+    //platformTicker.attach(&platformTickerExpired,) //will be used to change platform speed!!
+}
+
+void platformTimerExpired()
+{
+    isPlatformExpired = true;    
+}
+
+void advanceAndCheckPlatforms()
 {
-    for (int n = 0; n < 6; n++)
-        serial.printf("id= %d   x= %d   y= %d \n",lcd.allPlatforms[n]->id,lcd.allPlatforms[n]->x,lcd.allPlatforms[n]->y);  
+    //ADVANCE
+    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.    
+    }     
+    
+    lcd.refresh();
+}
+
+//NOT COMPLETE
+bool isBallFalling()
+{
+        
+        for (int i = 0; i < 6;i++)
+        {
+            Platform tempPlatform = lcd.getPlatformData(i);
+
+            //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