Harry Rance 200925395 Embedded Systems Project

Dependencies:   mbed

Committer:
harryrance
Date:
Sun Apr 30 15:30:30 2017 +0000
Revision:
4:107bdbbf78bf
Parent:
3:43970d8d642e
Child:
6:dca8b5e2ebe5
Full game engine and menu system complete. Need to go through and improve commenting;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harryrance 3:43970d8d642e 1 #include "Menu.h"
harryrance 3:43970d8d642e 2
harryrance 3:43970d8d642e 3 Menu::Menu()
harryrance 3:43970d8d642e 4 {
harryrance 3:43970d8d642e 5
harryrance 3:43970d8d642e 6 }
harryrance 3:43970d8d642e 7
harryrance 3:43970d8d642e 8 Menu::~Menu()
harryrance 3:43970d8d642e 9 {
harryrance 3:43970d8d642e 10
harryrance 3:43970d8d642e 11 }
harryrance 3:43970d8d642e 12
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 3:43970d8d642e 18 _is_game_active = 0;
harryrance 3:43970d8d642e 19 _page_index = 0;
harryrance 3:43970d8d642e 20 }
harryrance 3:43970d8d642e 21
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 3:43970d8d642e 28
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 3:43970d8d642e 38
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 3:43970d8d642e 43
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 3:43970d8d642e 52
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 3:43970d8d642e 57
harryrance 3:43970d8d642e 58 void Menu::move_selection_square_x(Direction d)
harryrance 3:43970d8d642e 59 {
harryrance 3:43970d8d642e 60 if (d == CENTRE) {
harryrance 3:43970d8d642e 61 _square_active = 1;
harryrance 3:43970d8d642e 62 }
harryrance 3:43970d8d642e 63 if (_page_index == 1){
harryrance 3:43970d8d642e 64 if (d == E && _square_active){
harryrance 3:43970d8d642e 65 _square_pos_x += 22;
harryrance 3:43970d8d642e 66 _square_pos_y = 41;
harryrance 3:43970d8d642e 67 _square_active = 0;
harryrance 3:43970d8d642e 68 } else if (d == W && _square_active){
harryrance 3:43970d8d642e 69 _square_pos_x -= 22;
harryrance 3:43970d8d642e 70 _square_pos_y = 41;
harryrance 3:43970d8d642e 71 _square_active = 0;
harryrance 3:43970d8d642e 72 }
harryrance 3:43970d8d642e 73 if(_square_pos_x < 7){
harryrance 3:43970d8d642e 74 _square_pos_x = 73;
harryrance 3:43970d8d642e 75 }
harryrance 3:43970d8d642e 76 if(_square_pos_x > 73){
harryrance 3:43970d8d642e 77 _square_pos_x = 7;
harryrance 3:43970d8d642e 78 }
harryrance 3:43970d8d642e 79 }
harryrance 3:43970d8d642e 80 }
harryrance 3:43970d8d642e 81
harryrance 3:43970d8d642e 82 void Menu::move_selection_square_y(Direction d)
harryrance 3:43970d8d642e 83 {
harryrance 3:43970d8d642e 84 if (d == CENTRE) {
harryrance 3:43970d8d642e 85 _square_active = 1;
harryrance 3:43970d8d642e 86 }
harryrance 4:107bdbbf78bf 87 if (_page_index == 0 || _page_index == 2){
harryrance 3:43970d8d642e 88 _square_pos_x = 70;
harryrance 3:43970d8d642e 89 if (d == S && _square_active){
harryrance 3:43970d8d642e 90 _square_pos_y += 8;
harryrance 3:43970d8d642e 91 _square_pos_x = 70;
harryrance 3:43970d8d642e 92 _square_active = 0;
harryrance 3:43970d8d642e 93 } else if (d == N && _square_active){
harryrance 3:43970d8d642e 94 _square_pos_y -= 8;
harryrance 3:43970d8d642e 95 _square_pos_x = 70;
harryrance 3:43970d8d642e 96 _square_active = 0;
harryrance 3:43970d8d642e 97 }
harryrance 3:43970d8d642e 98 if(_square_pos_y < 25){
harryrance 3:43970d8d642e 99 _square_pos_y = 41;
harryrance 3:43970d8d642e 100 }
harryrance 3:43970d8d642e 101 if(_square_pos_y > 41){
harryrance 3:43970d8d642e 102 _square_pos_y = 25;
harryrance 3:43970d8d642e 103 }
harryrance 3:43970d8d642e 104 }
harryrance 3:43970d8d642e 105 }
harryrance 3:43970d8d642e 106
harryrance 4:107bdbbf78bf 107 void Menu::exit_game(N5110 &lcd, Gamepad &pad)
harryrance 4:107bdbbf78bf 108 {
harryrance 4:107bdbbf78bf 109 if (_square_pos_y == 41 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0){
harryrance 4:107bdbbf78bf 110 lcd.turnOff();
harryrance 4:107bdbbf78bf 111 lcd.setBrightness(0.0);
harryrance 4:107bdbbf78bf 112 }
harryrance 4:107bdbbf78bf 113 }
harryrance 4:107bdbbf78bf 114
harryrance 3:43970d8d642e 115 void Menu::page_selection(Gamepad &pad)
harryrance 3:43970d8d642e 116 {
harryrance 3:43970d8d642e 117 if (_square_pos_y == 33 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0){
harryrance 3:43970d8d642e 118 _page_index = 1;
harryrance 3:43970d8d642e 119 _square_pos_x = 7;
harryrance 3:43970d8d642e 120 _square_pos_y = 41;
harryrance 3:43970d8d642e 121 }
harryrance 3:43970d8d642e 122 }
harryrance 3:43970d8d642e 123
harryrance 3:43970d8d642e 124 void Menu::step_back(Gamepad &pad)
harryrance 3:43970d8d642e 125 {
harryrance 3:43970d8d642e 126 if (_page_index == 1 && (pad.check_event(Gamepad::BACK_PRESSED))){
harryrance 3:43970d8d642e 127 _page_index = 0;
harryrance 3:43970d8d642e 128 _square_pos_x = 70;
harryrance 3:43970d8d642e 129 _square_pos_y = 33;
harryrance 3:43970d8d642e 130 }
harryrance 3:43970d8d642e 131 if (_page_index == 2 && (pad.check_event(Gamepad::BACK_PRESSED))){
harryrance 3:43970d8d642e 132 _page_index = 0;
harryrance 3:43970d8d642e 133 _square_pos_x = 70;
harryrance 3:43970d8d642e 134 _square_pos_y = 41;
harryrance 3:43970d8d642e 135 }
harryrance 3:43970d8d642e 136 }
harryrance 3:43970d8d642e 137
harryrance 3:43970d8d642e 138 void Menu::draw_shop(N5110 &lcd)
harryrance 3:43970d8d642e 139 {
harryrance 3:43970d8d642e 140 if(_page_index == 1){
harryrance 3:43970d8d642e 141 lcd.clear();
harryrance 3:43970d8d642e 142 lcd.printString("SHOP",35,0);
harryrance 3:43970d8d642e 143 draw_shop_ship_1(lcd);
harryrance 3:43970d8d642e 144 draw_shop_ship_2(lcd);
harryrance 3:43970d8d642e 145 draw_shop_ship_3(lcd);
harryrance 3:43970d8d642e 146 draw_shop_ship_4(lcd);
harryrance 3:43970d8d642e 147 draw_heart(lcd);
harryrance 3:43970d8d642e 148 lcd.printString("Life: 100 C",5,3);
harryrance 3:43970d8d642e 149
harryrance 3:43970d8d642e 150 if (_square_pos_x == 7){
harryrance 3:43970d8d642e 151 lcd.printString("Cost: 50 C",5,2);
harryrance 3:43970d8d642e 152 } else if (_square_pos_x == 29) {
harryrance 3:43970d8d642e 153 lcd.printString("Cost: 100 C",5,2);
harryrance 3:43970d8d642e 154 } else if (_square_pos_x == 51) {
harryrance 3:43970d8d642e 155 lcd.printString("Cost: 150 C",5,2);
harryrance 3:43970d8d642e 156 } else if (_square_pos_x == 73) {
harryrance 3:43970d8d642e 157 lcd.printString("Cost: 200 C",5,2);
harryrance 3:43970d8d642e 158 }
harryrance 3:43970d8d642e 159 }
harryrance 3:43970d8d642e 160 }
harryrance 3:43970d8d642e 161
harryrance 3:43970d8d642e 162 void Menu::draw_shop_ship_1(N5110 &lcd)
harryrance 3:43970d8d642e 163 {
harryrance 3:43970d8d642e 164 lcd.drawRect(5, 38, 2, 2, 1);
harryrance 3:43970d8d642e 165 lcd.drawRect(9, 38, 2, 2, 1);
harryrance 3:43970d8d642e 166 lcd.drawRect(13, 38, 2, 2, 1);
harryrance 3:43970d8d642e 167 lcd.drawRect(7, 36, 2, 2, 1);
harryrance 3:43970d8d642e 168 lcd.drawRect(11, 36, 2, 2, 1);
harryrance 3:43970d8d642e 169 lcd.drawRect(5, 34, 10, 2, 1);
harryrance 3:43970d8d642e 170 lcd.drawRect(7, 32, 6, 4, 1);
harryrance 3:43970d8d642e 171 lcd.drawRect(9, 30, 2, 2, 1);
harryrance 3:43970d8d642e 172 }
harryrance 3:43970d8d642e 173
harryrance 3:43970d8d642e 174 void Menu::draw_shop_ship_2(N5110 &lcd)
harryrance 3:43970d8d642e 175 {
harryrance 3:43970d8d642e 176 lcd.drawRect(29, 38, 6, 2, 1);
harryrance 3:43970d8d642e 177 lcd.drawRect(27, 36, 4, 2, 1);
harryrance 3:43970d8d642e 178 lcd.drawRect(33, 36, 4, 2, 1);
harryrance 3:43970d8d642e 179 lcd.drawRect(27, 34, 10, 2, 1);
harryrance 3:43970d8d642e 180 lcd.drawRect(27, 32, 2, 4, 1);
harryrance 3:43970d8d642e 181 lcd.drawRect(35, 32, 2, 4, 1);
harryrance 3:43970d8d642e 182 lcd.drawRect(31, 32, 2, 2, 1);
harryrance 3:43970d8d642e 183 }
harryrance 3:43970d8d642e 184
harryrance 3:43970d8d642e 185 void Menu::draw_shop_ship_3(N5110 &lcd)
harryrance 3:43970d8d642e 186 {
harryrance 3:43970d8d642e 187 lcd.drawRect(49, 38, 2, 2, 1);
harryrance 3:43970d8d642e 188 lcd.drawRect(57, 38, 2, 2, 1);
harryrance 3:43970d8d642e 189 lcd.drawRect(51, 36, 2, 2, 1);
harryrance 3:43970d8d642e 190 lcd.drawRect(55, 36, 2, 2, 1);
harryrance 3:43970d8d642e 191 lcd.drawRect(53, 34, 2, 2, 1);
harryrance 3:43970d8d642e 192 }
harryrance 3:43970d8d642e 193
harryrance 3:43970d8d642e 194 void Menu::draw_shop_ship_4(N5110 &lcd)
harryrance 3:43970d8d642e 195 {
harryrance 3:43970d8d642e 196 lcd.drawRect(71, 38, 10, 2, 1);
harryrance 3:43970d8d642e 197 lcd.drawRect(73, 36, 2, 2, 1);
harryrance 3:43970d8d642e 198 lcd.drawRect(77, 36, 2, 2, 1);
harryrance 3:43970d8d642e 199 lcd.drawRect(73, 34, 6, 2, 1);
harryrance 3:43970d8d642e 200 lcd.drawRect(71, 32, 2, 4, 1);
harryrance 3:43970d8d642e 201 lcd.drawRect(75, 30, 2, 4, 1);
harryrance 3:43970d8d642e 202 lcd.drawRect(79, 32, 2, 4, 1);
harryrance 3:43970d8d642e 203 }
harryrance 3:43970d8d642e 204
harryrance 3:43970d8d642e 205 void Menu::draw_heart(N5110 &lcd)
harryrance 3:43970d8d642e 206 {
harryrance 3:43970d8d642e 207 lcd.drawRect(41, 32,2,1,0);
harryrance 3:43970d8d642e 208 lcd.drawRect(44, 32,2,1,0);
harryrance 3:43970d8d642e 209 lcd.drawRect(40, 33,7,1,0);
harryrance 3:43970d8d642e 210 lcd.drawRect(39, 34,9,1,0);
harryrance 3:43970d8d642e 211 lcd.drawRect(40, 35,7,1,0);
harryrance 3:43970d8d642e 212 lcd.drawRect(41, 36,5,1,0);
harryrance 3:43970d8d642e 213 lcd.drawRect(42, 37,3,1,0);
harryrance 3:43970d8d642e 214 lcd.setPixel(43, 38);
harryrance 3:43970d8d642e 215 }
harryrance 3:43970d8d642e 216
harryrance 3:43970d8d642e 217 void Menu::initial_screen_selection(Gamepad &pad)
harryrance 3:43970d8d642e 218 {
harryrance 3:43970d8d642e 219 if (_square_pos_y == 25 && (pad.check_event(Gamepad::A_PRESSED)) && _page_index == 0) {
harryrance 3:43970d8d642e 220 run_game();
harryrance 3:43970d8d642e 221 }
harryrance 3:43970d8d642e 222 }
harryrance 3:43970d8d642e 223
harryrance 3:43970d8d642e 224 void Menu::run_game()
harryrance 3:43970d8d642e 225 {
harryrance 3:43970d8d642e 226 _is_game_active = 1;
harryrance 3:43970d8d642e 227 }
harryrance 3:43970d8d642e 228
harryrance 3:43970d8d642e 229 void Menu::stop_game()
harryrance 3:43970d8d642e 230 {
harryrance 3:43970d8d642e 231 _is_game_active = 0;
harryrance 3:43970d8d642e 232 }
harryrance 3:43970d8d642e 233
harryrance 3:43970d8d642e 234 int Menu::is_game_active()
harryrance 3:43970d8d642e 235 {
harryrance 3:43970d8d642e 236 return _is_game_active;
harryrance 3:43970d8d642e 237 }
harryrance 3:43970d8d642e 238
harryrance 3:43970d8d642e 239 int Menu::total_coins()
harryrance 3:43970d8d642e 240 {
harryrance 3:43970d8d642e 241 _all_coins = _boss.get_boss_coins() + _bullet.get_coins();
harryrance 3:43970d8d642e 242 return _all_coins;
harryrance 3:43970d8d642e 243 }
harryrance 3:43970d8d642e 244