ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18s2a_2

Dependencies:   mbed

Revision:
10:3e37b58e8600
Parent:
9:25597bc0cecc
Child:
12:8eb40a18f15d
--- a/main.cpp	Fri Jun 05 10:51:30 2020 +0000
+++ b/main.cpp	Fri Jun 05 18:45:05 2020 +0000
@@ -10,7 +10,7 @@
 Date: 05/03/2020
 */
 
-///////// pre-processor directives ////////
+// pre-processor directives
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
@@ -24,44 +24,42 @@
 #define SNAKE_SCORE 0
 
 
-/////////////// structs /////////////////
+// structs 
 struct UserInput {
     Direction d;
     float mag;
 };
 
 
-    
-    
-/////////////// objects ///////////////
-N5110 lcd;
-Gamepad pad;
-SnakeEngine snake;
+// objects
+N5110 lcd;  // Conects to LCD Display which is 84x48 pixels and can have 5 lines of text each contain 14 characters
+Gamepad pad; // Conects to rest of gamepad and allows access to LEDs, Controls, Sensors ect
+SnakeEngine snake; // Holds the majority of the game code and engine
 
 
-///////////// prototypes ///////////////
-void init();
+// prototypes t
+void init(); // Needed to setup the game and make sure everything is turned on
 void update_game(UserInput input);
-void render();
-void welcome();
+void render(); // Needed to make the screen work and update each frame
+void welcome(); // Displays the splash screen when the device turns on or is reset
 
-///////////// functions ////////////////
-int main()
+// functions 
+int main()    // Everything in main.cpp is contained in this 
 {
 
     int fps = 6;  // How many Frames Per Second (FPS)
 
-    init();     // turns on LCD and PAD
-    welcome();  // Waits for input
+    init();     // Turns on LCD and PAD
+    welcome();  // Welcome screen to be shown before the game starts
     
     render();  // first draw the initial frame 
-    wait(1.0f/fps);  // and wait for one frame period
+    wait(1.0f/fps);  // wait for one period
 
 
-    // game loop - read input, update the game state and render the display
+    // The game loop, this reads the input of the pad updates and and renders the frame each time. The game can be slowed down or sped up depending on the wait function
     while (1) {
-        snake.read_input(pad);
-        snake.update(pad);
+        snake.read_input(pad);  
+        snake.update(pad);      
         render();
         wait(1.0f/fps);
     }
@@ -71,9 +69,9 @@
 void init()
 {
     // need to initialise LCD and Gamepad 
-    lcd.init();
-    pad.init();
-    snake.init(APPLE_SIZE,SNAKE_SCORE, SNAKE_SPEED);
+    lcd.init(); // LCD turned on
+    pad.init(); // GamePad turned on
+    snake.init(APPLE_SIZE,SNAKE_SCORE, SNAKE_SPEED); //Initiates the values based on what is on the top of this file
 }
 
 // this function draws each frame on the LCD
@@ -103,199 +101,7 @@
     lcd.printString("  Press Start ",0,4);
     lcd.refresh();
     lcd.clear();
-    while ( pad.start_pressed() == false) {
+    while ( pad.start_pressed() == false) { // Will keep in the welcome state until the start button is pressed then it will go onto the game!
      }
      
-    }
-
-
-
-
-/*
-
-
-bool endGame;
-int borderW, borderH, appleX, appleY, snakeX, snakeY, score; 
-enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
-eDirection dir;
-
-         
-
-void setup()                          // Sets up the values for the game    
-{
-    srand(time(0));                   // Seed the generator, give it a starting value
-    endGame = false;                  // Sets the game as not finished
-    dir = STOP;                       // When the game starts the snake isn't moving
-    
-    borderW = WIDTH;                  // Border width and height
-    borderH = HEIGHT-8;               // Uses - 8 so we can get a line of text for the score
-   
-    snakeX = borderW/2;               // Snake Starts at Center
-    snakeY = borderH/2+8;
-   
-    appleX = rand() % borderW;        // Apple position 
-    appleY = rand() % borderH;      
-    
-    score = 0;                        // Sets the initial Score to 0
- 
-}
-
-void display()   // Deals with the fuctions of the display
- {
-        lcd.clear();   
-        lcd.drawRect(0,8,borderW,borderH,FILL_TRANSPARENT);      // Draws a border showing the game area
-        
-        char buffer[14];                                         // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-        int ScoreLength = sprintf(buffer,"Score:%2d",score);     // Shows the player the score
-        if (ScoreLength <= 14) 
-            lcd.printString(buffer,WIDTH/2-25,0);                // centers the score
-            
-            
-   lcd.drawRect(snakeX,snakeY,5,5,FILL_BLACK);                   // Draws the initial snake head 
-   
-   
-   lcd.drawCircle(appleX,appleY,2,FILL_BLACK);                   // Draws a circle representing an apple
-            
-  lcd.refresh();
-     
-     
-     }   
-
-void control()   // Reads the controls
-                    {
-            if (pad.A_pressed()){
-                        dir = RIGHT;
-                        }
-             if (pad.B_pressed()){
-                        dir = DOWN;
-                        }
-             if (pad.X_pressed()){
-                        dir = UP;
-                        }
-             if (pad.Y_pressed()){
-                        dir = LEFT;
-                        }       
-             if (pad.Y_pressed()){
-                        dir = LEFT;
-                        }  
-            if (pad.start_pressed()){   // Stops the Movement
-                        dir = STOP;
-                        } 
-                         
-                           
- 
-}
-
-void gameplay() {
-
-   // Change the direction the snake goes dependant on the input 
-    
-    if (dir == UP) {
-                 --snakeY;
-                 }
-    if   (dir == DOWN) {
-                 ++snakeY;
-                 }  
-    if   (dir == LEFT) {
-                --snakeX;
-                 }  
-    if   (dir == RIGHT) {
-                ++snakeX;
-                 }  
-  
-                                                        
-                                
-}
-
-
-void menu() {
-         while (1) { 
-     
-     pad.led(3,1);  // Only Show Green LEDS
-     pad.led(6,1);  //
-         
-         // Splash Screen Info
-   lcd.clear(); // we need to clear the screen first
-   lcd.printString("    Author   ",0,1);  
-   lcd.printString("Simon Atkinson",0,2); 
-   lcd.printString("   201255483  ",0,3); 
-   lcd.printString(" Uni of Leeds ",0,4); 
-   lcd.refresh(); // need to refresh display after setting pixels or writing strings
-           wait(1.0); // we don't want this screen on long!
-            
-       // 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();
-    wait(45.0);
-    lcd.clear();
-            
-    // Easter Egg time! 
-    pad.led(3,0);  // Turn Green LEDs Off
-     pad.led(6,0);  //
-     pad.led(2,1);  // Turn on Amber LEDs 
-     pad.led(5,1);  //
-    lcd.printString("  Do you need  ",0,1);  
-    lcd.printString("  more time?   ",0,2); 
-    lcd.printString("  Grandpa! ",0,3);
-    lcd.refresh();
-    wait(5.0);
-               
-    //returns back to normal welcome screen           
-    lcd.clear();
-        pad.led(3,1);  // Turn Green LEDs On
-     pad.led(6,1);  //
-     pad.led(2,0);  // Turn  off Amber LEDs 
-     pad.led(5,0);  //
-    lcd.printString("  Welcome to  ",0,1);  
-    lcd.printString("    Snake!   ",0,2); 
-    lcd.printString("  Press Start ",0,4);
-    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!
-}
-    
-    
-    }
-
-
-
-
-///////////// 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();  
-
-}
-
-} */
-
-
-
+}  //end of main.cpp