ELEC2645 (2018/19) / Mbed 2 deprecated el17cr

Dependencies:   mbed

Revision:
7:cf469c3505a2
Parent:
6:85314a3d69cd
--- a/main.cpp	Mon May 06 09:37:34 2019 +0000
+++ b/main.cpp	Thu May 09 06:22:13 2019 +0000
@@ -2,10 +2,10 @@
 ELEC2645 Embedded Systems Project
 School of Electronic & Electrical Engineering
 University of Leeds
-Name:
-Username:
-Student ID Number:
-Date:
+Name: Connor Rainey
+Username: el17cr
+Student ID Number: 201125971
+Date: 09/05/2019
 */
 
 #include "mbed.h"
@@ -13,103 +13,105 @@
 #include "N5110.h"
 #include "Falldown.h"
 
-//#define GROUND_WIDTH 70
+// These values cannot be changed
 #define GROUND_HEIGHT 2
-//#define GROUND_WIDTH 50
 #define BALL_SIZE 3
-//#define LINE_HEIGHT 1
-//#define LINE_WIDTH WIDTH
 
 
+// define initial ground width, ground speed and level
 int ground_width = 40;
-double ground_speed = 0.5;
+double ground_speed = 0.3;
 int level = 1;
 
-
+// struct holds data for reading joystick
 struct UserInput {
     Direction d;
     float mag;
 };
 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3
-Gamepad pad;
-Falldown falldown;
+Gamepad pad; //gamepad library
+Falldown falldown; // falldown class
 
 void init();
 void update_game(UserInput input);
 void render();
-void Level();
 void welcome();
+void game_over();
+void game_complete();
 
 
 int main()
 {
     init();
     welcome();
-    //Level();
     render();
-    wait(0.1);
-    while (1) {
+    wait(0.1); //delay required before clearing the display and constructing the next frame
+
+    // start game loop
+    while(1) {
         falldown.read_input(pad);
         falldown.update(pad);
         render();
         wait(0.1);
+        // check goal funtion returns a 1 if ball reaches the bottom of the screen
         if (falldown.check_goal() == 1) {
-            //falldown.read_input(pad);
-            //falldown.update(pad);
-            //render();
-            level = level++;
-            ground_width = ground_width + 2;
-            ground_speed = ground_speed + 0.2;
-            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed);
-            //ground_width = ground_width + 10;
+            level = level++; // increment level if goal reached
+            ground_width = ground_width + 1; // increases ground width
+            ground_speed = ground_speed + 0.1; // increase ground speed
+            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
             wait(0.1);
-            //clear lines
-            //redraw lines
+
+            // if on final level, and goal reached
+        } else if ((level == 5) && (falldown.check_complete() == 1)) {
+            game_complete(); // call game complete function
+            wait(0.5);
+            level = 1; // if game complete function is broken and player chooses to play again, re-set values for level, ground width and ground speed
+            ground_width = 40;
+            ground_speed = 0.5;
+            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
+            wait(0.1);
+
+            //
+        } else if (falldown.check_end() == 1)  {
+            game_over(); // call game over function
+            level = 1; // if game over function is broken and player chooses to play again, re-set values for level, ground width and ground speed
+            ground_width = 40;
+            ground_speed = 0.5;
+            falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed); // re-initialise game with new values
+            wait(0.1);
         }
     }
-    /*while (falldown.check_goal == 1) {
+}
 
-        falldown.init(GROUND_HEIGHT,BALL_SIZE);
-        wait(0.1);*/
-    }
-
-
-void init()
+void init() // initialise lcd, gamepad and game
 {
     lcd.init();
     pad.init();
-    
+
     falldown.init(GROUND_HEIGHT,ground_width,BALL_SIZE,ground_speed);
 }
 
-void render()
+void render() // render game
 {
+    lcd.clear(); //clear lcd
+    falldown.draw(lcd); //draws game
+    char buffer[1]; //displays level
+    sprintf(buffer,"%2d",level);
+    lcd.printString(buffer,0,1);
+    lcd.refresh();
+}
 
-    lcd.clear();
-    falldown.draw(lcd);
-    char buffer[1];
-    sprintf(buffer,"%2d",level);
-    lcd.printString(buffer,1,1);
+void welcome() //Displays welcome screen
+{
+    lcd.printString("*Falldown*",12,0);
+    lcd.printString("Get to",24,2);
+    lcd.printString("the bottom!",11,3);
+    lcd.printString("Press Start",11,5);
+
     lcd.refresh();
 
-}
-/*
-void Level() 
-{
-    int level = 1;
-    char buffer[14];
-    sprintf(buffer,"%2d",level);
-    lcd.printString(buffer,0,0);
-}
-*/
-
-void welcome() 
-{
-    lcd.printString("Falldown",0,1);
-    lcd.printString("Press Start",0,4);
-    lcd.refresh();
-    
+    // leds flash until start pressed
     while ( pad.check_event(Gamepad::START_PRESSED) == false) {
         pad.leds_on();
         wait(0.1);
@@ -117,3 +119,45 @@
         wait(0.1);
     }
 }
+
+void game_over()
+{
+    lcd.init(); //re initialise lcd
+    lcd.printString("Game Over! :-(",0,0);
+    lcd.printString("centre",25,2);
+    lcd.printString("joystick",20,3);
+    lcd.printString("Play again? A",0,5);
+    lcd.refresh();
+    while (pad.check_event(Gamepad::A_PRESSED) == false) {
+        pad.leds_on();
+        wait(0.1);
+        pad.leds_off();
+        wait(0.1);
+        if (pad.check_event(Gamepad::A_PRESSED) == true) {
+            break;  // break while loop when button is pressed and continue main game loop
+        }
+    }
+    wait(0.5);
+    pad.init(); //bug fix, initialise pad ensures button does remain true from last time it was pressed
+}
+
+// Function adjusted for completion of the game
+void game_complete()
+{
+    lcd.init();
+    lcd.printString("Game Complete!",0,0);
+    lcd.printString(":-)",33,2);
+    lcd.printString("Play again? A",0,4);
+    lcd.refresh();
+    while (pad.check_event(Gamepad::A_PRESSED) == false) {
+        pad.leds_on();
+        wait(0.1);
+        pad.leds_off();
+        wait(0.1);
+        if (pad.check_event(Gamepad::A_PRESSED) == true) {
+            break;
+        }
+    }
+    wait(0.5);
+    pad.init();
+}