Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

https://os.mbed.com/media/uploads/evanso/84bc1a30759fd6a1e3f1fd1fae3e97c2.png

Hello, soldier, you have been specially selected as the defender of planet earth.

Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.

But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.

As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.

Committer:
evanso
Date:
Fri May 22 13:02:26 2020 +0000
Revision:
69:753ba27325ce
Parent:
68:bb1650c657ef
Child:
70:8c4572d17441
Finished settings class and added it to into game engine.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 8:dd1037c5435b 1 #include "GameEngine.h"
evanso 67:a2984682d641 2
evanso 47:49fa1adc10b4 3 /** Define acelleromter object */
evanso 47:49fa1adc10b4 4 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
evanso 8:dd1037c5435b 5
evanso 49:ed569eceeaa4 6 /** Define SD card object */
evanso 49:ed569eceeaa4 7 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
evanso 67:a2984682d641 8
evanso 8:dd1037c5435b 9 GameEngine::GameEngine() {
evanso 44:2d957de3bce3 10 //attach ticker to isr
evanso 42:3aed75338272 11 ticker.attach(callback(this, &GameEngine::lcd_frame_time_isr), 0.04 );
evanso 8:dd1037c5435b 12 }
evanso 8:dd1037c5435b 13
evanso 8:dd1037c5435b 14 GameEngine::~GameEngine() {
evanso 8:dd1037c5435b 15
evanso 8:dd1037c5435b 16 }
evanso 8:dd1037c5435b 17
evanso 13:12276eed13ac 18 void GameEngine::init() {
evanso 36:27aa597db3d2 19 // Initalise objects
evanso 8:dd1037c5435b 20 pad.init();
evanso 8:dd1037c5435b 21 lcd.init();
evanso 8:dd1037c5435b 22 spaceship.init();
evanso 13:12276eed13ac 23 map.init(pad);
evanso 40:71f947254fda 24 menu.init();
evanso 47:49fa1adc10b4 25 accelerometer.init();
evanso 67:a2984682d641 26 saved.init();
evanso 69:753ba27325ce 27 setting.init();
evanso 58:a9a39424df52 28
evanso 58:a9a39424df52 29 //set first screen to main menu
evanso 58:a9a39424df52 30 current_menu_part_= menu.get_current_menu_part();
evanso 36:27aa597db3d2 31
evanso 36:27aa597db3d2 32 // Set seed value
evanso 36:27aa597db3d2 33 srand(pad.read_adc()*64000);
evanso 67:a2984682d641 34
evanso 67:a2984682d641 35 //Had to inlcude save test here as getting the error sd couldnt be created
evanso 67:a2984682d641 36 #ifdef SD_TEST
evanso 67:a2984682d641 37 saved.run_save_test(sd,lcd);
evanso 67:a2984682d641 38 #endif
evanso 8:dd1037c5435b 39 }
evanso 8:dd1037c5435b 40
evanso 64:e9dfc35a1738 41 void GameEngine::lcd_frame_time_isr(){
evanso 64:e9dfc35a1738 42 // set ISR flag
evanso 64:e9dfc35a1738 43 lcd_frame_time_flag_ = 1;
evanso 64:e9dfc35a1738 44 }
evanso 64:e9dfc35a1738 45
evanso 44:2d957de3bce3 46 // Menu Control-----------------------------------------------------------------
evanso 44:2d957de3bce3 47
evanso 44:2d957de3bce3 48 void GameEngine::game_select_part(){
evanso 44:2d957de3bce3 49 switch (current_menu_part_) {
evanso 44:2d957de3bce3 50 case main_menu: run_menu(); break;
evanso 44:2d957de3bce3 51 case play: run_play(); break;
evanso 44:2d957de3bce3 52 case settings: run_settings(); break;
evanso 44:2d957de3bce3 53 case saved_games: run_saved_games(); break;
evanso 44:2d957de3bce3 54 }
evanso 44:2d957de3bce3 55 }
evanso 44:2d957de3bce3 56
evanso 44:2d957de3bce3 57 void GameEngine::run_menu(){
evanso 44:2d957de3bce3 58 lcd.setContrast(pad.read_pot1());
evanso 44:2d957de3bce3 59 lcd.clear();
evanso 64:e9dfc35a1738 60
evanso 44:2d957de3bce3 61 // Scrolls, draws and selcts menu parts
evanso 44:2d957de3bce3 62 read_joystick_direction();
evanso 65:daa792a09e1f 63 menu.menu_scroll(d_);
evanso 44:2d957de3bce3 64 menu.draw_part(lcd);
evanso 65:daa792a09e1f 65 menu.select_part(pad.A_pressed());
evanso 69:753ba27325ce 66
evanso 44:2d957de3bce3 67 current_menu_part_ = menu.get_current_menu_part();
evanso 69:753ba27325ce 68
evanso 69:753ba27325ce 69 //stops double button press
evanso 69:753ba27325ce 70 if(pad.A_pressed()){
evanso 69:753ba27325ce 71 wait(0.3);
evanso 69:753ba27325ce 72 }
evanso 59:0b2e43312d6b 73 //inititialise the game if play
evanso 59:0b2e43312d6b 74 if (current_menu_part_ == play){
evanso 59:0b2e43312d6b 75 play_init();
evanso 59:0b2e43312d6b 76 }
evanso 64:e9dfc35a1738 77
evanso 64:e9dfc35a1738 78 lcd.refresh();
evanso 44:2d957de3bce3 79 }
evanso 44:2d957de3bce3 80
evanso 64:e9dfc35a1738 81
evanso 64:e9dfc35a1738 82
evanso 64:e9dfc35a1738 83
evanso 64:e9dfc35a1738 84 // Menu Play -------------------------------------------------------------------
evanso 58:a9a39424df52 85
evanso 44:2d957de3bce3 86 void GameEngine::run_play(){
evanso 64:e9dfc35a1738 87 int paused_flag = false;
evanso 44:2d957de3bce3 88 while (1) {
evanso 64:e9dfc35a1738 89 // Timer interupt to set frame rate of game
evanso 45:fc3238cd28c6 90 if (lcd_frame_time_flag_) {
evanso 44:2d957de3bce3 91 // resets ISR flag
evanso 45:fc3238cd28c6 92 lcd_frame_time_flag_ = 0;
evanso 44:2d957de3bce3 93
evanso 64:e9dfc35a1738 94 //Pause button pressed
evanso 68:bb1650c657ef 95 if (pad.start_pressed() && paused_counter_ > 10 ){
evanso 64:e9dfc35a1738 96 paused_flag = !paused_flag;
evanso 68:bb1650c657ef 97 paused_counter_ = 0 ;
evanso 58:a9a39424df52 98 }
evanso 68:bb1650c657ef 99 paused_counter_++;
evanso 64:e9dfc35a1738 100
evanso 64:e9dfc35a1738 101 //Draws pause screen if paused button presed
evanso 64:e9dfc35a1738 102 if (paused_flag){
evanso 68:bb1650c657ef 103 run_paused_game();
evanso 68:bb1650c657ef 104
evanso 64:e9dfc35a1738 105 //Otherwise draw gameplay screen
evanso 56:663d0546c235 106 }else{
evanso 56:663d0546c235 107 gameplay_loop();
evanso 56:663d0546c235 108 }
evanso 44:2d957de3bce3 109
evanso 68:bb1650c657ef 110 // break out of run play loop when lives = 0 or exite flag set
evanso 68:bb1650c657ef 111 if(!spaceship_lives_||exit_flag_){
evanso 59:0b2e43312d6b 112 if(!spaceship_lives_){
evanso 59:0b2e43312d6b 113 draw_game_over_screen();
evanso 68:bb1650c657ef 114 }
evanso 59:0b2e43312d6b 115 reset_map();
evanso 65:daa792a09e1f 116 pad.leds_off();
evanso 59:0b2e43312d6b 117 break;
evanso 56:663d0546c235 118 }
evanso 44:2d957de3bce3 119 }
evanso 44:2d957de3bce3 120
evanso 64:e9dfc35a1738 121 // MCU put to sleep between each frame to save power
evanso 64:e9dfc35a1738 122 sleep();
evanso 57:d4ce42c24561 123 }
evanso 65:daa792a09e1f 124 //stop taking double a press
evanso 69:753ba27325ce 125 wait(0.4);
evanso 44:2d957de3bce3 126 }
evanso 44:2d957de3bce3 127
evanso 64:e9dfc35a1738 128 void GameEngine::play_init(){
evanso 64:e9dfc35a1738 129 // Define variables
evanso 64:e9dfc35a1738 130 spawn_time_multipler_ = 1;
evanso 64:e9dfc35a1738 131 alien_number_ = 5;
evanso 64:e9dfc35a1738 132 spawn_alien_counter_ = 0;
evanso 64:e9dfc35a1738 133 spaceship_lives_ = 3;
evanso 64:e9dfc35a1738 134 reset_map_counter_ = 0;
evanso 64:e9dfc35a1738 135 smart_bomb_counter_ = 4;
evanso 64:e9dfc35a1738 136 create_people();
evanso 64:e9dfc35a1738 137 points_ = 0;
evanso 64:e9dfc35a1738 138 smart_bomb_timer_ = 0;
evanso 64:e9dfc35a1738 139 bullet_timer_ = 0;
evanso 64:e9dfc35a1738 140 lcd_frame_time_flag_ = 0;
evanso 68:bb1650c657ef 141 paused_counter_ = 25;
evanso 64:e9dfc35a1738 142
evanso 64:e9dfc35a1738 143 //Flags for screen control
evanso 64:e9dfc35a1738 144 exit_flag_ = false;
evanso 64:e9dfc35a1738 145 run_save_a_game_flag_ = false;
evanso 64:e9dfc35a1738 146 spaceship_destroyed_ = false;
evanso 64:e9dfc35a1738 147 }
evanso 64:e9dfc35a1738 148
evanso 13:12276eed13ac 149 void GameEngine::gameplay_loop() {
evanso 30:814674b189f0 150 // clear screen and set contrast
evanso 13:12276eed13ac 151 lcd.setContrast(pad.read_pot1());
evanso 8:dd1037c5435b 152 lcd.clear();
evanso 11:ab578a151f67 153
evanso 64:e9dfc35a1738 154 // creats aliens and people
evanso 41:5959256f4aab 155 spawn_aliens();
evanso 41:5959256f4aab 156 spawn_people();
evanso 41:5959256f4aab 157
evanso 41:5959256f4aab 158 //If spaceship is destroyed stop joystick input, dont draw spaceship sprite
evanso 45:fc3238cd28c6 159 if (!spaceship_destroyed_){
evanso 62:f0c86a854a9e 160 read_joystick_direction();
evanso 65:daa792a09e1f 161 //read_accelerometer_direction(accelerometer.get_roll_angle(),
evanso 65:daa792a09e1f 162 //accelerometer.get_pitch_angle());
evanso 41:5959256f4aab 163 spaceship.movement(d_);
evanso 41:5959256f4aab 164 spaceship.draw(lcd);
evanso 47:49fa1adc10b4 165 create_weapons_bullets();
evanso 47:49fa1adc10b4 166 create_weapons_smart_bomb();
evanso 41:5959256f4aab 167 }
evanso 41:5959256f4aab 168
evanso 41:5959256f4aab 169 // Draws objects
evanso 41:5959256f4aab 170 map.draw_map(lcd, d_);
evanso 41:5959256f4aab 171 draw_aliens();
evanso 41:5959256f4aab 172 draw_bullets();
evanso 41:5959256f4aab 173 draw_explosions();
evanso 41:5959256f4aab 174 draw_people();
evanso 45:fc3238cd28c6 175 hud.draw_HUD(lcd, spaceship_lives_, points_, smart_bomb_counter_);
evanso 41:5959256f4aab 176 spaceship_lives_leds();
evanso 41:5959256f4aab 177
evanso 41:5959256f4aab 178 reset_map_timer();
evanso 30:814674b189f0 179
evanso 18:11068b98e261 180 // refresh's screen
evanso 18:11068b98e261 181 lcd.refresh();
evanso 13:12276eed13ac 182 }
evanso 13:12276eed13ac 183
evanso 64:e9dfc35a1738 184 void GameEngine::draw_game_over_screen(){
evanso 64:e9dfc35a1738 185 //Draws game over screen then back to menu
evanso 64:e9dfc35a1738 186 lcd.clear();
evanso 64:e9dfc35a1738 187 lcd.printString("Game Over",15,3);
evanso 64:e9dfc35a1738 188 lcd.refresh();
evanso 64:e9dfc35a1738 189 wait(3);
evanso 64:e9dfc35a1738 190 current_menu_part_ = main_menu;
evanso 62:f0c86a854a9e 191 }
evanso 62:f0c86a854a9e 192
evanso 62:f0c86a854a9e 193 void GameEngine::run_paused_game(){
evanso 64:e9dfc35a1738 194 //Lunch save a game screen
evanso 64:e9dfc35a1738 195 if (pad.B_pressed()||run_save_a_game_flag_){
evanso 64:e9dfc35a1738 196 run_save_a_game_flag_ = true;
evanso 64:e9dfc35a1738 197 run_save_a_game();
evanso 64:e9dfc35a1738 198
evanso 64:e9dfc35a1738 199 //Draw pause screen
evanso 62:f0c86a854a9e 200 }else{
evanso 62:f0c86a854a9e 201 draw_pause_screen();
evanso 62:f0c86a854a9e 202 paused_flag_ = true;
evanso 62:f0c86a854a9e 203 }
evanso 64:e9dfc35a1738 204
evanso 64:e9dfc35a1738 205 //Exit to main menu
evanso 63:4631447440cf 206 if (pad.A_pressed()||saved.get_error()){
evanso 56:663d0546c235 207 current_menu_part_ = main_menu;
evanso 56:663d0546c235 208 exit_flag_ = true;
evanso 56:663d0546c235 209 }
evanso 56:663d0546c235 210 }
evanso 56:663d0546c235 211
evanso 56:663d0546c235 212 void GameEngine::draw_pause_screen(){
evanso 56:663d0546c235 213 // clear screen and set contrast
evanso 56:663d0546c235 214 lcd.setContrast(pad.read_pot1());
evanso 56:663d0546c235 215 lcd.clear();
evanso 56:663d0546c235 216
evanso 56:663d0546c235 217 //Draw strings
evanso 56:663d0546c235 218 lcd.printString("Paused!",24,0);
evanso 69:753ba27325ce 219 lcd.printString("Exit(A)",24,3);
evanso 69:753ba27325ce 220 lcd.printString("Save&Exit(B)",6,4);
evanso 56:663d0546c235 221
evanso 56:663d0546c235 222 // refresh's screen
evanso 56:663d0546c235 223 lcd.refresh();
evanso 56:663d0546c235 224 }
evanso 56:663d0546c235 225
evanso 64:e9dfc35a1738 226 void GameEngine::run_save_a_game(){
evanso 64:e9dfc35a1738 227 //Draw and interact with save ya game screen
evanso 64:e9dfc35a1738 228 read_joystick_direction();
evanso 64:e9dfc35a1738 229 saved.saved_games_scroll(d_);
evanso 64:e9dfc35a1738 230 saved.save_game_screen(lcd);
evanso 64:e9dfc35a1738 231
evanso 64:e9dfc35a1738 232 //Save game to save file that is displayed
evanso 68:bb1650c657ef 233 if (pad.A_pressed()){
evanso 64:e9dfc35a1738 234 //Define struct data
evanso 64:e9dfc35a1738 235 SavedGamesData data;
evanso 64:e9dfc35a1738 236
evanso 64:e9dfc35a1738 237 //Set values in struct data and saves them selected file
evanso 64:e9dfc35a1738 238 data.score = points_;
evanso 64:e9dfc35a1738 239 data.lives = spaceship_lives_;
evanso 64:e9dfc35a1738 240 data.smart_bombs = smart_bomb_counter_;
evanso 64:e9dfc35a1738 241 data.alien_number = alien_number_;
evanso 64:e9dfc35a1738 242 saved.add_saved_data(sd, data, lcd);
evanso 64:e9dfc35a1738 243
evanso 64:e9dfc35a1738 244 //exit to main menu
evanso 64:e9dfc35a1738 245 current_menu_part_ = main_menu;
evanso 64:e9dfc35a1738 246 exit_flag_ = true;
evanso 64:e9dfc35a1738 247 }
evanso 64:e9dfc35a1738 248 }
evanso 64:e9dfc35a1738 249
evanso 64:e9dfc35a1738 250
evanso 64:e9dfc35a1738 251
evanso 64:e9dfc35a1738 252
evanso 64:e9dfc35a1738 253 // Menu Setting ----------------------------------------------------------------
evanso 64:e9dfc35a1738 254
evanso 69:753ba27325ce 255 void GameEngine::run_settings(){
evanso 69:753ba27325ce 256 lcd.clear();
evanso 69:753ba27325ce 257 lcd.setContrast(pad.read_pot1());
evanso 69:753ba27325ce 258
evanso 69:753ba27325ce 259 // Scrolls, draws and changes different parts
evanso 69:753ba27325ce 260 read_joystick_direction();
evanso 69:753ba27325ce 261 setting.settings_scroll(d_);
evanso 69:753ba27325ce 262 setting.display_settings_screen(lcd, pad.read_pot1());
evanso 69:753ba27325ce 263 setting.change_setting(pad.A_pressed());
evanso 69:753ba27325ce 264
evanso 69:753ba27325ce 265 //go back to main menu if b
evanso 69:753ba27325ce 266 if (pad.B_pressed()){
evanso 69:753ba27325ce 267 current_menu_part_ = main_menu;
evanso 69:753ba27325ce 268 }
evanso 69:753ba27325ce 269
evanso 69:753ba27325ce 270 lcd.refresh();
evanso 57:d4ce42c24561 271 }
evanso 57:d4ce42c24561 272
evanso 64:e9dfc35a1738 273
evanso 64:e9dfc35a1738 274
evanso 64:e9dfc35a1738 275 // Menu Saved Games ------------------------------------------------------------
evanso 64:e9dfc35a1738 276
evanso 64:e9dfc35a1738 277 void GameEngine::run_saved_games(){
evanso 64:e9dfc35a1738 278 // Draws and interact with save screen
evanso 64:e9dfc35a1738 279 read_joystick_direction();
evanso 64:e9dfc35a1738 280 saved.saved_games_scroll(d_);
evanso 64:e9dfc35a1738 281 saved.display_saved_games(lcd);
evanso 64:e9dfc35a1738 282
evanso 64:e9dfc35a1738 283 //go back to main menu
evanso 69:753ba27325ce 284 if (pad.B_pressed()){
evanso 64:e9dfc35a1738 285 current_menu_part_ = main_menu;
evanso 64:e9dfc35a1738 286 }
evanso 64:e9dfc35a1738 287
evanso 69:753ba27325ce 288 //Load save, if no file press go back to main menu
evanso 64:e9dfc35a1738 289 if (pad.A_pressed()){
evanso 64:e9dfc35a1738 290 wait(0.035);
evanso 64:e9dfc35a1738 291 saved_games_overide_init();
evanso 64:e9dfc35a1738 292 if(saved.get_error()) {
evanso 64:e9dfc35a1738 293 saved.set_error(false);
evanso 64:e9dfc35a1738 294 current_menu_part_ = main_menu;
evanso 64:e9dfc35a1738 295 }else{
evanso 64:e9dfc35a1738 296 current_menu_part_ = play;
evanso 64:e9dfc35a1738 297 }
evanso 64:e9dfc35a1738 298 }
evanso 64:e9dfc35a1738 299 }
evanso 64:e9dfc35a1738 300
evanso 64:e9dfc35a1738 301 void GameEngine::saved_games_overide_init(){
evanso 64:e9dfc35a1738 302 play_init();
evanso 64:e9dfc35a1738 303
evanso 64:e9dfc35a1738 304 //Creats data object add sett it values to data saved in the file
evanso 64:e9dfc35a1738 305 SavedGamesData displayed_game;
evanso 64:e9dfc35a1738 306 displayed_game = saved.read_saved_data(sd, lcd);
evanso 64:e9dfc35a1738 307
evanso 64:e9dfc35a1738 308 //Set the variables to value in the displayed saved struct
evanso 64:e9dfc35a1738 309 points_ = displayed_game.score;
evanso 64:e9dfc35a1738 310 spaceship_lives_ = displayed_game.lives;
evanso 64:e9dfc35a1738 311 smart_bomb_counter_= displayed_game.smart_bombs;
evanso 64:e9dfc35a1738 312 alien_number_ = displayed_game.alien_number;
evanso 64:e9dfc35a1738 313 }
evanso 64:e9dfc35a1738 314
evanso 64:e9dfc35a1738 315
evanso 64:e9dfc35a1738 316
evanso 42:3aed75338272 317
evanso 39:fc5586b930e3 318 //Spaceship control ------------------------------------------------------------
evanso 39:fc5586b930e3 319
evanso 15:90b6821bcf64 320 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 321 d_ = pad.get_direction();
evanso 18:11068b98e261 322 }
evanso 18:11068b98e261 323
evanso 65:daa792a09e1f 324 void GameEngine::read_accelerometer_direction(float roll,float pitch){
evanso 47:49fa1adc10b4 325 // printf("Pitch = %f Roll = %f\n", pitch, roll);
evanso 47:49fa1adc10b4 326
evanso 47:49fa1adc10b4 327 //north
evanso 47:49fa1adc10b4 328 if(pitch >= -35){
evanso 47:49fa1adc10b4 329 if(roll >= 10){
evanso 47:49fa1adc10b4 330 d_ = NE;
evanso 47:49fa1adc10b4 331 }else if (roll <= -10){
evanso 47:49fa1adc10b4 332 d_ = NW;
evanso 47:49fa1adc10b4 333 }else {
evanso 47:49fa1adc10b4 334 d_ = N;
evanso 47:49fa1adc10b4 335 }
evanso 47:49fa1adc10b4 336 //south
evanso 47:49fa1adc10b4 337 }else if(pitch <= -55){
evanso 47:49fa1adc10b4 338 if (roll >= 10){
evanso 47:49fa1adc10b4 339 d_ = SE;
evanso 47:49fa1adc10b4 340 }else if(roll <= -10){
evanso 47:49fa1adc10b4 341 d_ = SW;
evanso 47:49fa1adc10b4 342 }else {
evanso 47:49fa1adc10b4 343 d_ = S;
evanso 47:49fa1adc10b4 344 }
evanso 47:49fa1adc10b4 345 // East/west
evanso 47:49fa1adc10b4 346 }else {
evanso 47:49fa1adc10b4 347 if (roll >= 10){
evanso 47:49fa1adc10b4 348 d_ = E;
evanso 47:49fa1adc10b4 349 }else if(roll <= -10){
evanso 47:49fa1adc10b4 350 d_ = W;
evanso 47:49fa1adc10b4 351 }else{
evanso 47:49fa1adc10b4 352 d_ = CENTRE;
evanso 47:49fa1adc10b4 353 }
evanso 47:49fa1adc10b4 354 }
evanso 47:49fa1adc10b4 355 }
evanso 47:49fa1adc10b4 356
evanso 39:fc5586b930e3 357 void GameEngine::spaceship_lives_leds(){
evanso 39:fc5586b930e3 358 pad.leds_off();
evanso 39:fc5586b930e3 359
evanso 39:fc5586b930e3 360 //top two red leds on if 1 lives left
evanso 45:fc3238cd28c6 361 if(spaceship_lives_ >= 1){
evanso 39:fc5586b930e3 362 pad.led(1,1);
evanso 39:fc5586b930e3 363 pad.led(4,1);
evanso 39:fc5586b930e3 364 }
evanso 39:fc5586b930e3 365
evanso 39:fc5586b930e3 366 //middle two orange leds also on if 2 lives left
evanso 45:fc3238cd28c6 367 if(spaceship_lives_ >= 2){
evanso 39:fc5586b930e3 368 pad.led(2,1);
evanso 39:fc5586b930e3 369 pad.led(5,1);
evanso 39:fc5586b930e3 370 }
evanso 39:fc5586b930e3 371
evanso 39:fc5586b930e3 372 //bottom two red leds also on if 3 lives left
evanso 45:fc3238cd28c6 373 if(spaceship_lives_ == 3){
evanso 39:fc5586b930e3 374 pad.led(3,1);
evanso 39:fc5586b930e3 375 pad.led(6,1);
evanso 39:fc5586b930e3 376 }
evanso 39:fc5586b930e3 377 }
evanso 39:fc5586b930e3 378
evanso 44:2d957de3bce3 379
evanso 44:2d957de3bce3 380
evanso 36:27aa597db3d2 381 //Weapon control ---------------------------------------------------------------
evanso 36:27aa597db3d2 382
evanso 36:27aa597db3d2 383 void GameEngine::create_weapons_bullets(){
evanso 39:fc5586b930e3 384 // controlls speed that bullet can be fired
evanso 45:fc3238cd28c6 385 if(bullet_timer_ <=0){
evanso 39:fc5586b930e3 386 if (pad.A_pressed()){
evanso 36:27aa597db3d2 387 // Bullet object
evanso 36:27aa597db3d2 388 Weapons new_bullet;
evanso 36:27aa597db3d2 389
evanso 36:27aa597db3d2 390 new_bullet.init(spaceship.get_pos(),
evanso 36:27aa597db3d2 391 spaceship.get_spaceship_sprite_direction(), true);
evanso 36:27aa597db3d2 392
evanso 36:27aa597db3d2 393 // Stores bullet object in vector
evanso 36:27aa597db3d2 394 bullet_vector.push_back(new_bullet);
evanso 39:fc5586b930e3 395 }
evanso 45:fc3238cd28c6 396 bullet_timer_ = 5;
evanso 39:fc5586b930e3 397 }
evanso 45:fc3238cd28c6 398 bullet_timer_--;
evanso 36:27aa597db3d2 399 }
evanso 36:27aa597db3d2 400
evanso 36:27aa597db3d2 401 void GameEngine::create_weapons_smart_bomb(){
evanso 39:fc5586b930e3 402 // timer to stop smart bomb button be accidently pressed twice
evanso 45:fc3238cd28c6 403 if(smart_bomb_timer_ <=0){
evanso 45:fc3238cd28c6 404 if (pad.B_pressed() && smart_bomb_counter_ > 0){
evanso 39:fc5586b930e3 405 weapons.smart_bomb(lcd);
evanso 45:fc3238cd28c6 406 smart_bomb_counter_--;
evanso 39:fc5586b930e3 407
evanso 39:fc5586b930e3 408 //Deletes alien object if on screen
evanso 39:fc5586b930e3 409 for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){
evanso 39:fc5586b930e3 410 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 36:27aa597db3d2 411
evanso 39:fc5586b930e3 412 // Creats explosion if alien was on the screen
evanso 39:fc5586b930e3 413 if(alien_pos.x <= 84 && alien_pos.x >= -6){
evanso 39:fc5586b930e3 414 create_explosion(alien_pos);
evanso 39:fc5586b930e3 415
evanso 39:fc5586b930e3 416 //delete person object if was being abducted by destroyed alien
evanso 39:fc5586b930e3 417 if(alien_vector[i].get_collision_people_element() > -1){
evanso 39:fc5586b930e3 418 people_vector.erase(people_vector.begin() +
evanso 39:fc5586b930e3 419 alien_vector[i].get_collision_people_element());
evanso 39:fc5586b930e3 420 }
evanso 39:fc5586b930e3 421
evanso 39:fc5586b930e3 422 alien_vector.erase(alien_vector.begin()+ i);
evanso 45:fc3238cd28c6 423 points_ ++;
evanso 39:fc5586b930e3 424 }
evanso 36:27aa597db3d2 425 }
evanso 45:fc3238cd28c6 426 smart_bomb_timer_ = 30;
evanso 39:fc5586b930e3 427 }
evanso 39:fc5586b930e3 428 }
evanso 45:fc3238cd28c6 429 smart_bomb_timer_--;
evanso 36:27aa597db3d2 430 }
evanso 36:27aa597db3d2 431
evanso 36:27aa597db3d2 432 void GameEngine::draw_bullets(){
evanso 36:27aa597db3d2 433 // interates over bullet vectors, moves and draws bullet objects
evanso 36:27aa597db3d2 434
evanso 36:27aa597db3d2 435 // Alien bullets
evanso 36:27aa597db3d2 436 for (int i = 0; i < alien_bullet_vector.size(); i++){
evanso 36:27aa597db3d2 437 alien_bullet_vector[i].draw_alien_bullet(lcd, d_);
evanso 36:27aa597db3d2 438
evanso 36:27aa597db3d2 439 // deletes alien bullet object after bullet has moved set distance
evanso 36:27aa597db3d2 440 if(alien_bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 36:27aa597db3d2 441 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 36:27aa597db3d2 442 }
evanso 36:27aa597db3d2 443
evanso 36:27aa597db3d2 444 // Deletes bullet and shpaceship if collision detected
evanso 36:27aa597db3d2 445 if (spaceship.check_collision(alien_bullet_vector[i]) &&
evanso 45:fc3238cd28c6 446 !spaceship_destroyed_ ){
evanso 36:27aa597db3d2 447 create_explosion(spaceship.get_pos());
evanso 36:27aa597db3d2 448
evanso 45:fc3238cd28c6 449 spaceship_destroyed_ = true;
evanso 36:27aa597db3d2 450 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 36:27aa597db3d2 451 }
evanso 36:27aa597db3d2 452 }
evanso 36:27aa597db3d2 453
evanso 36:27aa597db3d2 454 // Spaceship bullets
evanso 36:27aa597db3d2 455 for (int i = 0; i < bullet_vector.size(); i++){
evanso 36:27aa597db3d2 456 bullet_vector[i].draw_bullet(lcd);
evanso 36:27aa597db3d2 457
evanso 36:27aa597db3d2 458 // deletes bullet object after bullet has moved set distance
evanso 36:27aa597db3d2 459 if(bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 36:27aa597db3d2 460 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 36:27aa597db3d2 461 }
evanso 36:27aa597db3d2 462 }
evanso 36:27aa597db3d2 463 }
evanso 36:27aa597db3d2 464
evanso 36:27aa597db3d2 465
evanso 36:27aa597db3d2 466
evanso 36:27aa597db3d2 467 //Alien Control ----------------------------------------------------------------
evanso 36:27aa597db3d2 468
evanso 32:c006a9882778 469 void GameEngine::spawn_aliens(){
evanso 51:35cb8e604b72 470 if(alien_vector.size() <= alien_number_){
evanso 34:85ccc16f24d2 471 create_alien();
evanso 32:c006a9882778 472 }
evanso 32:c006a9882778 473
evanso 45:fc3238cd28c6 474 //printf( " alien_number_counter_ = %d\n",alien_number_counter_);
evanso 45:fc3238cd28c6 475
evanso 34:85ccc16f24d2 476 //slowley incresing the alien counter as game goes on to make harder
evanso 45:fc3238cd28c6 477 if(spawn_alien_counter_%(500*spawn_time_multipler_) == 0){
evanso 51:35cb8e604b72 478 alien_number_++;
evanso 45:fc3238cd28c6 479 spawn_time_multipler_++;
evanso 32:c006a9882778 480 }
evanso 34:85ccc16f24d2 481
evanso 45:fc3238cd28c6 482 spawn_alien_counter_++;
evanso 32:c006a9882778 483 }
evanso 32:c006a9882778 484
evanso 36:27aa597db3d2 485 void GameEngine::create_alien(){
evanso 36:27aa597db3d2 486 int position_x_start = 0;
evanso 36:27aa597db3d2 487
evanso 36:27aa597db3d2 488 // Alien object
evanso 36:27aa597db3d2 489 Alien new_alien;
evanso 36:27aa597db3d2 490
evanso 36:27aa597db3d2 491 //spawns aliens between x > 84 and x <0
evanso 36:27aa597db3d2 492 if(rand() % 2){
evanso 36:27aa597db3d2 493 position_x_start = rand() % 84 + 84;
evanso 36:27aa597db3d2 494 }else{
evanso 36:27aa597db3d2 495 position_x_start =rand() % 83 - 84;
evanso 36:27aa597db3d2 496 }
evanso 39:fc5586b930e3 497 new_alien.init(pad, position_x_start, (rand() % 33 + 9));
evanso 36:27aa597db3d2 498
evanso 36:27aa597db3d2 499 // Stores alien object in vector
evanso 36:27aa597db3d2 500 alien_vector.push_back(new_alien);
evanso 36:27aa597db3d2 501 }
evanso 36:27aa597db3d2 502
evanso 36:27aa597db3d2 503 void GameEngine::check_alien_people_collision(int i){
evanso 40:71f947254fda 504 // only a collision if all three of theses conditions are met:
evanso 40:71f947254fda 505 // check all these codition for every alien to every person
evanso 40:71f947254fda 506 // 1)person hasn't already had a collision
evanso 40:71f947254fda 507 // 2)There is an acutaual collision of people and alien
evanso 36:27aa597db3d2 508 for (int x = 0; x < people_vector.size(); x++){
evanso 36:27aa597db3d2 509 if (!people_vector[x].get_alien_collision_flag() &&
evanso 36:27aa597db3d2 510 people_vector[x].check_alien_collision(alien_vector[i])){
evanso 36:27aa597db3d2 511 alien_vector[i].set_collision_people_element(x);
evanso 36:27aa597db3d2 512 }
evanso 36:27aa597db3d2 513 }
evanso 36:27aa597db3d2 514
evanso 36:27aa597db3d2 515 //draws collision if detected
evanso 36:27aa597db3d2 516 if(alien_vector[i].get_collision_people_element() > -1){
evanso 36:27aa597db3d2 517 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 36:27aa597db3d2 518 map.get_length_map(), map.get_position_x_map(),true);
evanso 36:27aa597db3d2 519 }else{
evanso 36:27aa597db3d2 520 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 36:27aa597db3d2 521 map.get_length_map(), map.get_position_x_map(),false);
evanso 36:27aa597db3d2 522 }
evanso 36:27aa597db3d2 523 }
evanso 36:27aa597db3d2 524
evanso 36:27aa597db3d2 525 void GameEngine::alliens_fire_bullets(int i){
evanso 36:27aa597db3d2 526 if (alien_vector[i].get_alien_fire_counter()%60 == 0){
evanso 36:27aa597db3d2 527 Weapons alien_bullet;
evanso 36:27aa597db3d2 528
evanso 36:27aa597db3d2 529 // fires bullet towards direction of spaceship
evanso 36:27aa597db3d2 530 bool alien_bullet_direction = false;
evanso 36:27aa597db3d2 531 Vector2D spaceship_pos = spaceship.get_pos();
evanso 36:27aa597db3d2 532 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 36:27aa597db3d2 533 if(spaceship_pos.x > alien_pos.x){
evanso 36:27aa597db3d2 534 alien_bullet_direction = true;
evanso 36:27aa597db3d2 535 }
evanso 36:27aa597db3d2 536
evanso 36:27aa597db3d2 537 alien_bullet.init(alien_vector[i].get_pos(),
evanso 36:27aa597db3d2 538 alien_bullet_direction , false);
evanso 36:27aa597db3d2 539
evanso 36:27aa597db3d2 540 // Stores bullet object in vector
evanso 36:27aa597db3d2 541 alien_bullet_vector.push_back(alien_bullet);
evanso 36:27aa597db3d2 542 }
evanso 36:27aa597db3d2 543 }
evanso 36:27aa597db3d2 544
evanso 36:27aa597db3d2 545 void GameEngine::delete_aliens(int i){
evanso 36:27aa597db3d2 546 for (int x = 0; x < bullet_vector.size(); x++){
evanso 36:27aa597db3d2 547 if (alien_vector[i].check_collision(bullet_vector[x])){
evanso 36:27aa597db3d2 548 create_explosion(alien_vector[i].get_pos());
evanso 36:27aa597db3d2 549
evanso 36:27aa597db3d2 550 //delete person object if was carried by destroyed alien
evanso 36:27aa597db3d2 551 if(alien_vector[i].get_collision_people_element() > -1){
evanso 36:27aa597db3d2 552 people_vector.erase(people_vector.begin() +
evanso 36:27aa597db3d2 553 alien_vector[i].get_collision_people_element());
evanso 36:27aa597db3d2 554 }
evanso 36:27aa597db3d2 555
evanso 39:fc5586b930e3 556 bullet_vector.erase(bullet_vector.begin()+ x);
evanso 39:fc5586b930e3 557 alien_vector.erase(alien_vector.begin()+ i);
evanso 45:fc3238cd28c6 558 points_ ++;
evanso 36:27aa597db3d2 559 }
evanso 36:27aa597db3d2 560 }
evanso 36:27aa597db3d2 561 }
evanso 36:27aa597db3d2 562
evanso 36:27aa597db3d2 563 void GameEngine::draw_aliens(){
evanso 36:27aa597db3d2 564 // interates over alien vector and draws each new_alien object
evanso 36:27aa597db3d2 565 for (int i = 0; i < alien_vector.size(); i++){
evanso 36:27aa597db3d2 566
evanso 36:27aa597db3d2 567 check_alien_people_collision(i);
evanso 36:27aa597db3d2 568
evanso 36:27aa597db3d2 569 // deleted spaceship if alien ship touches spaceship
evanso 36:27aa597db3d2 570 if (spaceship.check_alien_collision(alien_vector[i]) &&
evanso 45:fc3238cd28c6 571 !spaceship_destroyed_){
evanso 36:27aa597db3d2 572 create_explosion(spaceship.get_pos());
evanso 45:fc3238cd28c6 573 spaceship_destroyed_ = true;
evanso 36:27aa597db3d2 574 }
evanso 36:27aa597db3d2 575
evanso 36:27aa597db3d2 576 alliens_fire_bullets(i);
evanso 36:27aa597db3d2 577
evanso 36:27aa597db3d2 578 delete_aliens(i);
evanso 34:85ccc16f24d2 579 }
evanso 34:85ccc16f24d2 580 }
evanso 34:85ccc16f24d2 581
evanso 36:27aa597db3d2 582
evanso 36:27aa597db3d2 583
evanso 36:27aa597db3d2 584 //Explotion Control ------------------------------------------------------------
evanso 36:27aa597db3d2 585
evanso 36:27aa597db3d2 586 void GameEngine::create_explosion(Vector2D destroyed_position){
evanso 36:27aa597db3d2 587 // explosion object
evanso 36:27aa597db3d2 588 Explosion new_explosion;
evanso 36:27aa597db3d2 589
evanso 36:27aa597db3d2 590 new_explosion.init(destroyed_position);
evanso 33:7fedd8029473 591
evanso 36:27aa597db3d2 592 // Stores explosion object in vector
evanso 36:27aa597db3d2 593 explosion_vector.push_back(new_explosion);
evanso 36:27aa597db3d2 594 }
evanso 36:27aa597db3d2 595
evanso 36:27aa597db3d2 596 void GameEngine::draw_explosions(){
evanso 36:27aa597db3d2 597 // interates over expoltion vector and draws each explosion object then
evanso 36:27aa597db3d2 598 // deleted object after set size
evanso 36:27aa597db3d2 599 for (int i = 0; i < explosion_vector.size(); i++){
evanso 36:27aa597db3d2 600 explosion_vector[i].draw_explosion(lcd);
evanso 31:6015e8ed859c 601
evanso 36:27aa597db3d2 602 // delete explosion after reaches set size
evanso 36:27aa597db3d2 603 if(explosion_vector[i].get_explosion_radius() == 8){
evanso 36:27aa597db3d2 604 explosion_vector.erase(explosion_vector.begin()+ i);
evanso 31:6015e8ed859c 605 }
evanso 36:27aa597db3d2 606 }
evanso 36:27aa597db3d2 607 }
evanso 36:27aa597db3d2 608
evanso 36:27aa597db3d2 609
evanso 36:27aa597db3d2 610
evanso 36:27aa597db3d2 611 //People Control ---------------------------------------------------------------
evanso 36:27aa597db3d2 612
evanso 36:27aa597db3d2 613 void GameEngine::spawn_people(){
evanso 36:27aa597db3d2 614 // Keeps number of people objects constant as people objecs are erased
evanso 36:27aa597db3d2 615 if(people_vector.size() <= 5){
evanso 36:27aa597db3d2 616 create_people();
evanso 31:6015e8ed859c 617 }
evanso 20:febd920ec29e 618 }
evanso 20:febd920ec29e 619
evanso 33:7fedd8029473 620 void GameEngine::create_people(){
evanso 33:7fedd8029473 621 // People object
evanso 33:7fedd8029473 622 People people;
evanso 33:7fedd8029473 623
evanso 33:7fedd8029473 624 people.init(pad, (rand() % 168 - 84));
evanso 33:7fedd8029473 625
evanso 33:7fedd8029473 626 // Stores alien object in vector
evanso 33:7fedd8029473 627 people_vector.push_back(people);
evanso 33:7fedd8029473 628 }
evanso 33:7fedd8029473 629
evanso 33:7fedd8029473 630 void GameEngine::draw_people(){
evanso 33:7fedd8029473 631 for (int i = 0; i < people_vector.size(); i++){
evanso 34:85ccc16f24d2 632 people_vector[i].draw_people(lcd, d_,
evanso 34:85ccc16f24d2 633 map.get_length_map(), map.get_position_x_map());
evanso 34:85ccc16f24d2 634
evanso 34:85ccc16f24d2 635 //errase person if at top of screen as captured by alien and alien is
evanso 34:85ccc16f24d2 636 //set to track mode
evanso 34:85ccc16f24d2 637 Vector2D people_pos = people_vector[i].get_pos();
evanso 40:71f947254fda 638
evanso 40:71f947254fda 639 for (int x = 0; x < alien_vector.size(); x++){
evanso 40:71f947254fda 640 Vector2D alien_pos = alien_vector[x].get_pos();
evanso 40:71f947254fda 641 if(people_pos.y < 30 && alien_pos.y < 9){
evanso 40:71f947254fda 642
evanso 40:71f947254fda 643 //set alien who abducted person to track mode
evanso 34:85ccc16f24d2 644 if(alien_vector[x].get_collision_people_element() == i){
evanso 34:85ccc16f24d2 645 alien_vector[x].set_track_flag(true);
evanso 40:71f947254fda 646 people_vector.erase(people_vector.begin()+ i);
evanso 34:85ccc16f24d2 647 }
evanso 40:71f947254fda 648
evanso 40:71f947254fda 649 }
evanso 34:85ccc16f24d2 650
evanso 34:85ccc16f24d2 651 }
evanso 33:7fedd8029473 652 }
evanso 33:7fedd8029473 653 }
evanso 33:7fedd8029473 654
evanso 19:1bc0a2d22054 655
evanso 25:70b55f5bfc87 656
evanso 36:27aa597db3d2 657 //Map Control-------------------------------------------------------------------
evanso 19:1bc0a2d22054 658
evanso 36:27aa597db3d2 659 void GameEngine::reset_map_timer(){
evanso 45:fc3238cd28c6 660 if(spaceship_destroyed_){
evanso 45:fc3238cd28c6 661 reset_map_counter_++;
evanso 36:27aa597db3d2 662
evanso 36:27aa597db3d2 663 // stops map movement
evanso 36:27aa597db3d2 664 d_ = CENTRE;
evanso 36:27aa597db3d2 665
evanso 36:27aa597db3d2 666 // reset map after set time
evanso 45:fc3238cd28c6 667 if(reset_map_counter_ == 50){
evanso 36:27aa597db3d2 668 reset_map();
evanso 25:70b55f5bfc87 669 }
evanso 36:27aa597db3d2 670 }
evanso 25:70b55f5bfc87 671 }
evanso 36:27aa597db3d2 672
evanso 30:814674b189f0 673 void GameEngine::reset_map(){
evanso 36:27aa597db3d2 674 // Reassign values to variables
evanso 30:814674b189f0 675 spaceship.init();
evanso 45:fc3238cd28c6 676 spaceship_lives_--;
evanso 45:fc3238cd28c6 677 spaceship_destroyed_ = false;
evanso 45:fc3238cd28c6 678 reset_map_counter_ = 0;
evanso 33:7fedd8029473 679
evanso 34:85ccc16f24d2 680 // erase aliens so redrawn in random positions when respawning spaceship
evanso 34:85ccc16f24d2 681 for (int i = (alien_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 682 alien_vector.erase(alien_vector.begin()+ i);
evanso 34:85ccc16f24d2 683 }
evanso 36:27aa597db3d2 684
evanso 34:85ccc16f24d2 685 // erase all people so redrawn at bottom of map
evanso 34:85ccc16f24d2 686 for (int i = (people_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 687 people_vector.erase(people_vector.begin()+ i);
evanso 34:85ccc16f24d2 688 }
evanso 36:27aa597db3d2 689
evanso 36:27aa597db3d2 690 // erase all alien bullets
evanso 34:85ccc16f24d2 691 for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 692 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 33:7fedd8029473 693 }
evanso 20:febd920ec29e 694 }