Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Revision:
8:9b77eea95088
Parent:
7:cd3cafda3dd4
Child:
9:f720f5d87420
diff -r cd3cafda3dd4 -r 9b77eea95088 main.cpp
--- a/main.cpp	Wed Apr 10 09:18:25 2019 +0000
+++ b/main.cpp	Wed May 08 13:49:01 2019 +0000
@@ -6,7 +6,7 @@
 Student ID Number:201171978 Date:11/03/2019
 */
 
-// Breakout logo sprite
+/** Breakout logo sprite */
 const int breakout[10][75] =   {
     { 1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 },
     { 1,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1 },
@@ -24,50 +24,66 @@
 #include "mbed.h"
 #include "N5110.h"
 #include "Gamepad.h"
-#include "PlayerControl.h"
+#include "Paddle.h"
 #include "Ball.h"
 #include "Map.h"
 
-const int fps = 10; //sets the fps of the game doesn't change
+const int fps = 20; /** Sets the fps of the game, doesn't change */
 
 /////////////// objects ///////////////
-N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3
-Gamepad pad; // Gamepad buttons
-PlayerControl cont; // Object for player
-Ball ball;
-Map map;
+AnalogIn pot(PTB2); /** Potentiometer to set contrast of screen */
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  /** K64F - pwr from 3V3 */
+Gamepad pad; /** Gamepad buttons */
+Paddle paddle; /** Paddle object */
+Ball ball; /** Ball object */
+Map map; /** Map object */
 
 ///////////// function prototypes ///////////////
-void init();
-void welcome();
-void render();
+void init();        /** Initialize LCD and Gamepad */ 
+void welcome();     /** Simple welcome screen displayed on start-up */ 
+void render();      /** Used as the game loop */ 
+void contrast();    /** Allows to set contrast, if not suitable */ 
 
 ///////////// functions ////////////////
 int main() 
 {   
-    init();     //initialise LCD and Gamepad
-    welcome();  // waiting for the user to start
-    render();   //game loop
+    init();     /** Initialize LCD and Gamepad */ 
+    welcome();  /** Waiting for the user to start */ 
+    render();   /** Game Loop */ 
 }
 
+
 void init() 
 {
-  lcd.init();
-  pad.init();
-  lcd.setContrast(0.55); // setting contrast to 55% seems good
-  lcd.normalMode();      // normal colour mode
-  lcd.setBrightness(0.5); // put LED backlight on 50%
+  lcd.init();               /** Initialize the LCD */
+  pad.init();               /** Initialize the Gamepad */
+  lcd.setContrast(0.55);    /** Initially set contrast to 55% */
+  lcd.normalMode();         /** Normal color mode */
+  lcd.setBrightness(0.5);   /** Puts LED backlight on 50% */
 }   
 
+/** Simple welcome screen displayed on start-up */
 void welcome() 
 {
     lcd.printString("Welcome to",12,0);
-    // x origin, y origin, rows, cols, sprite
-    lcd.drawSprite(5,15,10,75,(int *)breakout);   
-    lcd.printString("Press Start",9,4);
+    lcd.drawSprite(5,15,10,75,(int *)breakout); // x origin, y origin, rows, cols, sprite
+    lcd.printString("Press A ",2,4);
+    lcd.printString("for Controls ",11,5);
     lcd.refresh();   
     
-    // flash the LEDs until start button is pressed 
+    /** Flash the LEDs until start button is pressed */
+    while (pad.check_event(Gamepad::A_PRESSED) == false) 
+    {
+
+    }
+    lcd.clear();  
+    lcd.printString("Use pot to ->",4,0);
+    lcd.printString("set contrast",4,1);
+    lcd.printString("Use Joystick ",6,2);
+    lcd.printString("to move paddle ",0,3);
+    lcd.printString("Press Start ",9,4);
+    lcd.printString("to begin ",16,5);
+    lcd.refresh();  
     while (pad.check_event(Gamepad::START_PRESSED) == false) 
     {
         for (int i = 1; i < 7; i++) 
@@ -75,31 +91,35 @@
             pad.led(i,1);
             wait(0.1);
             pad.led(i,0);
-        }
+        }  
     }
 }
 
-
+/** Resets everything if game is won / lost */
 void resetGame()
 {
-    ball.reset();
-    cont.reset();
-    map.reset();
+    paddle.reset(); /** Reset paddle */
+    ball.reset();   /** Reset ball */
+    map.reset();    /** Reset map */
 }
 
+/** Checks if game is lost */
 void checkLost()
 {
-    Vector2D posPad = cont.get_padPos();
+    const Vector2D& posPad = paddle.getPos();
+    /** Checks if ball is below paddle, i.e. lost */
     if (ball.getPos().y > posPad.y) 
     {
         while (1) 
         {
+            /** Prints a simple 'lost' message and a restart prompt */
             lcd.clear();
             lcd.printString("You Lost",17,2);
             lcd.printString("Press A",20,3);
             lcd.printString("to restart",12,4);
             wait(0.1);
             lcd.refresh();
+            /** Checks if reset button is pressed, if it is reset */
             if (pad.check_event(Gamepad::A_PRESSED) == true)
             {
                 resetGame();
@@ -109,21 +129,26 @@
     }
 }
 
-void checkWon() {
+/** Checks if level / game is won */
+void checkWon() 
+{
     if (map.checkLevel()) {
-        // we have cleared a level
-        ball.reset();
-        cont.reset();
+        /** We have cleared a level */
+        ball.reset();       /** Reset ball */
+        paddle.reset();     /** Reset paddle */
         
         if (map.hasWon()) {
+             /** We have cleared all levels */
              while (1) 
-            {
+            {   
+                /** Prints a win message and asks if you want to restart */
                 lcd.clear();
                 lcd.printString("You WON!",17,2);
                 lcd.printString("Press A",20,3);
                 lcd.printString("to restart",12,4);
                 wait(0.1);
                 lcd.refresh();
+                /** Checks if reset button is pressed, if it is reset */
                 if (pad.check_event(Gamepad::A_PRESSED) == true)
                 {
                     resetGame();
@@ -134,29 +159,49 @@
     }
 }
 
+/** Game loop */
 void render() 
 {
-    map.initBricks();
+    
+    
+    map.initBricks(); /** Initialize the map */
     
     while(1) 
-    {   
-        // physics, input + game logic
-        cont.controlPlayer(pad); 
-        cont.get_padPos(); 
-        ball.getPos();
+    {  
+        
+        /** Physics, movement, control input + game logic */
+        /** Control Input */
+        paddle.controlPaddle(pad); 
+        
+        /** Movement and physics */
         ball.move();
-        ball.hitPad(cont);
-        map.checkCollision(ball);
+        paddle.move();
+        
+        map.checkCollision(ball, paddle);
+        map.update();
+        ball.hitPad(paddle);
+        
+        /** Rendering */
+        lcd.clear();        /** Initialize render */
+        contrast();         /** Sets contrast */
+        map.drawMap(lcd);   /** Draw map objects */
+        paddle.draw(lcd);   /** Draws paddle */
+        ball.draw(lcd);     /** Draws ball */
+        
+        lcd.refresh();      /** Finalize render */
+        
+        wait(1.0f/fps);     /** Frames per second */
+        
+        /** Check Lost/Won */
         checkLost();
         checkWon();
-        
-        // rendering
-        lcd.clear(); // initialise the LCD
-        map.drawMap(lcd);
-        cont.drawPlayer(lcd);
-        ball.draw(lcd);
-        lcd.refresh();
-        
-        wait(1.0f/fps);
     }
-}
\ No newline at end of file
+}
+
+/** Allows to set contrast, if not suitable */
+void contrast() 
+{
+    float contrast = pot.read(); /** Read value from potentiometer(0-1) */
+    lcd.setContrast(contrast); /** Set that value for the contrast */
+}
+    
\ No newline at end of file