Harry Rance 200925395 Embedded Systems Project

Dependencies:   mbed

Committer:
harryrance
Date:
Wed May 03 16:24:01 2017 +0000
Revision:
6:dca8b5e2ebe5
Parent:
4:107bdbbf78bf
Full project complete with Doxygen commenting throughout; ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harryrance 3:43970d8d642e 1 #include "Menu.h"
harryrance 6:dca8b5e2ebe5 2 //Constructor
harryrance 3:43970d8d642e 3 Menu::Menu()
harryrance 3:43970d8d642e 4 {
harryrance 3:43970d8d642e 5
harryrance 3:43970d8d642e 6 }
harryrance 6:dca8b5e2ebe5 7 //Destructor
harryrance 3:43970d8d642e 8 Menu::~Menu()
harryrance 3:43970d8d642e 9 {
harryrance 3:43970d8d642e 10
harryrance 3:43970d8d642e 11 }
harryrance 6:dca8b5e2ebe5 12 //Initialise function - sets the initial square position and coins number.
harryrance 3:43970d8d642e 13 void Menu::initialise(int square_pos, int all_coins)
harryrance 3:43970d8d642e 14 {
harryrance 3:43970d8d642e 15 _square_pos_y = square_pos;
harryrance 3:43970d8d642e 16 _square_pos_x = 70;
harryrance 3:43970d8d642e 17 _all_coins = all_coins;
harryrance 6:dca8b5e2ebe5 18 _is_game_active = 0; //tells the game to not start running initially.
harryrance 6:dca8b5e2ebe5 19 _page_index = 0; //starts on the first menu page.
harryrance 3:43970d8d642e 20 }
harryrance 6:dca8b5e2ebe5 21 //Draw function - draws all of the elements of the menu system.
harryrance 3:43970d8d642e 22 void Menu::draw(N5110 &lcd)
harryrance 3:43970d8d642e 23 {
harryrance 3:43970d8d642e 24 draw_initial_screen(lcd);
harryrance 3:43970d8d642e 25 draw_shop(lcd);
harryrance 3:43970d8d642e 26 draw_selection_square(lcd);
harryrance 3:43970d8d642e 27 }
harryrance 6:dca8b5e2ebe5 28 //Update function - used to update the position of the selection square and the screen state depending on user inputs.
harryrance 4:107bdbbf78bf 29 void Menu::update(Gamepad &pad, N5110 &lcd)
harryrance 3:43970d8d642e 30 {
harryrance 3:43970d8d642e 31 move_selection_square_y(_d);
harryrance 3:43970d8d642e 32 move_selection_square_x(_d);
harryrance 3:43970d8d642e 33 initial_screen_selection(pad);
harryrance 3:43970d8d642e 34 page_selection(pad);
harryrance 3:43970d8d642e 35 step_back(pad);
harryrance 4:107bdbbf78bf 36 exit_game(lcd, pad);
harryrance 3:43970d8d642e 37 }
harryrance 6:dca8b5e2ebe5 38 //Read Input function - reads the input on the joystick to move selection square.
harryrance 3:43970d8d642e 39 void Menu::read_input(Gamepad &pad)
harryrance 3:43970d8d642e 40 {
harryrance 3:43970d8d642e 41 _d = pad.get_direction();
harryrance 3:43970d8d642e 42 }
harryrance 6:dca8b5e2ebe5 43 //Draws the initial start-up menu screen.
harryrance 3:43970d8d642e 44 void Menu::draw_initial_screen(N5110 &lcd)
harryrance 3:43970d8d642e 45 {
harryrance 3:43970d8d642e 46 lcd.printString("SPACE INVADERS",0,0);
harryrance 3:43970d8d642e 47 lcd.printString("BOSS FIGHT",13,1);
harryrance 3:43970d8d642e 48 lcd.printString("Start Game", 5, 3);
harryrance 3:43970d8d642e 49 lcd.printString("Shop", 5, 4);
harryrance 4:107bdbbf78bf 50 lcd.printString("Exit", 5, 5);
harryrance 3:43970d8d642e 51 }
harryrance 6:dca8b5e2ebe5 52 //Draws the selection square with a movable x and y origin.
harryrance 3:43970d8d642e 53 void Menu::draw_selection_square(N5110 &lcd)
harryrance 3:43970d8d642e 54 {
harryrance 3:43970d8d642e 55 lcd.drawRect(_square_pos_x, _square_pos_y, 5, 5, 1);
harryrance 3:43970d8d642e 56 }
harryrance 6:dca8b5e2ebe5 57 //Used to move selection square in the x position.
harryrance 3:43970d8d642e 58 void Menu::move_selection_square_x(Direction d)
harryrance 3:43970d8d642e 59 {
harryrance 6:dca8b5e2ebe5 60 if (d == CENTRE) { //calling this using the _square_active variable ensures that the selection square
harryrance 6:dca8b5e2ebe5 61 //only moves up/down/left/right once per movement of the joystick.
harryrance 3:43970d8d642e 62 _square_active = 1;
harryrance 3:43970d8d642e 63 }
harryrance 3:43970d8d642e 64 if (_page_index == 1){
harryrance 3:43970d8d642e 65 if (d == E && _square_active){
harryrance 3:43970d8d642e 66 _square_pos_x += 22;
harryrance 3:43970d8d642e 67 _square_pos_y = 41;
harryrance 3:43970d8d642e 68 _square_active = 0;
harryrance 3:43970d8d642e 69 } else if (d == W && _square_active){
harryrance 3:43970d8d642e 70 _square_pos_x -= 22;
harryrance 3:43970d8d642e 71 _square_pos_y = 41;
harryrance 3:43970d8d642e 72 _square_active = 0;
harryrance 3:43970d8d642e 73 }
harryrance 6:dca8b5e2ebe5 74 //resets square position if it moves further than the amount of options.
harryrance 3:43970d8d642e 75 if(_square_pos_x < 7){
harryrance 3:43970d8d642e 76 _square_pos_x = 73;
harryrance 3:43970d8d642e 77 }
harryrance 3:43970d8d642e 78 if(_square_pos_x > 73){
harryrance 3:43970d8d642e 79 _square_pos_x = 7;
harryrance 3:43970d8d642e 80 }
harryrance 3:43970d8d642e 81 }
harryrance 3:43970d8d642e 82 }
harryrance 6:dca8b5e2ebe5 83 //Moves selection square in the y direction.
harryrance 3:43970d8d642e 84 void Menu::move_selection_square_y(Direction d)
harryrance 3:43970d8d642e 85 {
harryrance 3:43970d8d642e 86 if (d == CENTRE) {
harryrance 3:43970d8d642e 87 _square_active = 1;
harryrance 3:43970d8d642e 88 }
harryrance 4:107bdbbf78bf 89 if (_page_index == 0 || _page_index == 2){
harryrance 3:43970d8d642e 90 _square_pos_x = 70;
harryrance 3:43970d8d642e 91 if (d == S && _square_active){
harryrance 3:43970d8d642e 92 _square_pos_y += 8;
harryrance 3:43970d8d642e 93 _square_pos_x = 70;
harryrance 3:43970d8d642e 94 _square_active = 0;
harryrance 3:43970d8d642e 95 } else if (d == N && _square_active){
harryrance 3:43970d8d642e 96 _square_pos_y -= 8;
harryrance 3:43970d8d642e 97 _square_pos_x = 70;
harryrance 3:43970d8d642e 98 _square_active = 0;
harryrance 3:43970d8d642e 99 }
harryrance 3:43970d8d642e 100 if(_square_pos_y < 25){
harryrance 3:43970d8d642e 101 _square_pos_y = 41;
harryrance 3:43970d8d642e 102 }
harryrance 3:43970d8d642e 103 if(_square_pos_y > 41){
harryrance 3:43970d8d642e 104 _square_pos_y = 25;
harryrance 3:43970d8d642e 105 }
harryrance 3:43970d8d642e 106 }
harryrance 3:43970d8d642e 107 }
harryrance 6:dca8b5e2ebe5 108 //turns off LCD screen and sets brightness to 0 if this option is selected.
harryrance 4:107bdbbf78bf 109 void Menu::exit_game(N5110 &lcd, Gamepad &pad)
harryrance 4:107bdbbf78bf 110 {
harryrance 4:107bdbbf78bf 111 if (_square_pos_y == 41 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0){
harryrance 4:107bdbbf78bf 112 lcd.turnOff();
harryrance 4:107bdbbf78bf 113 lcd.setBrightness(0.0);
harryrance 4:107bdbbf78bf 114 }
harryrance 4:107bdbbf78bf 115 }
harryrance 6:dca8b5e2ebe5 116 //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.
harryrance 3:43970d8d642e 117 void Menu::page_selection(Gamepad &pad)
harryrance 3:43970d8d642e 118 {
harryrance 3:43970d8d642e 119 if (_square_pos_y == 33 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0){
harryrance 3:43970d8d642e 120 _page_index = 1;
harryrance 3:43970d8d642e 121 _square_pos_x = 7;
harryrance 3:43970d8d642e 122 _square_pos_y = 41;
harryrance 3:43970d8d642e 123 }
harryrance 3:43970d8d642e 124 }
harryrance 6:dca8b5e2ebe5 125 //allows the user to use the BACK button to step back to the main screen.
harryrance 3:43970d8d642e 126 void Menu::step_back(Gamepad &pad)
harryrance 3:43970d8d642e 127 {
harryrance 3:43970d8d642e 128 if (_page_index == 1 && (pad.check_event(Gamepad::BACK_PRESSED))){
harryrance 3:43970d8d642e 129 _page_index = 0;
harryrance 3:43970d8d642e 130 _square_pos_x = 70;
harryrance 3:43970d8d642e 131 _square_pos_y = 33;
harryrance 3:43970d8d642e 132 }
harryrance 3:43970d8d642e 133 if (_page_index == 2 && (pad.check_event(Gamepad::BACK_PRESSED))){
harryrance 3:43970d8d642e 134 _page_index = 0;
harryrance 3:43970d8d642e 135 _square_pos_x = 70;
harryrance 3:43970d8d642e 136 _square_pos_y = 41;
harryrance 3:43970d8d642e 137 }
harryrance 3:43970d8d642e 138 }
harryrance 6:dca8b5e2ebe5 139 //Draws all of the shop elements.
harryrance 3:43970d8d642e 140 void Menu::draw_shop(N5110 &lcd)
harryrance 3:43970d8d642e 141 {
harryrance 3:43970d8d642e 142 if(_page_index == 1){
harryrance 3:43970d8d642e 143 lcd.clear();
harryrance 3:43970d8d642e 144 lcd.printString("SHOP",35,0);
harryrance 3:43970d8d642e 145 draw_shop_ship_1(lcd);
harryrance 3:43970d8d642e 146 draw_shop_ship_2(lcd);
harryrance 3:43970d8d642e 147 draw_shop_ship_3(lcd);
harryrance 3:43970d8d642e 148 draw_shop_ship_4(lcd);
harryrance 3:43970d8d642e 149 draw_heart(lcd);
harryrance 3:43970d8d642e 150 lcd.printString("Life: 100 C",5,3);
harryrance 6:dca8b5e2ebe5 151 //changes displayed ship price depending on square position.
harryrance 3:43970d8d642e 152 if (_square_pos_x == 7){
harryrance 3:43970d8d642e 153 lcd.printString("Cost: 50 C",5,2);
harryrance 3:43970d8d642e 154 } else if (_square_pos_x == 29) {
harryrance 3:43970d8d642e 155 lcd.printString("Cost: 100 C",5,2);
harryrance 3:43970d8d642e 156 } else if (_square_pos_x == 51) {
harryrance 3:43970d8d642e 157 lcd.printString("Cost: 150 C",5,2);
harryrance 3:43970d8d642e 158 } else if (_square_pos_x == 73) {
harryrance 3:43970d8d642e 159 lcd.printString("Cost: 200 C",5,2);
harryrance 3:43970d8d642e 160 }
harryrance 3:43970d8d642e 161 }
harryrance 3:43970d8d642e 162 }
harryrance 6:dca8b5e2ebe5 163 //sets pixels for the first shop ship.
harryrance 3:43970d8d642e 164 void Menu::draw_shop_ship_1(N5110 &lcd)
harryrance 3:43970d8d642e 165 {
harryrance 3:43970d8d642e 166 lcd.drawRect(5, 38, 2, 2, 1);
harryrance 3:43970d8d642e 167 lcd.drawRect(9, 38, 2, 2, 1);
harryrance 3:43970d8d642e 168 lcd.drawRect(13, 38, 2, 2, 1);
harryrance 3:43970d8d642e 169 lcd.drawRect(7, 36, 2, 2, 1);
harryrance 3:43970d8d642e 170 lcd.drawRect(11, 36, 2, 2, 1);
harryrance 3:43970d8d642e 171 lcd.drawRect(5, 34, 10, 2, 1);
harryrance 3:43970d8d642e 172 lcd.drawRect(7, 32, 6, 4, 1);
harryrance 3:43970d8d642e 173 lcd.drawRect(9, 30, 2, 2, 1);
harryrance 3:43970d8d642e 174 }
harryrance 6:dca8b5e2ebe5 175 //sets pixels for the second shop ship.
harryrance 3:43970d8d642e 176 void Menu::draw_shop_ship_2(N5110 &lcd)
harryrance 3:43970d8d642e 177 {
harryrance 3:43970d8d642e 178 lcd.drawRect(29, 38, 6, 2, 1);
harryrance 3:43970d8d642e 179 lcd.drawRect(27, 36, 4, 2, 1);
harryrance 3:43970d8d642e 180 lcd.drawRect(33, 36, 4, 2, 1);
harryrance 3:43970d8d642e 181 lcd.drawRect(27, 34, 10, 2, 1);
harryrance 3:43970d8d642e 182 lcd.drawRect(27, 32, 2, 4, 1);
harryrance 3:43970d8d642e 183 lcd.drawRect(35, 32, 2, 4, 1);
harryrance 3:43970d8d642e 184 lcd.drawRect(31, 32, 2, 2, 1);
harryrance 3:43970d8d642e 185 }
harryrance 6:dca8b5e2ebe5 186 //sets pixels for the third shop ship.
harryrance 3:43970d8d642e 187 void Menu::draw_shop_ship_3(N5110 &lcd)
harryrance 3:43970d8d642e 188 {
harryrance 3:43970d8d642e 189 lcd.drawRect(49, 38, 2, 2, 1);
harryrance 3:43970d8d642e 190 lcd.drawRect(57, 38, 2, 2, 1);
harryrance 3:43970d8d642e 191 lcd.drawRect(51, 36, 2, 2, 1);
harryrance 3:43970d8d642e 192 lcd.drawRect(55, 36, 2, 2, 1);
harryrance 3:43970d8d642e 193 lcd.drawRect(53, 34, 2, 2, 1);
harryrance 3:43970d8d642e 194 }
harryrance 6:dca8b5e2ebe5 195 //sets pixels for the fourth shop ship.
harryrance 3:43970d8d642e 196 void Menu::draw_shop_ship_4(N5110 &lcd)
harryrance 3:43970d8d642e 197 {
harryrance 3:43970d8d642e 198 lcd.drawRect(71, 38, 10, 2, 1);
harryrance 3:43970d8d642e 199 lcd.drawRect(73, 36, 2, 2, 1);
harryrance 3:43970d8d642e 200 lcd.drawRect(77, 36, 2, 2, 1);
harryrance 3:43970d8d642e 201 lcd.drawRect(73, 34, 6, 2, 1);
harryrance 3:43970d8d642e 202 lcd.drawRect(71, 32, 2, 4, 1);
harryrance 3:43970d8d642e 203 lcd.drawRect(75, 30, 2, 4, 1);
harryrance 3:43970d8d642e 204 lcd.drawRect(79, 32, 2, 4, 1);
harryrance 3:43970d8d642e 205 }
harryrance 6:dca8b5e2ebe5 206 //sets pixels for the shop heart.
harryrance 3:43970d8d642e 207 void Menu::draw_heart(N5110 &lcd)
harryrance 3:43970d8d642e 208 {
harryrance 3:43970d8d642e 209 lcd.drawRect(41, 32,2,1,0);
harryrance 3:43970d8d642e 210 lcd.drawRect(44, 32,2,1,0);
harryrance 3:43970d8d642e 211 lcd.drawRect(40, 33,7,1,0);
harryrance 3:43970d8d642e 212 lcd.drawRect(39, 34,9,1,0);
harryrance 3:43970d8d642e 213 lcd.drawRect(40, 35,7,1,0);
harryrance 3:43970d8d642e 214 lcd.drawRect(41, 36,5,1,0);
harryrance 3:43970d8d642e 215 lcd.drawRect(42, 37,3,1,0);
harryrance 3:43970d8d642e 216 lcd.setPixel(43, 38);
harryrance 3:43970d8d642e 217 }
harryrance 6:dca8b5e2ebe5 218 //runs the game if this option is selected.
harryrance 3:43970d8d642e 219 void Menu::initial_screen_selection(Gamepad &pad)
harryrance 3:43970d8d642e 220 {
harryrance 3:43970d8d642e 221 if (_square_pos_y == 25 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0) {
harryrance 3:43970d8d642e 222 run_game();
harryrance 3:43970d8d642e 223 }
harryrance 3:43970d8d642e 224 }
harryrance 6:dca8b5e2ebe5 225 //sets the _is_game_active variable to 1. Indicated game should start running.
harryrance 3:43970d8d642e 226 void Menu::run_game()
harryrance 3:43970d8d642e 227 {
harryrance 3:43970d8d642e 228 _is_game_active = 1;
harryrance 3:43970d8d642e 229 }
harryrance 6:dca8b5e2ebe5 230 //sets the _is_game_active variable to 0. Indicated game should stop running.
harryrance 3:43970d8d642e 231 void Menu::stop_game()
harryrance 3:43970d8d642e 232 {
harryrance 3:43970d8d642e 233 _is_game_active = 0;
harryrance 3:43970d8d642e 234 }
harryrance 6:dca8b5e2ebe5 235 //returns integer value denoting whether game should be running or not
harryrance 3:43970d8d642e 236 int Menu::is_game_active()
harryrance 3:43970d8d642e 237 {
harryrance 3:43970d8d642e 238 return _is_game_active;
harryrance 3:43970d8d642e 239 }
harryrance 6:dca8b5e2ebe5 240 //returns integer value containing the number of coins the user currently holds.
harryrance 3:43970d8d642e 241 int Menu::total_coins()
harryrance 3:43970d8d642e 242 {
harryrance 3:43970d8d642e 243 _all_coins = _boss.get_boss_coins() + _bullet.get_coins();
harryrance 3:43970d8d642e 244 return _all_coins;
harryrance 3:43970d8d642e 245 }
harryrance 3:43970d8d642e 246