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@10:aca793aa7abc, 2020-04-22 (annotated)
- Committer:
- evanso
- Date:
- Wed Apr 22 20:02:22 2020 +0000
- Revision:
- 10:aca793aa7abc
- Parent:
- 9:1ddb8dc93e48
- Child:
- 11:ab578a151f67
Reduced length of spaceship movement function using a switch statement. In the previous commit, I changed the draw line function in the LCD class; I changed its unassigned int variables to normal int variables as it was crashing the game.
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 | 5:acd809cc824c | 28 | spaceship_sprite_direction_ = true; |
| evanso | 3:dee187b8b30c | 29 | } |
| evanso | 3:dee187b8b30c | 30 | |
| evanso | 3:dee187b8b30c | 31 | void Spaceship::draw(N5110 &lcd) { |
| evanso | 6:12e8433382b3 | 32 | off_screen_x_y_checker(); |
| evanso | 5:acd809cc824c | 33 | // Draws spaceships at defined x and y positions with different sprite direction depending on joystick postion |
| evanso | 5:acd809cc824c | 34 | if (spaceship_sprite_direction_){ |
| evanso | 5:acd809cc824c | 35 | lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_E); |
| evanso | 5:acd809cc824c | 36 | }else if (!spaceship_sprite_direction_){ |
| evanso | 5:acd809cc824c | 37 | lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_W); |
| evanso | 5:acd809cc824c | 38 | } |
| evanso | 6:12e8433382b3 | 39 | // printf to find position of spaceship for making the off screen checkers |
| evanso | 5:acd809cc824c | 40 | //usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_); |
| evanso | 4:0df2b85e47f1 | 41 | } |
| evanso | 4:0df2b85e47f1 | 42 | |
| evanso | 4:0df2b85e47f1 | 43 | void Spaceship::clear_spaceship(N5110 &lcd){ |
| evanso | 4:0df2b85e47f1 | 44 | // Clears spaceship from LCD by drawing a white sprite |
| evanso | 4:0df2b85e47f1 | 45 | lcd.drawRect(position_x_spaceship_, position_y_spaceship_, 13, 4, FILL_WHITE); |
| evanso | 3:dee187b8b30c | 46 | } |
| evanso | 3:dee187b8b30c | 47 | |
| evanso | 6:12e8433382b3 | 48 | void Spaceship::off_screen_x_y_checker(){ |
| evanso | 6:12e8433382b3 | 49 | // checks y postion and alters y position |
| evanso | 6:12e8433382b3 | 50 | if (position_y_spaceship_ < 1) { |
| evanso | 6:12e8433382b3 | 51 | position_y_spaceship_ = 1; |
| evanso | 6:12e8433382b3 | 52 | } |
| evanso | 6:12e8433382b3 | 53 | if (position_y_spaceship_ > 44) { |
| evanso | 6:12e8433382b3 | 54 | position_y_spaceship_ = 44; |
| evanso | 6:12e8433382b3 | 55 | } |
| evanso | 6:12e8433382b3 | 56 | |
| evanso | 6:12e8433382b3 | 57 | if (position_x_spaceship_ > 52) { |
| evanso | 6:12e8433382b3 | 58 | position_x_spaceship_ = 52; |
| evanso | 6:12e8433382b3 | 59 | } |
| evanso | 6:12e8433382b3 | 60 | if (position_x_spaceship_ < 22) { |
| evanso | 6:12e8433382b3 | 61 | position_x_spaceship_ = 22; |
| evanso | 6:12e8433382b3 | 62 | } |
| evanso | 10:aca793aa7abc | 63 | } |
| evanso | 10:aca793aa7abc | 64 | |
| evanso | 4:0df2b85e47f1 | 65 | void Spaceship::movement(Gamepad &pad){ |
| evanso | 4:0df2b85e47f1 | 66 | Direction joystick_direction = pad.get_direction(); |
| evanso | 10:aca793aa7abc | 67 | switch (joystick_direction) { |
| evanso | 10:aca793aa7abc | 68 | case N: set_spaceship_peram(-1, 0, false , false); break; |
| evanso | 10:aca793aa7abc | 69 | case NE: set_spaceship_peram(1, -1, true, true); break; |
| evanso | 10:aca793aa7abc | 70 | case E: set_spaceship_peram(1, 0, true, true); break; |
| evanso | 10:aca793aa7abc | 71 | case SE: set_spaceship_peram(1, 1, true, true); break; |
| evanso | 10:aca793aa7abc | 72 | case S: set_spaceship_peram(0, 1, false, false); break; |
| evanso | 10:aca793aa7abc | 73 | case SW: set_spaceship_peram(-1, 1, true, false); break; |
| evanso | 10:aca793aa7abc | 74 | case W: set_spaceship_peram(-1, 0, true, false); break; |
| evanso | 10:aca793aa7abc | 75 | case NW: set_spaceship_peram(-1, -1, true, false); break; |
| evanso | 10:aca793aa7abc | 76 | } |
| evanso | 4:0df2b85e47f1 | 77 | } |
| evanso | 10:aca793aa7abc | 78 | |
| evanso | 7:0af4ced868f5 | 79 | int Spaceship::get_position_x_spaceship(){ |
| evanso | 7:0af4ced868f5 | 80 | return position_x_spaceship_; |
| evanso | 7:0af4ced868f5 | 81 | } |
| evanso | 10:aca793aa7abc | 82 | |
| evanso | 10:aca793aa7abc | 83 | void Spaceship::set_spaceship_peram(int x_change,int y_change, bool sprite_change, bool sprite_param){ |
| evanso | 10:aca793aa7abc | 84 | position_x_spaceship_ += x_change; |
| evanso | 10:aca793aa7abc | 85 | position_y_spaceship_ += y_change; |
| evanso | 10:aca793aa7abc | 86 | if (sprite_change) { |
| evanso | 10:aca793aa7abc | 87 | spaceship_sprite_direction_ = sprite_param; |
| evanso | 10:aca793aa7abc | 88 | } |
| evanso | 10:aca793aa7abc | 89 | } |