Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Spaceship/Spaceship.cpp@15:90b6821bcf64, 2020-04-28 (annotated)
- 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?
| User | Revision | Line number | New 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 | 5:acd809cc824c | 43 | //usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_); |
| evanso | 4:0df2b85e47f1 | 44 | } |
| evanso | 4:0df2b85e47f1 | 45 | |
| evanso | 6:12e8433382b3 | 46 | void Spaceship::off_screen_x_y_checker(){ |
| evanso | 11:ab578a151f67 | 47 | // checks y postion spand alters y position |
| evanso | 13:12276eed13ac | 48 | if (position_y_spaceship_ < 0) { |
| evanso | 13:12276eed13ac | 49 | position_y_spaceship_ = 0; |
| evanso | 11:ab578a151f67 | 50 | }else if (position_y_spaceship_ > 44) { |
| evanso | 6:12e8433382b3 | 51 | position_y_spaceship_ = 44; |
| evanso | 6:12e8433382b3 | 52 | } |
| evanso | 6:12e8433382b3 | 53 | |
| evanso | 11:ab578a151f67 | 54 | // checks x postion of spaceship and alters x position |
| evanso | 6:12e8433382b3 | 55 | if (position_x_spaceship_ > 52) { |
| evanso | 6:12e8433382b3 | 56 | position_x_spaceship_ = 52; |
| evanso | 11:ab578a151f67 | 57 | }else if (position_x_spaceship_ < 22) { |
| evanso | 6:12e8433382b3 | 58 | position_x_spaceship_ = 22; |
| evanso | 6:12e8433382b3 | 59 | } |
| evanso | 10:aca793aa7abc | 60 | } |
| evanso | 10:aca793aa7abc | 61 | |
| evanso | 13:12276eed13ac | 62 | void Spaceship::movement(Direction d_){ |
| evanso | 11:ab578a151f67 | 63 | // switch statement to change spaceship parameters depedning on joystick direction |
| evanso | 13:12276eed13ac | 64 | switch (d_) { |
| evanso | 11:ab578a151f67 | 65 | case CENTRE: set_spaceship_peram(0, 0, false , false); break; |
| evanso | 13:12276eed13ac | 66 | case N: set_spaceship_peram(0, -1, false , false); break; |
| evanso | 10:aca793aa7abc | 67 | case NE: set_spaceship_peram(1, -1, true, true); break; |
| evanso | 10:aca793aa7abc | 68 | case E: set_spaceship_peram(1, 0, true, true); break; |
| evanso | 10:aca793aa7abc | 69 | case SE: set_spaceship_peram(1, 1, true, true); break; |
| evanso | 10:aca793aa7abc | 70 | case S: set_spaceship_peram(0, 1, false, false); break; |
| evanso | 10:aca793aa7abc | 71 | case SW: set_spaceship_peram(-1, 1, true, false); break; |
| evanso | 10:aca793aa7abc | 72 | case W: set_spaceship_peram(-1, 0, true, false); break; |
| evanso | 10:aca793aa7abc | 73 | case NW: set_spaceship_peram(-1, -1, true, false); break; |
| evanso | 10:aca793aa7abc | 74 | } |
| evanso | 13:12276eed13ac | 75 | //printf("y= %d\n", position_y_spaceship_); |
| evanso | 4:0df2b85e47f1 | 76 | } |
| evanso | 10:aca793aa7abc | 77 | |
| evanso | 11:ab578a151f67 | 78 | void Spaceship::set_spaceship_peram(int x_change,int y_change, bool sprite_change, bool sprite_param){ |
| evanso | 11:ab578a151f67 | 79 | position_x_spaceship_ += x_change; |
| evanso | 11:ab578a151f67 | 80 | position_y_spaceship_ += y_change; |
| evanso | 11:ab578a151f67 | 81 | |
| evanso | 13:12276eed13ac | 82 | // Only changes sprite direction when jostick direction isn't N or S |
| evanso | 11:ab578a151f67 | 83 | if (sprite_change) { |
| evanso | 11:ab578a151f67 | 84 | spaceship_sprite_direction_ = sprite_param; |
| evanso | 11:ab578a151f67 | 85 | } |
| evanso | 11:ab578a151f67 | 86 | } |
| evanso | 11:ab578a151f67 | 87 | |
| evanso | 7:0af4ced868f5 | 88 | int Spaceship::get_position_x_spaceship(){ |
| evanso | 7:0af4ced868f5 | 89 | return position_x_spaceship_; |
| evanso | 7:0af4ced868f5 | 90 | } |
| evanso | 10:aca793aa7abc | 91 | |
| evanso | 11:ab578a151f67 | 92 | int Spaceship::get_position_y_spaceship(){ |
| evanso | 11:ab578a151f67 | 93 | return position_y_spaceship_; |
| evanso | 10:aca793aa7abc | 94 | } |
| evanso | 11:ab578a151f67 | 95 | |
| evanso | 11:ab578a151f67 | 96 |