Owen Cavender 201159294

Dependencies:   mbed

Revision:
2:ffbfd3f53ee2
Parent:
1:897160a1a3ae
diff -r 897160a1a3ae -r ffbfd3f53ee2 main.cpp
--- a/main.cpp	Tue May 26 12:17:59 2020 +0000
+++ b/main.cpp	Sat May 30 06:12:09 2020 +0000
@@ -1,127 +1,119 @@
 /*
 ELEC2645 Embedded Systems Project
-School of Electronic & Electrical Engineering
+School of Electronic & Electrical appleering
 University of Leeds
 2019/20
 
+/////////////SNAKE ///////////
 Name: Owen Cavender
 Username: el17oc
 Student ID Number: 201159294
 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.
-moving a character or checking for collisions. The final step is to update the display and render
-this on the LCD. There is usual
 */
 
-// includes
-#include "mbed.h"
+//////////////////////////////
+
+//includes
+#include "mbed.h"                        //addresses each header file of external classes being used in the programme
 #include "Gamepad.h"
 #include "N5110.h"
 #include "snake.h"
 #include "GameEngine.h"
 
+
 //objects
 Gamepad pad;
 Snake snake;
 N5110 lcd;
 GameEngine engine;
+
 //functions
-
 void welcome();
 void render();
 void init();
-void update_game_state();
+void update_snake();
+
 
-int main()  //FUNCTION 0
+//Serial pc(USBTX, USBRX);
+
+int main()
 {
 
-    int fps = 6;  // frames per second
+    init();                   //initialises game objects
+    welcome();                //displays welcome screen before entering game loop
+
+
+
+    while (1) {
+
 
-    init();                   // initialise and then display welcome screen...
-    welcome();
-    wait(1.0f/fps);  // and wait for one frame period
-    render();
+        update_snake();                                                     //reads input from gamepad and updates the position of the snake - this is the function influences how the other functions behave
+        snake.check_gameover(lcd);                                          //checking if any one of the three game over conditions is true
+        //   snake.gameover_true(lcd, pad);                                      //checking if an apple has been collected - increases the score and generates new apple position if apple has been collected
+        snake.apple_collected(lcd, pad);
+        snake.get_Apple_position(lcd);
+        engine.get_LEDs(pad, snake);
 
-    // game loop - read input, update the game state and render the display
-    while (1) {
-        snake.set_direction(pad);
-        update_game_state();
-        wait(1.0f/fps);                                 //need to make wait function - relate it to clock
+        engine.print_countdown(lcd, snake);                                //prints the value of the countdown counter - it will have incremented by 25 if an apple has been collected and would have decreased by 1 if not
+        snake.render(lcd, pad);                                            //prints the snake, the apple, the score and the border
+        //      snake.print_display_time(lcd);
+
+
+        wait(0.01);
     }
 
 }
 
-
-
 void init()
 {
+    lcd.init();             //initialises the lcd
+    pad.init();             //initialises the gamepad
+    snake.init();           //initialises snake class
 
-    snake.init();           //need to initialise snake class
-    pad.init();
-    lcd.init();
-    engine.init(48, 24, 48, 30);    // need to choose initial values A$AP   shx shy apx apy
+
 }
 
-void update_game_state()   //FUNCTION 2
+void update_snake()
 {
-
-    snake.set_direction(pad);
-    snake.move_and_draw_snake(lcd);
-    snake.gameover_true(lcd);
-    snake.check_if_scored(lcd, pad);
-
+    snake.get_direction(pad);                                          //sets the direction based on buttons pressed on the Gamepad
+    snake.render_clear_tail(lcd);                                      //clears the trailing pixel of the snake before updating position to avoid the snake growing with each move - the trailing pixel would be locked and fixed as 1 in its initial position without this
+    // =needs to be cleared before _x3, _y3 is updated
+    snake.move_snake();                                                //alters the coordinate of each snake bit in accordance with the Gamepad instruction
+    engine.get_LEDs(pad, snake);                                       //Turns on on LEDS based on where the snake is in the box
 }
 
 void welcome()
 {
 
-    lcd.printString("     SNAKE    ",0,1);
-    lcd.printString("  Press Start ",0,4);
-    lcd.refresh();
+
+    while ( pad.start_pressed() == false) {             //main menu - wont enter game loop until start is pressed
+        lcd.printString("     SNAKE    ",0,1);          //
+        lcd.printString("COLLECT APPLES",0,3);          //game task
+        lcd.printString("  Press Start ",0,5);          //instruction
+        lcd.setContrast( pad.read_pot1());              //adjust contrast
+
 
-    // wait flashing LEDs until start button is pressed
-    while ( pad.start_pressed() == false) {
-        lcd.setContrast( pad.read_pot1());
-        pad.leds_on();
+        pad.leds_off();
+        pad.led(1,1);
+        wait(0.2);
+        pad.led(1,0);                                   //turning on the LEDS in a sequence which reflects the snakes movement
+        pad.led(2,1);
+        wait (0.2);
+        pad.led(2,0);
+        pad.led(3,1);
+        wait(0.2);
+        pad.led(3,0);
         wait(0.1);
-        pad.leds_off();
-        wait(0.1);
+        pad.led(6,1);
+        wait (0.2);
+        pad.led(6,0);
+        pad.led(5,1);
+        wait (0.2);
+        pad.led(5,0);
+        pad.led(4,1);
+        wait(0.2);
+
+        lcd.refresh();                              //updates screen
     }
 
 }
-
-
-//void render(N5110 &lcd)
-//{
-void render()
-{
-    // clear screen, re-draw and refresh
-    lcd.clear();
-    lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);
-    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
-//}
-
-
-
-
-
-
-//has an apple been collected? if so increase length by 1 increase score by 1 and makes coin collecting noise and spawn new apple
-//has snake touched itself or wall - if so end game - present score - make xxx sound fail --- apple needs to be different to the snakes body and different to wall
-//has button button been pressed - if so change direction accordingly
-//max length - when length = width x height - 1 - game complete
-//each pixel of length must follow the front pixel
-// 2 modes - time race - "get an apple before time runs out" - resets timer --- then classic snake
-
-
-
-//need to add audio, leds, (main menu end menu and entering and leaving the while(1) loop), time trial mode, .h files
\ No newline at end of file