Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Sun Apr 26 17:08:10 2020 +0000
Revision:
13:12276eed13ac
Parent:
11:ab578a151f67
Child:
15:90b6821bcf64
Changed spaceship unit test code so it now done properly and doesn't require my input. Changed where objects are defined to reduce the number of arguments that need passing when a function is called. Edited gamepad code to properly add ADC pin.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 3:dee187b8b30c 1 #include "Spaceship.h"
evanso 3:dee187b8b30c 2
evanso 5:acd809cc824c 3 const int k_spaceship_sprite_E[4][13] = {
evanso 3:dee187b8b30c 4 { 1,1,1,1,1,1,0,0,0,0,0,0,0 },
evanso 3:dee187b8b30c 5 { 0,1,1,1,1,1,1,1,1,0,0,0,0 },
evanso 3:dee187b8b30c 6 { 1,1,1,1,1,1,1,1,1,1,1,1,1 },
evanso 3:dee187b8b30c 7 { 0,0,1,1,1,1,1,0,0,0,0,0,0 },
evanso 3:dee187b8b30c 8 };
evanso 5:acd809cc824c 9
evanso 5:acd809cc824c 10 const int k_spaceship_sprite_W[4][13] = {
evanso 5:acd809cc824c 11 { 0,0,0,0,0,0,0,1,1,1,1,1,1 },
evanso 5:acd809cc824c 12 { 0,0,0,0,1,1,1,1,1,1,1,1,0 },
evanso 5:acd809cc824c 13 { 1,1,1,1,1,1,1,1,1,1,1,1,1 },
evanso 5:acd809cc824c 14 { 0,0,0,0,0,0,1,1,1,1,1,0,0 },
evanso 5:acd809cc824c 15 };
evanso 3:dee187b8b30c 16
evanso 3:dee187b8b30c 17 Spaceship::Spaceship() {
evanso 3:dee187b8b30c 18
evanso 3:dee187b8b30c 19 }
evanso 3:dee187b8b30c 20
evanso 3:dee187b8b30c 21 Spaceship::~Spaceship() {
evanso 3:dee187b8b30c 22
evanso 3:dee187b8b30c 23 }
evanso 3:dee187b8b30c 24
evanso 3:dee187b8b30c 25 void Spaceship::init() {
evanso 5:acd809cc824c 26 position_x_spaceship_ = 36;
evanso 5:acd809cc824c 27 position_y_spaceship_ = 22;
evanso 13:12276eed13ac 28
evanso 13:12276eed13ac 29 // sets origonal spaceship direction to facing East
evanso 13:12276eed13ac 30 spaceship_sprite_direction_ = true;
evanso 3:dee187b8b30c 31 }
evanso 3:dee187b8b30c 32
evanso 3:dee187b8b30c 33 void Spaceship::draw(N5110 &lcd) {
evanso 6:12e8433382b3 34 off_screen_x_y_checker();
evanso 11:ab578a151f67 35
evanso 5:acd809cc824c 36 // Draws spaceships at defined x and y positions with different sprite direction depending on joystick postion
evanso 5:acd809cc824c 37 if (spaceship_sprite_direction_){
evanso 5:acd809cc824c 38 lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_E);
evanso 5:acd809cc824c 39 }else if (!spaceship_sprite_direction_){
evanso 5:acd809cc824c 40 lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_W);
evanso 5:acd809cc824c 41 }
evanso 11:ab578a151f67 42
evanso 6:12e8433382b3 43 // printf to find position of spaceship for making the off screen checkers
evanso 5:acd809cc824c 44 //usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_);
evanso 4:0df2b85e47f1 45 }
evanso 4:0df2b85e47f1 46
evanso 6:12e8433382b3 47 void Spaceship::off_screen_x_y_checker(){
evanso 11:ab578a151f67 48 // checks y postion spand alters y position
evanso 13:12276eed13ac 49 if (position_y_spaceship_ < 0) {
evanso 13:12276eed13ac 50 position_y_spaceship_ = 0;
evanso 11:ab578a151f67 51 }else if (position_y_spaceship_ > 44) {
evanso 6:12e8433382b3 52 position_y_spaceship_ = 44;
evanso 6:12e8433382b3 53 }
evanso 6:12e8433382b3 54
evanso 11:ab578a151f67 55 // checks x postion of spaceship and alters x position
evanso 6:12e8433382b3 56 if (position_x_spaceship_ > 52) {
evanso 6:12e8433382b3 57 position_x_spaceship_ = 52;
evanso 11:ab578a151f67 58 }else if (position_x_spaceship_ < 22) {
evanso 6:12e8433382b3 59 position_x_spaceship_ = 22;
evanso 6:12e8433382b3 60 }
evanso 10:aca793aa7abc 61 }
evanso 10:aca793aa7abc 62
evanso 13:12276eed13ac 63 void Spaceship::movement(Direction d_){
evanso 11:ab578a151f67 64 // switch statement to change spaceship parameters depedning on joystick direction
evanso 13:12276eed13ac 65 switch (d_) {
evanso 11:ab578a151f67 66 case CENTRE: set_spaceship_peram(0, 0, false , false); break;
evanso 13:12276eed13ac 67 case N: set_spaceship_peram(0, -1, false , false); break;
evanso 10:aca793aa7abc 68 case NE: set_spaceship_peram(1, -1, true, true); break;
evanso 10:aca793aa7abc 69 case E: set_spaceship_peram(1, 0, true, true); break;
evanso 10:aca793aa7abc 70 case SE: set_spaceship_peram(1, 1, true, true); break;
evanso 10:aca793aa7abc 71 case S: set_spaceship_peram(0, 1, false, false); break;
evanso 10:aca793aa7abc 72 case SW: set_spaceship_peram(-1, 1, true, false); break;
evanso 10:aca793aa7abc 73 case W: set_spaceship_peram(-1, 0, true, false); break;
evanso 10:aca793aa7abc 74 case NW: set_spaceship_peram(-1, -1, true, false); break;
evanso 10:aca793aa7abc 75 }
evanso 13:12276eed13ac 76 //printf("y= %d\n", position_y_spaceship_);
evanso 4:0df2b85e47f1 77 }
evanso 10:aca793aa7abc 78
evanso 11:ab578a151f67 79 void Spaceship::set_spaceship_peram(int x_change,int y_change, bool sprite_change, bool sprite_param){
evanso 11:ab578a151f67 80 position_x_spaceship_ += x_change;
evanso 11:ab578a151f67 81 position_y_spaceship_ += y_change;
evanso 11:ab578a151f67 82
evanso 13:12276eed13ac 83 // Only changes sprite direction when jostick direction isn't N or S
evanso 11:ab578a151f67 84 if (sprite_change) {
evanso 11:ab578a151f67 85 spaceship_sprite_direction_ = sprite_param;
evanso 11:ab578a151f67 86 }
evanso 11:ab578a151f67 87 }
evanso 11:ab578a151f67 88
evanso 7:0af4ced868f5 89 int Spaceship::get_position_x_spaceship(){
evanso 7:0af4ced868f5 90 return position_x_spaceship_;
evanso 7:0af4ced868f5 91 }
evanso 10:aca793aa7abc 92
evanso 11:ab578a151f67 93 int Spaceship::get_position_y_spaceship(){
evanso 11:ab578a151f67 94 return position_y_spaceship_;
evanso 10:aca793aa7abc 95 }
evanso 11:ab578a151f67 96
evanso 11:ab578a151f67 97