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:
Thu May 14 22:41:16 2020 +0000
Revision:
33:7fedd8029473
Parent:
32:c006a9882778
Child:
34:85ccc16f24d2
Began work on people class. People now randomly print on screen.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 8:dd1037c5435b 1 #include "GameEngine.h"
evanso 8:dd1037c5435b 2
evanso 8:dd1037c5435b 3 GameEngine::GameEngine() {
evanso 8:dd1037c5435b 4
evanso 8:dd1037c5435b 5 }
evanso 8:dd1037c5435b 6
evanso 8:dd1037c5435b 7 GameEngine::~GameEngine() {
evanso 8:dd1037c5435b 8
evanso 8:dd1037c5435b 9 }
evanso 8:dd1037c5435b 10
evanso 13:12276eed13ac 11 void GameEngine::init() {
evanso 32:c006a9882778 12 srand(pad.read_adc()*64000);
evanso 8:dd1037c5435b 13 pad.init();
evanso 8:dd1037c5435b 14 lcd.init();
evanso 8:dd1037c5435b 15 spaceship.init();
evanso 13:12276eed13ac 16 map.init(pad);
evanso 32:c006a9882778 17
evanso 32:c006a9882778 18 //spawn 5 aliens at start of game
evanso 32:c006a9882778 19 for(int i = 0; i < 6; i++){
evanso 32:c006a9882778 20 create_alien();
evanso 32:c006a9882778 21 }
evanso 33:7fedd8029473 22 spawn_alien_rate = 250;
evanso 33:7fedd8029473 23 spawn_alien_counter = 0;
evanso 30:814674b189f0 24 spaceship_lives = 3;
evanso 30:814674b189f0 25 spaceship_destroyed = false;
evanso 30:814674b189f0 26 reset_map_counter = 0;
evanso 31:6015e8ed859c 27 smart_bomb_counter = 5;
evanso 33:7fedd8029473 28 create_people();
evanso 8:dd1037c5435b 29 }
evanso 8:dd1037c5435b 30
evanso 13:12276eed13ac 31 void GameEngine::gameplay_loop() {
evanso 30:814674b189f0 32 // clear screen and set contrast
evanso 13:12276eed13ac 33 lcd.setContrast(pad.read_pot1());
evanso 8:dd1037c5435b 34 lcd.clear();
evanso 11:ab578a151f67 35
evanso 33:7fedd8029473 36 // creat objects
evanso 31:6015e8ed859c 37 create_weapons();
evanso 32:c006a9882778 38 spawn_aliens();
evanso 19:1bc0a2d22054 39
evanso 33:7fedd8029473 40 //If spaceship is destroyed can't move
evanso 30:814674b189f0 41 if (!spaceship_destroyed){
evanso 33:7fedd8029473 42 read_joystick_direction();
evanso 33:7fedd8029473 43 spaceship.movement(d_);
evanso 30:814674b189f0 44 spaceship.draw(lcd);
evanso 30:814674b189f0 45 }
evanso 33:7fedd8029473 46
evanso 33:7fedd8029473 47 // Draws objects
evanso 18:11068b98e261 48 map.draw_map(lcd, d_);
evanso 20:febd920ec29e 49 draw_aliens();
evanso 19:1bc0a2d22054 50 draw_bullets();
evanso 33:7fedd8029473 51 draw_explosions();
evanso 33:7fedd8029473 52 draw_people();
evanso 30:814674b189f0 53
evanso 30:814674b189f0 54 // Resets map after set frames so spaceship explosion animation showes
evanso 30:814674b189f0 55 if(spaceship_destroyed){
evanso 30:814674b189f0 56 reset_map_counter++;
evanso 33:7fedd8029473 57 d_ = CENTRE;
evanso 30:814674b189f0 58 if(reset_map_counter == 50){
evanso 30:814674b189f0 59 reset_map();
evanso 30:814674b189f0 60 }
evanso 30:814674b189f0 61 }
evanso 30:814674b189f0 62
evanso 18:11068b98e261 63 // refresh's screen
evanso 18:11068b98e261 64 lcd.refresh();
evanso 13:12276eed13ac 65 }
evanso 13:12276eed13ac 66
evanso 15:90b6821bcf64 67 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 68 d_ = pad.get_direction();
evanso 18:11068b98e261 69 }
evanso 18:11068b98e261 70
evanso 32:c006a9882778 71 void GameEngine::spawn_aliens(){
evanso 33:7fedd8029473 72 int rounded_spawn_counter = floor(spawn_alien_rate);
evanso 33:7fedd8029473 73 if(spawn_alien_counter%250== 0){
evanso 32:c006a9882778 74 create_alien();
evanso 32:c006a9882778 75 }
evanso 32:c006a9882778 76
evanso 32:c006a9882778 77 if(spawn_alien_counter > 125){
evanso 33:7fedd8029473 78 spawn_alien_rate --;
evanso 32:c006a9882778 79 }
evanso 33:7fedd8029473 80 spawn_alien_counter++;
evanso 32:c006a9882778 81 }
evanso 32:c006a9882778 82
evanso 31:6015e8ed859c 83 void GameEngine::create_weapons(){
evanso 31:6015e8ed859c 84 //Spaceship bullets
evanso 19:1bc0a2d22054 85 if (pad.A_pressed()){
evanso 19:1bc0a2d22054 86 // Bullet object
evanso 19:1bc0a2d22054 87 Weapons new_bullet;
evanso 19:1bc0a2d22054 88
evanso 27:8bb2bd97c319 89 new_bullet.init(spaceship.get_pos(),
evanso 28:a5958497d5ce 90 spaceship.get_spaceship_sprite_direction(), true);
evanso 19:1bc0a2d22054 91
evanso 20:febd920ec29e 92 // Stores bullet object in vector
evanso 19:1bc0a2d22054 93 bullet_vector.push_back(new_bullet);
evanso 20:febd920ec29e 94 }
evanso 33:7fedd8029473 95
evanso 31:6015e8ed859c 96 // Smart bomb
evanso 31:6015e8ed859c 97 if (pad.B_pressed() && smart_bomb_counter > 0){
evanso 31:6015e8ed859c 98 weapons.smart_bomb(lcd);
evanso 31:6015e8ed859c 99 smart_bomb_counter--;
evanso 31:6015e8ed859c 100
evanso 31:6015e8ed859c 101 //Deletes alien object if on screen
evanso 33:7fedd8029473 102 for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){
evanso 31:6015e8ed859c 103 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 31:6015e8ed859c 104
evanso 31:6015e8ed859c 105 if(alien_pos.x <= 84 && alien_pos.x >= -6){
evanso 31:6015e8ed859c 106 create_explosion(alien_pos);
evanso 31:6015e8ed859c 107 alien_vector.erase(alien_vector.begin()+ i);
evanso 31:6015e8ed859c 108 }
evanso 31:6015e8ed859c 109 }
evanso 31:6015e8ed859c 110
evanso 31:6015e8ed859c 111 }
evanso 20:febd920ec29e 112 }
evanso 20:febd920ec29e 113
evanso 33:7fedd8029473 114 void GameEngine::create_people(){
evanso 33:7fedd8029473 115 // People object
evanso 33:7fedd8029473 116 People people;
evanso 33:7fedd8029473 117
evanso 33:7fedd8029473 118 people.init(pad, (rand() % 168 - 84));
evanso 33:7fedd8029473 119
evanso 33:7fedd8029473 120 // Stores alien object in vector
evanso 33:7fedd8029473 121 people_vector.push_back(people);
evanso 33:7fedd8029473 122 }
evanso 33:7fedd8029473 123
evanso 33:7fedd8029473 124 void GameEngine::draw_people(){
evanso 33:7fedd8029473 125 for (int i = 0; i < people_vector.size(); i++){
evanso 33:7fedd8029473 126 people_vector[i].draw_people(lcd, d_);
evanso 33:7fedd8029473 127 }
evanso 33:7fedd8029473 128 }
evanso 33:7fedd8029473 129
evanso 20:febd920ec29e 130 void GameEngine::create_alien(){
evanso 32:c006a9882778 131 int position_x_start = 0;
evanso 32:c006a9882778 132
evanso 20:febd920ec29e 133 // Alien object
evanso 20:febd920ec29e 134 Alien new_alien;
evanso 32:c006a9882778 135
evanso 32:c006a9882778 136 //Creats randon number for x pos for x > 84 and x <0
evanso 32:c006a9882778 137 if(rand() % 2){
evanso 33:7fedd8029473 138 position_x_start = rand() % 84 + 84;
evanso 32:c006a9882778 139 }else{
evanso 33:7fedd8029473 140 position_x_start =rand() % 83 - 84;
evanso 32:c006a9882778 141 }
evanso 32:c006a9882778 142
evanso 32:c006a9882778 143 new_alien.init(pad, position_x_start, (rand() % 45));
evanso 20:febd920ec29e 144
evanso 20:febd920ec29e 145 // Stores alien object in vector
evanso 20:febd920ec29e 146 alien_vector.push_back(new_alien);
evanso 19:1bc0a2d22054 147 }
evanso 19:1bc0a2d22054 148
evanso 29:e96d91f1d39c 149 void GameEngine::create_explosion(Vector2D destroyed_position){
evanso 25:70b55f5bfc87 150 // explosion object
evanso 25:70b55f5bfc87 151 Explosion new_explosion;
evanso 25:70b55f5bfc87 152
evanso 29:e96d91f1d39c 153 new_explosion.init(destroyed_position);
evanso 25:70b55f5bfc87 154
evanso 25:70b55f5bfc87 155 // Stores explosion object in vector
evanso 25:70b55f5bfc87 156 explosion_vector.push_back(new_explosion);
evanso 25:70b55f5bfc87 157 }
evanso 25:70b55f5bfc87 158
evanso 19:1bc0a2d22054 159 void GameEngine::draw_bullets(){
evanso 29:e96d91f1d39c 160 // interates over bullet vectors, moves and draws bullet object
evanso 29:e96d91f1d39c 161
evanso 29:e96d91f1d39c 162 // Alien bullets
evanso 28:a5958497d5ce 163 for (int i = 0; i < alien_bullet_vector.size(); i++){
evanso 28:a5958497d5ce 164 alien_bullet_vector[i].draw_alien_bullet(lcd, d_);
evanso 28:a5958497d5ce 165
evanso 29:e96d91f1d39c 166 // deletes alien bullet object after bullet has moved set distance
evanso 33:7fedd8029473 167 if(alien_bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 28:a5958497d5ce 168 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 28:a5958497d5ce 169 }
evanso 29:e96d91f1d39c 170
evanso 30:814674b189f0 171 // Deletes bullet and shpaceship if collision detected
evanso 30:814674b189f0 172 if (spaceship.check_collision(alien_bullet_vector[i]) &&
evanso 30:814674b189f0 173 !spaceship_destroyed ){
evanso 29:e96d91f1d39c 174 create_explosion(spaceship.get_pos());
evanso 30:814674b189f0 175
evanso 30:814674b189f0 176 spaceship_destroyed = true;
evanso 30:814674b189f0 177 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 29:e96d91f1d39c 178 }
evanso 28:a5958497d5ce 179 }
evanso 29:e96d91f1d39c 180
evanso 29:e96d91f1d39c 181 // Spaceship bullets
evanso 19:1bc0a2d22054 182 for (int i = 0; i < bullet_vector.size(); i++){
evanso 19:1bc0a2d22054 183 bullet_vector[i].draw_bullet(lcd);
evanso 28:a5958497d5ce 184
evanso 28:a5958497d5ce 185 // deletes bullet object after bullet has moved set distance
evanso 28:a5958497d5ce 186 if(bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 20:febd920ec29e 187 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 20:febd920ec29e 188 }
evanso 19:1bc0a2d22054 189 }
evanso 19:1bc0a2d22054 190 }
evanso 19:1bc0a2d22054 191
evanso 20:febd920ec29e 192 void GameEngine::draw_aliens(){
evanso 29:e96d91f1d39c 193 // interates over alien vector and draws each new_alien object
evanso 20:febd920ec29e 194 for (int i = 0; i < alien_vector.size(); i++){
evanso 27:8bb2bd97c319 195 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 27:8bb2bd97c319 196 map.get_length_map(), map.get_position_x_map());
evanso 20:febd920ec29e 197
evanso 30:814674b189f0 198 // deleted spaceship if alien ship touches spaceship
evanso 30:814674b189f0 199 if (spaceship.check_alien_collision(alien_vector[i]) &&
evanso 30:814674b189f0 200 !spaceship_destroyed){
evanso 30:814674b189f0 201 create_explosion(spaceship.get_pos());
evanso 30:814674b189f0 202 spaceship_destroyed = true;
evanso 30:814674b189f0 203 }
evanso 30:814674b189f0 204
evanso 29:e96d91f1d39c 205 // alien sprites fire bullet randomley
evanso 28:a5958497d5ce 206 if (alien_vector[i].get_alien_fire_counter()%60 == 0){
evanso 28:a5958497d5ce 207 Weapons alien_bullet;
evanso 28:a5958497d5ce 208
evanso 28:a5958497d5ce 209 // fires bullet towards direction of spaceship
evanso 28:a5958497d5ce 210 bool alien_bullet_direction = false;
evanso 28:a5958497d5ce 211 Vector2D spaceship_pos = spaceship.get_pos();
evanso 28:a5958497d5ce 212 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 28:a5958497d5ce 213 if(spaceship_pos.x > alien_pos.x){
evanso 28:a5958497d5ce 214 alien_bullet_direction = true;
evanso 28:a5958497d5ce 215 }
evanso 28:a5958497d5ce 216
evanso 28:a5958497d5ce 217 alien_bullet.init(alien_vector[i].get_pos(),
evanso 28:a5958497d5ce 218 alien_bullet_direction , false);
evanso 28:a5958497d5ce 219
evanso 28:a5958497d5ce 220 // Stores bullet object in vector
evanso 28:a5958497d5ce 221 alien_bullet_vector.push_back(alien_bullet);
evanso 28:a5958497d5ce 222 }
evanso 28:a5958497d5ce 223
evanso 29:e96d91f1d39c 224 // Deletes bullet and alien if collision detected and draws explosion
evanso 29:e96d91f1d39c 225 for (int x = 0; x < bullet_vector.size(); x++){
evanso 29:e96d91f1d39c 226 if (alien_vector[i].check_collision(bullet_vector[x])){
evanso 29:e96d91f1d39c 227 create_explosion(alien_vector[i].get_pos());
evanso 25:70b55f5bfc87 228
evanso 29:e96d91f1d39c 229 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 29:e96d91f1d39c 230 alien_vector.erase(alien_vector.begin()+ i);
evanso 29:e96d91f1d39c 231 }
evanso 25:70b55f5bfc87 232 }
evanso 25:70b55f5bfc87 233 }
evanso 25:70b55f5bfc87 234 }
evanso 25:70b55f5bfc87 235
evanso 25:70b55f5bfc87 236 void GameEngine::draw_explosions(){
evanso 27:8bb2bd97c319 237 // interates over expoltion vector and draws each explosion object then
evanso 27:8bb2bd97c319 238 // deleted object after set size
evanso 25:70b55f5bfc87 239 for (int i = 0; i < explosion_vector.size(); i++){
evanso 25:70b55f5bfc87 240 explosion_vector[i].draw_explosion(lcd);
evanso 25:70b55f5bfc87 241
evanso 25:70b55f5bfc87 242 // delete explosion after reaches set size
evanso 25:70b55f5bfc87 243 if(explosion_vector[i].get_explosion_radius() == 8){
evanso 25:70b55f5bfc87 244 explosion_vector.erase(explosion_vector.begin()+ i);
evanso 20:febd920ec29e 245 }
evanso 20:febd920ec29e 246 }
evanso 30:814674b189f0 247 }
evanso 30:814674b189f0 248
evanso 30:814674b189f0 249 void GameEngine::reset_map(){
evanso 30:814674b189f0 250 spaceship.init();
evanso 30:814674b189f0 251 spaceship_lives--;
evanso 30:814674b189f0 252 spaceship_destroyed = false;
evanso 30:814674b189f0 253 reset_map_counter = 0;
evanso 33:7fedd8029473 254
evanso 33:7fedd8029473 255 //moves alien of screen to random positions when respawning spaceship
evanso 33:7fedd8029473 256 for (int i = 0; i < alien_vector.size(); i++){
evanso 33:7fedd8029473 257 int position_x_start = 0;
evanso 33:7fedd8029473 258
evanso 33:7fedd8029473 259 //Creats randon number for x pos for x > 84 and x <0
evanso 33:7fedd8029473 260 if(rand() % 2){
evanso 33:7fedd8029473 261 position_x_start = rand() % 84 + 84;
evanso 33:7fedd8029473 262 }else{
evanso 33:7fedd8029473 263 position_x_start =rand() % 83 - 84;
evanso 33:7fedd8029473 264 }
evanso 33:7fedd8029473 265
evanso 33:7fedd8029473 266 alien_vector[i].set_alien_x_pos(position_x_start);
evanso 33:7fedd8029473 267 }
evanso 20:febd920ec29e 268 }