ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Revision:
34:a9b14a4ccd46
Parent:
33:de130e274391
Child:
36:0c852c5ade4b
--- a/main.cpp	Thu May 09 11:39:59 2019 +0000
+++ b/main.cpp	Thu May 09 14:07:09 2019 +0000
@@ -18,19 +18,18 @@
 # include "tests.h"
 #endif
 
-// structs
 struct UserInput {
     Direction d;
     float mag;
 };
 
-// objects
+////////////////////////    OBJECTS     ///////////////////////////////////
 N5110 lcd(PTC5,PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // START, LCD SCE, LCD RST, LCD DC, LCD MOSI, LCD CLK, LCD Backlight
 Gamepad pad;
 Engine eng;
 Doodler dood;
 
-// functions
+//////////////////////// FUNCTIONS DECLARED //////////////////////////////
 void init();
 void update_game(UserInput input);
 void render();
@@ -38,6 +37,8 @@
 void check_score();
 void game_over();
 
+
+////////////////////////// MAIN CODE /////////////////////////////////////
 int main()
 {
 #ifdef WITH_TESTING
@@ -66,21 +67,23 @@
     }
 }
 
-// initialies all classes and libraries
-void init()
-{  // need to initialise LCD and Gamepad
+
+////////////////////////// FUNCTIONS CODE /////////////////////////////////////
+void init()  // initialies all classes and libraries, LCD and Gamepad
+{  
     lcd.init();
     lcd.setBrightness(1);
     lcd.setContrast(0.5),
-                    pad.init();
+    pad.init();
     eng.init();
 }
 
-void render()
+// prints the current engine screen
+void render() 
 {
-    lcd.clear();
-    eng.draw(lcd);
-    lcd.refresh();
+    lcd.clear();  // clears the previous screen display 
+    eng.draw(lcd);  // sets the current updated screen display
+    lcd.refresh();  // prints the current screen diplay 
 }
 
 // Starting menu screen display
@@ -88,8 +91,8 @@
 {
     lcd.printString(" Doodle Jump! ",0,1);
     lcd.printString("  Press Start ",0,4);
-    lcd.refresh();
-    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+    lcd.refresh(); 
+    while ( pad.check_event(Gamepad::START_PRESSED) == false) {  // infinite loop until START pressed
         pad.leds_on();
         wait(0.1);
         pad.leds_off();
@@ -97,25 +100,31 @@
     }
 }
 
+// Checks if the previous score has increased by one right after one to correct an infinite addition
 void check_score()
 {
     bool score_added = eng.get_score_check();
     eng.update(pad);
     bool updated_score_added = eng.get_score_check();
-    if ( (score_added == true) && (updated_score_added == true) ) {
-        eng.subtract_score();
-    }  // checking if the previous score has increased by one right after one update means that the a floor
-}  // remains fixed at y = 10 (which adds a score). This will then prevent the score from increasing infinitely.
+    if ( 
+    (score_added == true) &&  // conditions indicate the score has been added sequentially after two updates
+    (updated_score_added == true)  // this means the same floor has remained in the position that sets an addition, since it is
+    )  // an impossibility for it to be another floor (floors have specific y-coordinate separation so there is never one next to another)
+    {
+        eng.subtract_score();  // subtracts the incorrect addition
+    }  
+}  
 
+// Called on the main function to display the game over screen when the game has ended
 void game_over()
 {
     lcd.clear();
     lcd.printString("Game Over!",3,1);
     lcd.printString("Press back",3,3);
     int score = eng.get_score();
-    char buffer[14];
-    sprintf(buffer,"SCORE:%d", score);
-    lcd.printString(buffer, 3, 5);
+    char text[14]; 
+    sprintf(text,"SCORE:%d", score);
+    lcd.printString(text, 3, 5);
     lcd.refresh();
     while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
         pad.leds_on();