ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18s2a_2

Dependencies:   mbed

Revision:
7:9bd49beccdd1
Parent:
6:8e2fca142827
Child:
8:32825d724856
--- a/main.cpp	Thu Jun 04 19:13:21 2020 +0000
+++ b/main.cpp	Thu Jun 04 21:04:55 2020 +0000
@@ -15,14 +15,91 @@
 #include "Gamepad.h"
 #include "N5110.h"
 #include "Bitmap.h"
-#include <ctime>
+#include "SnakeEngine.h"
+
+#define SCORE 2
+
+
+/////////////// structs /////////////////
+struct UserInput {
+    Direction d;
+    float mag;
+};
+/////////////// objects ///////////////
+N5110 lcd;
+Gamepad pad;
+SnakeEngine snake;
+
+///////////// prototypes ///////////////
+void init();
+void update_game(UserInput input);
+void render();
+void welcome();
+
+///////////// functions ////////////////
+int main()
+{
+
+
+    int fps = 6;  // frames per second
+
+    init();     // initialise and then display welcome screen...
+    welcome();  // waiting for the user to start
+    
+    render();  // first draw the initial frame 
+    wait(1.0f/fps);  // and wait for one frame period
+
+
+    // game loop - read input, update the game state and render the display
+    while (1) {
+        snake.read_input(pad);
+        snake.update(pad);
+        render();
+        wait(1.0f/fps);
+    }
+}
+
+// initialies all classes and libraries
+void init()
+{
+    // need to initialise LCD and Gamepad 
+    lcd.init();
+    pad.init();
+     
+
+
+}
+
+// this function draws each frame on the LCD
+void render()
+{
+    // clear screen, re-draw and refresh
+    lcd.clear();  
+    snake.draw(lcd);
+    lcd.refresh();
+}
+
+// simple splash screen displayed on start-up
+void welcome() {
+    pad.led(3,1);  // Only Show Green LEDS
+    pad.led(6,1);  //
+            
+       // main menu screen no interaction yet pressing start won't do anything but its a start if you pardon the pun!     
+    lcd.clear();
+    lcd.printString("  Welcome to  ",0,1);  
+    lcd.printString("    Snake!   ",0,2); 
+    lcd.printString("  Press Start ",0,4);
+    lcd.refresh();
+    lcd.clear();
+    while ( pad.start_pressed() == false) {
+     }
+     
+    }
 
 
 
 
-/////////////// objects ///////////////
-N5110 lcd;
-Gamepad pad;
+/*
 
 
 bool endGame;
@@ -32,7 +109,6 @@
 
          
 
-
 void setup()                          // Sets up the values for the game    
 {
     srand(time(0));                   // Seed the generator, give it a starting value
@@ -86,7 +162,15 @@
                         }
              if (pad.Y_pressed()){
                         dir = LEFT;
-                        }          
+                        }       
+             if (pad.Y_pressed()){
+                        dir = LEFT;
+                        }  
+            if (pad.start_pressed()){   // Stops the Movement
+                        dir = STOP;
+                        } 
+                         
+                           
  
 }
 
@@ -111,34 +195,9 @@
                                 
 }
 
-///////////// functions ////////////////
-int main()
-{
-//initialise Display and gamepad
-    lcd.init();                       // Turns the LCD display on
-    pad.init();                       // Turns the gamepad on
-    pad.leds_off();                   // Turns the LEDs OFF
 
-// Main Game Section
-
-setup();
-while (!endGame)
-{
-    display();
-    control();
-    gameplay();
-    wait (0.06);                       // Sets how often the while loop functions can adjust the speed of the game!
-}
- 
- 
- 
- /*  Working on game will bring welcome screen back in
- 
-  // first need to initialise display
-    lcd.init();
-    pad.leds_off();
-
-     while ( pad.start_pressed() == false) { 
+void menu() {
+         while (1) { 
      
      pad.led(3,1);  // Only Show Green LEDS
      pad.led(6,1);  //
@@ -181,26 +240,50 @@
     lcd.printString("  Welcome to  ",0,1);  
     lcd.printString("    Snake!   ",0,2); 
     lcd.printString("  Press Start ",0,4);
-    lcd.refresh();
+    lcd.refresh();  
+} 
+    }
 
+void ingame() {
     
+    while (!endGame)
+{
+    display();
+    control();
+    gameplay();
+    wait (0.06);                       // Sets how often the while loop functions can adjust the speed of the game!
 }
+    
+    
+    }
+
+
 
 
-     while ( pad.start_pressed() == true) { 
-     pad.leds_off();
-     pad.led(0,1);  // Only Show Red LEDS
-     pad.led(3,1);
-         
-         // Splash Screen Info
-   lcd.clear(); // we need to clear the screen first
-   lcd.printString(" Placeholder  ",0,1);  
-   lcd.printString("  Game Coming ",0,2); 
-   lcd.printString("     Soon     ",0,3); 
-   lcd.printString("      :)      ",0,4); 
-   lcd.refresh(); // need to refresh display after setting pixels or writing strings
+///////////// functions ////////////////
+int main()
+{
+//initialise Display and gamepad
+    lcd.init();                       // Turns the LCD display on
+    pad.init();                       // Turns the gamepad on
+    pad.leds_off();                   // Turns the LEDs OFF
+
+// Main Game Section
+
+setup();
+
+
+
+ if (pad.start_pressed()){ 
+  menu();
+} 
  
-    
-} */ 
+ else {
+   ingame();  
 
 }
+
+} */
+
+
+