Final Submission. I have read and agreed with Statement of Academic Integrity.

Dependencies:   mbed Gamepad N5110 Joystick

Files at this revision

API Documentation at this revision

Comitter:
el16dlc
Date:
Thu May 09 13:52:41 2019 +0000
Parent:
9:a7ea33e6bd82
Commit message:
Final

Changed in this revision

Game_engine/Game_engine.cpp Show annotated file Show diff for this revision Revisions of this file
Game_engine/Game_engine.h Show annotated file Show diff for this revision Revisions of this file
Snake/Snake.cpp Show annotated file Show diff for this revision Revisions of this file
Snake/Snake.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r a7ea33e6bd82 -r aedca0082855 Game_engine/Game_engine.cpp
--- a/Game_engine/Game_engine.cpp	Thu May 09 11:36:21 2019 +0000
+++ b/Game_engine/Game_engine.cpp	Thu May 09 13:52:41 2019 +0000
@@ -1,21 +1,24 @@
 #include "Game_engine.h"
 #include "Snake.h"
 
+// constructor and destructor empty
 GameEngine::GameEngine() {
 }
+GameEngine::~GameEngine() {
+}
 
-GameEngine::~GameEngine() {
-}
+// Instance
 Snake snake;
 
 void GameEngine::direction_reset() {
-    _direction = 0;    
+    _direction = 0; // resets direction so menu can be selected using buttons
 }
+
 void GameEngine::init() {
-    snake.init();
+    snake.init(); // initialize snake
     _game_cont = true;
-    snake.set_food_posX((rand()%(19) + 1) * 4);
-    snake.set_food_posY((rand()%(11) + 1) * 4 - 2);
+    snake.set_food_posX((rand()%(19) + 1) * 4); // randomize horizontal food position
+    snake.set_food_posY((rand()%(11) + 1) * 4 - 2); // randomize vertical food position
     _body_seg = 1;
     _direction = 0;
     _score = 0;
@@ -23,26 +26,26 @@
 
 void GameEngine::draw(N5110 &lcd) {    
     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // Draws screen limits
-    snake.draw_head(lcd);
-    snake.draw_food(lcd);
+    snake.draw_head(lcd); // call head drawing
+    snake.draw_food(lcd); // call food drawing
 }
 
 void GameEngine::get_dir(Gamepad &gamepad) {
-    if ( gamepad.check_event(Gamepad::Y_PRESSED) && _direction != 1) {
+    if ( gamepad.check_event(Gamepad::Y_PRESSED) && _direction != 1) { // if Y button pressed go up unless snake is going down
         _direction = 0;
     }
-    else if ( gamepad.check_event(Gamepad::A_PRESSED) && _direction != 0) {
+    else if ( gamepad.check_event(Gamepad::A_PRESSED) && _direction != 0) { // if A button pressed go down unless snake is going up
         _direction = 1;
     }
-    else if ( gamepad.check_event(Gamepad::X_PRESSED) && _direction != 3) {
+    else if ( gamepad.check_event(Gamepad::X_PRESSED) && _direction != 3) { // if X button pressed go left unless snake is going right
         _direction = 2;
     }
-    else if ( gamepad.check_event(Gamepad::B_PRESSED) && _direction != 2) {
+    else if ( gamepad.check_event(Gamepad::B_PRESSED) && _direction != 2) { // if B button pressed go right unless snake is going left
         _direction = 3;
     }
 }
 
-void GameEngine::snake_move() {
+void GameEngine::snake_move() { // checks direction of snake and increments snake in direction of travel
     if (_direction == 0) {
         snake.set_snake_posY (snake.get_snake_posY()- 4);    
     } else if (_direction == 1) {
@@ -57,55 +60,61 @@
 void GameEngine::food_move() {
     srand(time(NULL));
     _food_reset = true;
-    if(snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY()) {
+    if(snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY()) { // if snake head and food position in same place then reset food
         snake.set_food_posX((rand()%(19) + 1) * 4);
         snake.set_food_posY((rand()%(11) + 1) * 4 - 2);
-        while(_food_reset == true) {
+        while(_food_reset == true) { // loop until food reset flag is not triggered
             _food_reset = false;
             for (int k = _body_seg; k > 0; k--) {
                 if((_body_posX[k] == snake.get_food_posX() && _body_posY[k] == snake.get_food_posY()) || (snake.get_snake_posX() == snake.get_food_posX() && snake.get_snake_posY() == snake.get_food_posY())) {
+                        // if food position is the same as any of the snake body segments then reset food
                     snake.set_food_posX((rand()%(19) + 1) * 4);
                     snake.set_food_posY((rand()%(11) + 1) * 4 - 2); 
                     _food_reset = true;  
                 }
             } 
         }
-        _body_seg = _body_seg + 1;
-        _score = _score + 1;
+        _body_seg = _body_seg + 1; // increment number of body segments
+        _score = _score + 1; // increment score
     }    
 }
+
 void GameEngine::snake_body(N5110 &lcd) {
     _body_posX[0] = snake.get_snake_posX();
     _body_posY[0] = snake.get_snake_posY();
     for (int j = _body_seg; j > 0; j--) {
-        lcd.drawRect(_body_posX[j],_body_posY[j],4,4,FILL_BLACK); 
+        lcd.drawRect(_body_posX[j],_body_posY[j],4,4,FILL_BLACK); // draw each body part
         _body_posX[j] = _body_posX[j-1];
         _body_posY[j] = _body_posY[j-1];   
     }
 }
 
 void GameEngine::check_wall_collision() {
-    if (snake.get_snake_posX() == 0 || snake.get_snake_posX() > 80 || snake.get_snake_posY() < 0 || snake.get_snake_posY() > 44) {
+    if (snake.get_snake_posX() < 4 || snake.get_snake_posX() > 80 || snake.get_snake_posY() < 0 || snake.get_snake_posY() > 44) { // check if snake head has hit any of the walls
         _game_cont = false;
     }    
 }
 
 void GameEngine::check_snake_collision() {
     for (int i = _body_seg; i > 1; i--) {
-        if (_body_posX[i] == snake.get_snake_posX() && _body_posY[i] == snake.get_snake_posY()) {
-            _game_cont = false;
+        if (_body_posX[i] == snake.get_snake_posX() && _body_posY[i] == snake.get_snake_posY()) { // if snake head matches any part of snake body collision has occurred
+            _game_cont = false; 
         }   
     }
-   
 }
 
 // accessors
+// this method gets the boolean value for checking whether the game should be running
 bool GameEngine::get_game_cont() {
     return _game_cont;    
 }
+
+// this method gets the direction set by the gamepad
 int GameEngine::get_direction() {
     return _direction;    
 }
+
+// this method gets the score
 int GameEngine::get_score() {
     return _score;    
 }
diff -r a7ea33e6bd82 -r aedca0082855 Game_engine/Game_engine.h
--- a/Game_engine/Game_engine.h	Thu May 09 11:36:21 2019 +0000
+++ b/Game_engine/Game_engine.h	Thu May 09 13:52:41 2019 +0000
@@ -9,23 +9,57 @@
 #include "Snake.h"
 
 
-
+/** GameEngine Class
+ * @brief The game engine for snake game
+ * @author Daniel Crockford 201039580
+ * @date 09 May 2019
+ */
 class GameEngine {
 
 public:
+    /** Constructor */
     GameEngine();
+    
+    /** DeConstructor */
     ~GameEngine();
+    
+    /** resets direction */
     void direction_reset();
+    
+    /** initialises engine */
     void init();
+    
+    /** draws all objects
+    * @param N5110 class for controlling lcd screen */
     void draw(N5110 &lcd);
+    
+    /** get directions from gamepad
+    * param Gamepad class for accessing gamepad controls */
     void get_dir(Gamepad &gamepad);
+    
+    /** moves snake */
     void snake_move();
+    
+    /** moves food */
     void food_move();
+    
+    /** draws snake body
+    * @param N5110 class for controlling lcd screen*/
     void snake_body(N5110 &lcd);
+    
+    /** checks for collisions with wall and creates flag */
     void check_wall_collision();
+    
+    /** checks for collision with snake body and creates flag */
     void check_snake_collision();
+    
+    /** get game continue flag */ 
     bool get_game_cont();
+    
+    /** get direction value */
     int get_direction();
+    
+    /** get score */
     int get_score();
     
 private:
diff -r a7ea33e6bd82 -r aedca0082855 Snake/Snake.cpp
--- a/Snake/Snake.cpp	Thu May 09 11:36:21 2019 +0000
+++ b/Snake/Snake.cpp	Thu May 09 13:52:41 2019 +0000
@@ -1,5 +1,6 @@
 #include "Snake.h"
 
+// constructor and destructor empty
 Snake::Snake() { 
 }
  
@@ -15,28 +16,39 @@
     lcd.drawRect(_snake_posX,_snake_posY,4,4,FILL_BLACK); // Draws snake head
 }
 
-void Snake::set_snake_posX(int snake_posX) {_snake_posX = snake_posX;} // mutators allow changing snake position
+// this method allows changing the horizontal snake position
+void Snake::set_snake_posX(int snake_posX) {_snake_posX = snake_posX;}
+
+// this method allows changing thevertical snake position 
 void Snake::set_snake_posY(int snake_posY) {_snake_posY = snake_posY;}
 
-// accessors
+// this method gets the horizontal position of snake
 int Snake::get_snake_posX() {
     return _snake_posX;    
 }
+
+// this method gets the vertical position of snake
 int Snake::get_snake_posY() {
     return _snake_posY;    
 }
 
-void Snake::set_food_posX(int food_posX) {_food_posX = food_posX;} // mutators allow changing snake position
+// this method allows changing the horizontal position of food
+void Snake::set_food_posX(int food_posX) {_food_posX = food_posX;}
+
+// this method allows changing the vertical position of food
 void Snake::set_food_posY(int food_posY) {_food_posY = food_posY;}
 
 // accessors
+// this method gets the food horizontal position
 int Snake::get_food_posX() {
     return _food_posX;    
 }
+
+// this method gets the food vertical position
 int Snake::get_food_posY() {
     return _food_posY;    
 }
 
 void Snake::draw_food(N5110 &lcd) { 
-    lcd.drawRect(_food_posX,_food_posY,4,4,FILL_BLACK);
+    lcd.drawRect(_food_posX,_food_posY,4,4,FILL_BLACK); // draw food
 }
\ No newline at end of file
diff -r a7ea33e6bd82 -r aedca0082855 Snake/Snake.h
--- a/Snake/Snake.h	Thu May 09 11:36:21 2019 +0000
+++ b/Snake/Snake.h	Thu May 09 13:52:41 2019 +0000
@@ -5,25 +5,57 @@
 #include "N5110.h"
 #include "Gamepad.h"
 
+/** Snake Class
+ * @brief The class for drawing snake and determining snake position for snake game
+ * @author Daniel Crockford 201039580
+ * @date 09 May 2019
+ */
 class Snake {
     
 public:
-    // constructor
+    /** Constructor */
     Snake();
     
-    // destructor
+    /** Deconstructor */
     ~Snake();
 
+    /** initialise snake */
     void init();
+    
+    /** draw the snake head
+    * @param N5110 class for controlling the lcd screen */
     void draw_head(N5110 &lcd);
-    void set_snake_posX(int snake_posX); // mutators
+    
+    /** allows snake horizontal position to be set from outside
+    * @param value between 0 and 80 */
+    void set_snake_posX(int snake_posX);
+    
+    /** allows snake vertical position to be set from outside
+    * @param value between 0 and 44 */
     void set_snake_posY(int snake_posY);
-    int get_snake_posX(); //accessors
+    
+    /** get snake horizontal position */
+    int get_snake_posX();
+    
+    /** get snake vertical position*/
     int get_snake_posY();
+    
+    /** draw food
+    * @param N5110 class for controlling the lcd screen*/ 
     void draw_food(N5110 &lcd);
-    void set_food_posX(int food_posX); // mutators
+    
+    /** allows food horizontal position to be set from outside
+    * @param value between 4 and 80 */
+    void set_food_posX(int food_posX);
+    
+    /** allows food vertical position to be set from outside
+    * @param value between 4 and 40 */
     void set_food_posY(int food_posY);
-    int get_food_posX(); //accessors
+    
+    /** get snake food horizontal position */
+    int get_food_posX();
+    
+    /** get food vertical position */ 
     int get_food_posY();
 
 private:
diff -r a7ea33e6bd82 -r aedca0082855 main.cpp
--- a/main.cpp	Thu May 09 11:36:21 2019 +0000
+++ b/main.cpp	Thu May 09 13:52:41 2019 +0000
@@ -73,15 +73,13 @@
 }
 
 // Protoype menu
-void Menu() {
-    //engine.get_dir(gamepad);    
+void Menu() {  
     game_speed = 0;
     lcd.clear();
     lcd.printString("Select speed",0,0);
     lcd.printString("Slowest: X",0,2);
     lcd.printString("Fastest: B",0,3);
     lcd.refresh(); 
-    
     while(game_speed == 0) {
         engine.get_dir(gamepad);
         if (engine.get_direction() == 2) {
@@ -90,7 +88,6 @@
             game_speed = 2;    
         }
     }
-
     if (game_speed == 1) {
         speed = 0.5;    
     } else if (game_speed == 2) {