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:
Wed May 27 02:06:05 2020 +0000
Revision:
87:832ca78426b5
Parent:
85:87bc28b151d8
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 3:dee187b8b30c 1 #include "Spaceship.h"
evanso 82:3211b31e9421 2
evanso 85:87bc28b151d8 3 // Constant Definitions
evanso 82:3211b31e9421 4 #define SPRITE_X_LENGTH 13
evanso 82:3211b31e9421 5 #define SPRITE_Y_LENGTH 4
evanso 82:3211b31e9421 6 #define STARTING_X_POS 36
evanso 82:3211b31e9421 7 #define STARTING_Y_POS 22
evanso 82:3211b31e9421 8 #define OFF_SCREEN_Y_ONE 9
evanso 82:3211b31e9421 9 #define OFF_SCREEN_Y_TWO 44
evanso 82:3211b31e9421 10 #define OFF_SCREEN_X_ONE 56
evanso 82:3211b31e9421 11 #define OFF_SCREEN_X_TWO 15
evanso 82:3211b31e9421 12
evanso 3:dee187b8b30c 13
evanso 82:3211b31e9421 14 const int k_spaceship_sprite_E[SPRITE_Y_LENGTH][SPRITE_X_LENGTH] = {
evanso 3:dee187b8b30c 15 { 1,1,1,1,1,1,0,0,0,0,0,0,0 },
evanso 3:dee187b8b30c 16 { 0,1,1,1,1,1,1,1,1,0,0,0,0 },
evanso 3:dee187b8b30c 17 { 1,1,1,1,1,1,1,1,1,1,1,1,1 },
evanso 3:dee187b8b30c 18 { 0,0,1,1,1,1,1,0,0,0,0,0,0 },
evanso 3:dee187b8b30c 19 };
evanso 5:acd809cc824c 20
evanso 82:3211b31e9421 21 const int k_spaceship_sprite_W[SPRITE_Y_LENGTH][SPRITE_X_LENGTH] = {
evanso 5:acd809cc824c 22 { 0,0,0,0,0,0,0,1,1,1,1,1,1 },
evanso 5:acd809cc824c 23 { 0,0,0,0,1,1,1,1,1,1,1,1,0 },
evanso 5:acd809cc824c 24 { 1,1,1,1,1,1,1,1,1,1,1,1,1 },
evanso 5:acd809cc824c 25 { 0,0,0,0,0,0,1,1,1,1,1,0,0 },
evanso 5:acd809cc824c 26 };
evanso 3:dee187b8b30c 27
evanso 3:dee187b8b30c 28 Spaceship::Spaceship() {
evanso 3:dee187b8b30c 29
evanso 3:dee187b8b30c 30 }
evanso 3:dee187b8b30c 31
evanso 3:dee187b8b30c 32 Spaceship::~Spaceship() {
evanso 3:dee187b8b30c 33
evanso 3:dee187b8b30c 34 }
evanso 3:dee187b8b30c 35
evanso 3:dee187b8b30c 36 void Spaceship::init() {
evanso 36:27aa597db3d2 37 // Assign values to variables
evanso 82:3211b31e9421 38 CheckCollision::sprite_x_length = SPRITE_X_LENGTH;
evanso 82:3211b31e9421 39 CheckCollision::sprite_y_length = SPRITE_Y_LENGTH;
evanso 82:3211b31e9421 40 CheckAlienCollision::sprite_x_length = SPRITE_X_LENGTH;
evanso 82:3211b31e9421 41 CheckAlienCollision::sprite_y_length = SPRITE_Y_LENGTH;
evanso 82:3211b31e9421 42 position_x_ = STARTING_X_POS;
evanso 82:3211b31e9421 43 position_y_ = STARTING_Y_POS;
evanso 13:12276eed13ac 44
evanso 85:87bc28b151d8 45 // Sets original spaceship direction to facing East
evanso 27:8bb2bd97c319 46 direction_ = true;
evanso 3:dee187b8b30c 47 }
evanso 3:dee187b8b30c 48
evanso 3:dee187b8b30c 49 void Spaceship::draw(N5110 &lcd) {
evanso 6:12e8433382b3 50 off_screen_x_y_checker();
evanso 11:ab578a151f67 51
evanso 27:8bb2bd97c319 52 // Draws spaceships at defined x and y positions with different sprite
evanso 85:87bc28b151d8 53 // Direction depending on joystick position
evanso 82:3211b31e9421 54 if (direction_) {
evanso 82:3211b31e9421 55 lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH,
evanso 82:3211b31e9421 56 SPRITE_X_LENGTH,(int*)k_spaceship_sprite_E);
evanso 82:3211b31e9421 57 }else if (!direction_) {
evanso 82:3211b31e9421 58 lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, SPRITE_X_LENGTH,
evanso 82:3211b31e9421 59 (int*)k_spaceship_sprite_W);
evanso 5:acd809cc824c 60 }
evanso 11:ab578a151f67 61
evanso 83:35d1e846eab2 62 // printf("Spaceship Y postion = %d\n",position_y_);
evanso 4:0df2b85e47f1 63 }
evanso 4:0df2b85e47f1 64
evanso 82:3211b31e9421 65 void Spaceship::off_screen_x_y_checker() {
evanso 85:87bc28b151d8 66 // Checks y position of spaceship and then alters y position if off map
evanso 82:3211b31e9421 67 if (position_y_ < OFF_SCREEN_Y_ONE) {
evanso 82:3211b31e9421 68 position_y_ = OFF_SCREEN_Y_ONE;
evanso 82:3211b31e9421 69 }else if (position_y_ > OFF_SCREEN_Y_TWO) {
evanso 82:3211b31e9421 70 position_y_ = OFF_SCREEN_Y_TWO;
evanso 6:12e8433382b3 71 }
evanso 6:12e8433382b3 72
evanso 85:87bc28b151d8 73 // Checks x position of spaceship and alters x position
evanso 82:3211b31e9421 74 if (position_x_ > OFF_SCREEN_X_ONE) {
evanso 82:3211b31e9421 75 position_x_ = OFF_SCREEN_X_ONE;
evanso 82:3211b31e9421 76 }else if (position_x_ < OFF_SCREEN_X_TWO) {
evanso 82:3211b31e9421 77 position_x_ = OFF_SCREEN_X_TWO;
evanso 6:12e8433382b3 78 }
evanso 10:aca793aa7abc 79 }
evanso 10:aca793aa7abc 80
evanso 82:3211b31e9421 81 void Spaceship::movement(Direction d_) {
evanso 85:87bc28b151d8 82 // Switch statement to change spaceship parameters depending on
evanso 82:3211b31e9421 83 // Joystick direction
evanso 13:12276eed13ac 84 switch (d_) {
evanso 11:ab578a151f67 85 case CENTRE: set_spaceship_peram(0, 0, false , false); break;
evanso 13:12276eed13ac 86 case N: set_spaceship_peram(0, -1, false , false); break;
evanso 16:1ee3d3804557 87 case NE: set_spaceship_peram(-1, -1, true, true); break;
evanso 16:1ee3d3804557 88 case E: set_spaceship_peram(-1, 0, true, true); break;
evanso 16:1ee3d3804557 89 case SE: set_spaceship_peram(-1, 1, true, true); break;
evanso 10:aca793aa7abc 90 case S: set_spaceship_peram(0, 1, false, false); break;
evanso 16:1ee3d3804557 91 case SW: set_spaceship_peram(1, 1, true, false); break;
evanso 16:1ee3d3804557 92 case W: set_spaceship_peram(1, 0, true, false); break;
evanso 16:1ee3d3804557 93 case NW: set_spaceship_peram(1, -1, true, false); break;
evanso 10:aca793aa7abc 94 }
evanso 27:8bb2bd97c319 95
evanso 82:3211b31e9421 96 // printf("y= %d\n", position_y_);
evanso 4:0df2b85e47f1 97 }
evanso 10:aca793aa7abc 98
evanso 27:8bb2bd97c319 99 void Spaceship::set_spaceship_peram(int x_change,int y_change,
evanso 82:3211b31e9421 100 bool sprite_change, bool sprite_param) {
evanso 27:8bb2bd97c319 101 position_x_ += x_change;
evanso 27:8bb2bd97c319 102 position_y_ += y_change;
evanso 11:ab578a151f67 103
evanso 85:87bc28b151d8 104 // Only changes sprite direction when joystick direction isn't N or S
evanso 11:ab578a151f67 105 if (sprite_change) {
evanso 27:8bb2bd97c319 106 direction_ = sprite_param;
evanso 11:ab578a151f67 107 }
evanso 11:ab578a151f67 108 }
evanso 11:ab578a151f67 109
evanso 82:3211b31e9421 110 bool Spaceship::get_spaceship_sprite_direction() {
evanso 27:8bb2bd97c319 111 return direction_;
evanso 85:87bc28b151d8 112 }