Harry Rance 200925395 Embedded Systems Project

Dependencies:   mbed

Revision:
6:dca8b5e2ebe5
Parent:
4:107bdbbf78bf
--- a/Menu.cpp	Tue May 02 18:47:22 2017 +0000
+++ b/Menu.cpp	Wed May 03 16:24:01 2017 +0000
@@ -1,31 +1,31 @@
 #include "Menu.h"
-
+//Constructor
 Menu::Menu()
 {
 
 }
-
+//Destructor
 Menu::~Menu()
 {
 
 }
-
+//Initialise function - sets the initial square position and coins number.
 void Menu::initialise(int square_pos, int all_coins)
 {
   _square_pos_y = square_pos;
   _square_pos_x = 70;
   _all_coins = all_coins;
-  _is_game_active = 0;
-  _page_index = 0;
+  _is_game_active = 0; //tells the game to not start running initially.
+  _page_index = 0; //starts on the first menu page.
 }
-
+//Draw function - draws all of the elements of the menu system.
 void Menu::draw(N5110 &lcd)
 {
   draw_initial_screen(lcd);
   draw_shop(lcd);
   draw_selection_square(lcd);
 }
-
+//Update function - used to update the position of the selection square and the screen state depending on user inputs.
 void Menu::update(Gamepad &pad, N5110 &lcd)
 {
   move_selection_square_y(_d);
@@ -35,12 +35,12 @@
   step_back(pad);
   exit_game(lcd, pad);
 }
-
+//Read Input function - reads the input on the joystick to move selection square.
 void Menu::read_input(Gamepad &pad)
 {
   _d = pad.get_direction();
 }
-
+//Draws the initial start-up menu screen.
 void Menu::draw_initial_screen(N5110 &lcd)
 {
   lcd.printString("SPACE INVADERS",0,0);
@@ -49,15 +49,16 @@
   lcd.printString("Shop", 5, 4);
   lcd.printString("Exit", 5, 5);
 }
-
+//Draws the selection square with a movable x and y origin.
 void Menu::draw_selection_square(N5110 &lcd)
 {
   lcd.drawRect(_square_pos_x, _square_pos_y, 5, 5, 1);
 }
-
+//Used to move selection square in the x position.
 void Menu::move_selection_square_x(Direction d)
 {
-  if (d == CENTRE) {
+  if (d == CENTRE) { //calling this using the _square_active variable ensures that the selection square
+                     //only moves up/down/left/right once per movement of the joystick.
     _square_active = 1;
   }
   if (_page_index == 1){
@@ -70,6 +71,7 @@
       _square_pos_y = 41;
       _square_active = 0;
     }
+    //resets square position if it moves further than the amount of options.
     if(_square_pos_x < 7){
       _square_pos_x = 73;
     }
@@ -78,7 +80,7 @@
     }
   }
 }
-
+//Moves selection square in the y direction.
 void Menu::move_selection_square_y(Direction d)
 {
   if (d == CENTRE) {
@@ -103,7 +105,7 @@
     }
   }
 }
-
+//turns off LCD screen and sets brightness to 0 if this option is selected.
 void Menu::exit_game(N5110 &lcd, Gamepad &pad)
 {
   if (_square_pos_y == 41 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0){
@@ -111,7 +113,7 @@
     lcd.setBrightness(0.0);
   }
 }
-
+//moves into the shop screen if this option is selected. Also sets the x and y position of the square to the correct position for the shop.
 void Menu::page_selection(Gamepad &pad)
 {
   if (_square_pos_y == 33 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0){
@@ -120,7 +122,7 @@
     _square_pos_y = 41;
   }
 }
-
+//allows the user to use the BACK button to step back to the main screen.
 void Menu::step_back(Gamepad &pad)
 {
   if (_page_index == 1 && (pad.check_event(Gamepad::BACK_PRESSED))){
@@ -134,7 +136,7 @@
     _square_pos_y = 41;
   }
 }
-
+//Draws all of the shop elements.
 void Menu::draw_shop(N5110 &lcd)
 {
   if(_page_index == 1){
@@ -146,7 +148,7 @@
     draw_shop_ship_4(lcd);
     draw_heart(lcd);
     lcd.printString("Life: 100 C",5,3);
-
+    //changes displayed ship price depending on square position.
     if (_square_pos_x == 7){
       lcd.printString("Cost: 50 C",5,2);
     } else if (_square_pos_x == 29) {
@@ -158,7 +160,7 @@
     }
   }
 }
-
+//sets pixels for the first shop ship.
 void Menu::draw_shop_ship_1(N5110 &lcd)
 {
   lcd.drawRect(5, 38, 2, 2, 1);
@@ -170,7 +172,7 @@
   lcd.drawRect(7, 32, 6, 4, 1);
   lcd.drawRect(9, 30, 2, 2, 1);
 }
-
+//sets pixels for the second shop ship.
 void Menu::draw_shop_ship_2(N5110 &lcd)
 {
   lcd.drawRect(29, 38, 6, 2, 1);
@@ -181,7 +183,7 @@
   lcd.drawRect(35, 32, 2, 4, 1);
   lcd.drawRect(31, 32, 2, 2, 1);
 }
-
+//sets pixels for the third shop ship.
 void Menu::draw_shop_ship_3(N5110 &lcd)
 {
   lcd.drawRect(49, 38, 2, 2, 1);
@@ -190,7 +192,7 @@
   lcd.drawRect(55, 36, 2, 2, 1);
   lcd.drawRect(53, 34, 2, 2, 1);
 }
-
+//sets pixels for the fourth shop ship.
 void Menu::draw_shop_ship_4(N5110 &lcd)
 {
   lcd.drawRect(71, 38, 10, 2, 1);
@@ -201,7 +203,7 @@
   lcd.drawRect(75, 30, 2, 4, 1);
   lcd.drawRect(79, 32, 2, 4, 1);
 }
-
+//sets pixels for the shop heart.
 void Menu::draw_heart(N5110 &lcd)
 {
   lcd.drawRect(41, 32,2,1,0);
@@ -213,29 +215,29 @@
   lcd.drawRect(42, 37,3,1,0);
   lcd.setPixel(43, 38);
 }
-
+//runs the game if this option is selected.
 void Menu::initial_screen_selection(Gamepad &pad)
 {
   if (_square_pos_y == 25 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0) {
     run_game();
   }
 }
-
+//sets the _is_game_active variable to 1. Indicated game should start running.
 void Menu::run_game()
 {
   _is_game_active = 1;
 }
-
+//sets the _is_game_active variable to 0. Indicated game should stop running.
 void Menu::stop_game()
 {
   _is_game_active = 0;
 }
-
+//returns integer value denoting whether game should be running or not
 int Menu::is_game_active()
 {
   return _is_game_active;
 }
-
+//returns integer value containing the number of coins the user currently holds.
 int Menu::total_coins()
 {
   _all_coins = _boss.get_boss_coins() + _bullet.get_coins();