Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Revision:
6:bf90044188d0
Parent:
5:d716013c6a18
Child:
8:997f90c88246
--- a/main.cpp	Wed May 27 10:27:34 2020 +0000
+++ b/main.cpp	Thu May 28 15:02:30 2020 +0000
@@ -1,6 +1,6 @@
 /*
 ELEC2645 Embedded Systems Project
-School of Electronic & Electrical Engineering
+School of Electronic & Electrical appleering
 University of Leeds
 2019/20
 
@@ -10,7 +10,7 @@
 Date: 20/04/2020
 
 The game loop will be entered after the game is initialised. The first step is to process the
-user input (if any). The next step is to update the game engine depending on the input i.e.
+user input (if any). The next step is to update the game apple depending on the input i.e.
 moving a character or checking for collisions. The final step is to update the display and render
 this on the LCD. There is usual////
 */
@@ -20,35 +20,48 @@
 #include "Gamepad.h"
 #include "N5110.h"
 #include "snake.h"
-#include "GameEngine.h"
+#include "Apple.h"
+
+
 
 //objects
 Gamepad pad;
 Snake snake;
 N5110 lcd;
-GameEngine engine;
+Apple apple;
+Timer timer;
 //functions
 
 void welcome();
 void render();
 void init();
 void update_game_state();
+void gameover_true();
 
-int main()  
+//Serial pc(USBTX, USBRX);
+
+int main()
 {
-
+    printf("hello world");
     int fps = 6;  // frames per second
 
     init();                   // initialise and then display welcome screen...
     welcome();
     wait(1.0f/fps);  // and wait for one frame period
     render();
-                                                         //ROB atm the code works to here 
-                                                         //need to draw an initial snake    
+    timer.start();                                     //ROB atm the code works to here
+    //need to draw an initial snake
     while (1) {
-        snake.set_direction(pad);
-        update_game_state();
-        wait(1.0f/fps);                                 
+        ;
+        snake.get_position(pad);
+        snake.check_collisions();
+        snake.check_score(lcd, pad, apple, timer);
+        snake.get_timer(timer);
+
+        gameover_true();
+        snake.render(lcd, apple);
+
+        wait(1.0f/fps);
     }
 
 }
@@ -58,31 +71,22 @@
 void init()
 {
 
-    snake.init(engine);           //need to initialise snake class
+    snake.init();           //need to initialise snake class
     pad.init();
     lcd.init();
-    engine.init(48, 24, 48, 30, 48, 23);    // need to choose initial values A$AP   int shx, int shy, int apx, int apy, int Oshx, int Oshy
-}
-
-void update_game_state()   //FUNCTION 2
-{
-
-    snake.set_direction(pad);
-    snake.move_snakebody(engine, lcd);
-    
-    snake.check_scored(lcd, pad, engine);
-    snake.gameover_true(lcd);
+    apple.init(48, 24);
 
 }
 
+
+
 void welcome()
 {
-
+    lcd.refresh();
+    printf("hello world");
     lcd.printString("     SNAKE    ",0,1);
     lcd.printString("  Press Start ",0,4);
-    lcd.refresh();
 
-    // wait flashing LEDs until start button is pressed
     while ( pad.start_pressed() == false) {
         lcd.setContrast( pad.read_pot1());
         pad.leds_on();
@@ -96,27 +100,40 @@
 
 void render()
 {
-    
     lcd.clear();
     lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);
- //   Vector2D originalhead = engine.get_Snakehead(Snakehead);                                                     //draw constant rectangle around frame of lcd indicating walls
-   // Vector2D originalbody =  engine.get_oldSnakehead();
-    
-  //  lcd.setPixel(originalhead.x, originalhead.y,1);
-    //lcd.setPixel(originalbody.x, originalbody.y,1);
+    lcd.printString("     GO!      ", 0,25);
+    wait(1.0);
     lcd.refresh();
 }
 
 
-// for (int z=0; z<=_length; z++) {
-//     lcd.setPixel(snakebody[z].x, snakebody[z].y, 1);
-// }
-//use to draw wall - do i need to draw it everytime or can i do it as  constant
-//}
 
 
+void gameover_true()      //taking action if crash has occured
+{
+    bool gameover = snake.get_gameover();
+    int score = snake.get_score();
+
+    while (gameover == true) {
+        lcd.clear();
+        lcd.refresh();
+        lcd.printString( "  Game Over L ", 0, 2 );
+        snake.print_scores(lcd);
+
+        if (score <= 7) {
+            lcd.printString("    Loser     ", 0,10);
+        }
+        if (7< score <= 25) {
+            lcd.printString("    Good Job!     ", 0,10);
+        } else {
+            lcd.printString("    SNAKE PRO     ", 0,10);
+            lcd.printString(" ~.~.~.~.<8>~  ", 0, 5);
+        }
+    }
 
 
+}
 
 
 //has an apple been collected? if so increase length by 1 increase score by 1 and makes coin collecting noise and spawn new apple