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@7:0af4ced868f5, 2020-04-18 (annotated)
- Committer:
- evanso
- Date:
- Sat Apr 18 16:36:52 2020 +0000
- Revision:
- 7:0af4ced868f5
- Parent:
- 6:12e8433382b3
- Child:
- 9:1ddb8dc93e48
A random map now is drawn on the screen and the map moves with joystick input making it look like the spaceship is moving through the map.
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 | 6:12e8433382b3 | 63 | } |
| evanso | 5:acd809cc824c | 64 | // NEED TO REDUCE FUNTION LENGTH ONCE WORKING |
| evanso | 4:0df2b85e47f1 | 65 | void Spaceship::movement(Gamepad &pad){ |
| evanso | 4:0df2b85e47f1 | 66 | Direction joystick_direction = pad.get_direction(); |
| evanso | 4:0df2b85e47f1 | 67 | // Changes x and y postion values depending on joystick input |
| evanso | 4:0df2b85e47f1 | 68 | if(joystick_direction == N){ |
| evanso | 4:0df2b85e47f1 | 69 | position_y_spaceship_+= -1; |
| evanso | 5:acd809cc824c | 70 | }else if(joystick_direction == NE){ |
| evanso | 6:12e8433382b3 | 71 | position_x_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 72 | position_y_spaceship_+= -1; |
| evanso | 5:acd809cc824c | 73 | spaceship_sprite_direction_ = true; |
| evanso | 4:0df2b85e47f1 | 74 | }else if(joystick_direction == E){ |
| evanso | 4:0df2b85e47f1 | 75 | position_x_spaceship_+= 2; |
| evanso | 5:acd809cc824c | 76 | spaceship_sprite_direction_ = true; |
| evanso | 5:acd809cc824c | 77 | }else if(joystick_direction == SE){ |
| evanso | 6:12e8433382b3 | 78 | position_x_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 79 | position_y_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 80 | spaceship_sprite_direction_ = true; |
| evanso | 5:acd809cc824c | 81 | }else if(joystick_direction == S){ |
| evanso | 5:acd809cc824c | 82 | position_y_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 83 | }else if(joystick_direction == SW){ |
| evanso | 5:acd809cc824c | 84 | position_y_spaceship_+= 1; |
| evanso | 6:12e8433382b3 | 85 | position_x_spaceship_-= 1; |
| evanso | 5:acd809cc824c | 86 | spaceship_sprite_direction_ = false; |
| evanso | 4:0df2b85e47f1 | 87 | }else if(joystick_direction == W){ |
| evanso | 4:0df2b85e47f1 | 88 | position_x_spaceship_-= 2; |
| evanso | 5:acd809cc824c | 89 | spaceship_sprite_direction_ = false; |
| evanso | 5:acd809cc824c | 90 | }else if(joystick_direction == NW){ |
| evanso | 6:12e8433382b3 | 91 | position_x_spaceship_-= 1; |
| evanso | 5:acd809cc824c | 92 | position_y_spaceship_+= -1; |
| evanso | 5:acd809cc824c | 93 | spaceship_sprite_direction_ = false; |
| evanso | 6:12e8433382b3 | 94 | } |
| evanso | 4:0df2b85e47f1 | 95 | } |
| evanso | 7:0af4ced868f5 | 96 | int Spaceship::get_position_x_spaceship(){ |
| evanso | 7:0af4ced868f5 | 97 | return position_x_spaceship_; |
| evanso | 7:0af4ced868f5 | 98 | } |