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:
Tue Apr 28 19:13:12 2020 +0000
Revision:
15:90b6821bcf64
Parent:
13:12276eed13ac
Child:
16:1ee3d3804557
Added DEBUG compile macros to check calculate_map_map was producing correct output

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 8:dd1037c5435b 12 pad.init();
evanso 8:dd1037c5435b 13 lcd.init();
evanso 8:dd1037c5435b 14 spaceship.init();
evanso 13:12276eed13ac 15 map.init(pad);
evanso 8:dd1037c5435b 16 move_map_= 0;
evanso 11:ab578a151f67 17
evanso 8:dd1037c5435b 18 }
evanso 8:dd1037c5435b 19
evanso 13:12276eed13ac 20 void GameEngine::gameplay_loop() {
evanso 11:ab578a151f67 21 // clear screen
evanso 13:12276eed13ac 22 lcd.setContrast(pad.read_pot1());
evanso 8:dd1037c5435b 23 lcd.clear();
evanso 11:ab578a151f67 24
evanso 15:90b6821bcf64 25 // Gets movements
evanso 15:90b6821bcf64 26 read_joystick_direction();
evanso 13:12276eed13ac 27 spaceship.movement(d_);
evanso 15:90b6821bcf64 28 calculate_map_movement();
evanso 13:12276eed13ac 29
evanso 15:90b6821bcf64 30 // Redraws
evanso 15:90b6821bcf64 31 spaceship.draw(lcd);
evanso 8:dd1037c5435b 32 map.draw_map(lcd, move_map_);
evanso 11:ab578a151f67 33
evanso 11:ab578a151f67 34 //refresh's screen
evanso 8:dd1037c5435b 35 lcd.refresh();
evanso 8:dd1037c5435b 36 }
evanso 8:dd1037c5435b 37
evanso 15:90b6821bcf64 38 void GameEngine::calculate_map_movement(){
evanso 13:12276eed13ac 39 // Gets postition of spaceship
evanso 15:90b6821bcf64 40 int position_x_spaceship_= spaceship.get_position_x_spaceship();
evanso 13:12276eed13ac 41
evanso 11:ab578a151f67 42 // moves the map in oposite direction to spaceship when it's position is at min and max x positions and joystick has direction
evanso 13:12276eed13ac 43 if (position_x_spaceship_ == 21 && d_ != CENTRE){
evanso 8:dd1037c5435b 44 move_map_ = 1;
evanso 13:12276eed13ac 45 }else if (position_x_spaceship_ == 53 && d_ != CENTRE){
evanso 8:dd1037c5435b 46 move_map_ = -1;
evanso 8:dd1037c5435b 47 }else {
evanso 8:dd1037c5435b 48 move_map_ = 0;
evanso 15:90b6821bcf64 49 }
evanso 15:90b6821bcf64 50
evanso 15:90b6821bcf64 51 // Debug and check variables when function is called
evanso 15:90b6821bcf64 52 #ifdef CALCULATE_MAP_MOVEMENT_DEBUG
evanso 15:90b6821bcf64 53 printf("move map = %d\n", move_map_);
evanso 15:90b6821bcf64 54 printf("direction = %d\n", d_);
evanso 15:90b6821bcf64 55 printf("x = %d\n", position_x_spaceship_);
evanso 15:90b6821bcf64 56 #endif
evanso 13:12276eed13ac 57 }
evanso 13:12276eed13ac 58
evanso 15:90b6821bcf64 59 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 60 d_ = pad.get_direction();
evanso 8:dd1037c5435b 61 }