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@6:12e8433382b3, 2020-04-16 (annotated)
- Committer:
- evanso
- Date:
- Thu Apr 16 16:25:53 2020 +0000
- Revision:
- 6:12e8433382b3
- Parent:
- 5:acd809cc824c
- Child:
- 7:0af4ced868f5
Restricted spaceship movement to middle third of the screen in the x-direction. Started adding drawing functions to map class.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| evanso | 3:dee187b8b30c | 1 | #include "Spaceship.h" |
| evanso | 5:acd809cc824c | 2 | |
| evanso | 5:acd809cc824c | 3 | //Serial usb(USBTX, USBRX); |
| evanso | 3:dee187b8b30c | 4 | |
| evanso | 5:acd809cc824c | 5 | const int k_spaceship_sprite_E[4][13] = { |
| evanso | 3:dee187b8b30c | 6 | { 1,1,1,1,1,1,0,0,0,0,0,0,0 }, |
| evanso | 3:dee187b8b30c | 7 | { 0,1,1,1,1,1,1,1,1,0,0,0,0 }, |
| evanso | 3:dee187b8b30c | 8 | { 1,1,1,1,1,1,1,1,1,1,1,1,1 }, |
| evanso | 3:dee187b8b30c | 9 | { 0,0,1,1,1,1,1,0,0,0,0,0,0 }, |
| evanso | 3:dee187b8b30c | 10 | }; |
| evanso | 5:acd809cc824c | 11 | |
| evanso | 5:acd809cc824c | 12 | const int k_spaceship_sprite_W[4][13] = { |
| evanso | 5:acd809cc824c | 13 | { 0,0,0,0,0,0,0,1,1,1,1,1,1 }, |
| evanso | 5:acd809cc824c | 14 | { 0,0,0,0,1,1,1,1,1,1,1,1,0 }, |
| evanso | 5:acd809cc824c | 15 | { 1,1,1,1,1,1,1,1,1,1,1,1,1 }, |
| evanso | 5:acd809cc824c | 16 | { 0,0,0,0,0,0,1,1,1,1,1,0,0 }, |
| evanso | 5:acd809cc824c | 17 | }; |
| evanso | 3:dee187b8b30c | 18 | |
| evanso | 3:dee187b8b30c | 19 | Spaceship::Spaceship() { |
| evanso | 3:dee187b8b30c | 20 | |
| evanso | 3:dee187b8b30c | 21 | } |
| evanso | 3:dee187b8b30c | 22 | |
| evanso | 3:dee187b8b30c | 23 | Spaceship::~Spaceship() { |
| evanso | 3:dee187b8b30c | 24 | |
| evanso | 3:dee187b8b30c | 25 | } |
| evanso | 3:dee187b8b30c | 26 | |
| evanso | 3:dee187b8b30c | 27 | void Spaceship::init() { |
| evanso | 5:acd809cc824c | 28 | position_x_spaceship_ = 36; |
| evanso | 5:acd809cc824c | 29 | position_y_spaceship_ = 22; |
| evanso | 5:acd809cc824c | 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 | 5:acd809cc824c | 35 | // Draws spaceships at defined x and y positions with different sprite direction depending on joystick postion |
| evanso | 5:acd809cc824c | 36 | if (spaceship_sprite_direction_){ |
| evanso | 5:acd809cc824c | 37 | lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_E); |
| evanso | 5:acd809cc824c | 38 | }else if (!spaceship_sprite_direction_){ |
| evanso | 5:acd809cc824c | 39 | lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_W); |
| evanso | 5:acd809cc824c | 40 | } |
| evanso | 6:12e8433382b3 | 41 | // printf to find position of spaceship for making the off screen checkers |
| evanso | 5:acd809cc824c | 42 | //usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_); |
| evanso | 4:0df2b85e47f1 | 43 | } |
| evanso | 4:0df2b85e47f1 | 44 | |
| evanso | 4:0df2b85e47f1 | 45 | void Spaceship::clear_spaceship(N5110 &lcd){ |
| evanso | 4:0df2b85e47f1 | 46 | // Clears spaceship from LCD by drawing a white sprite |
| evanso | 4:0df2b85e47f1 | 47 | lcd.drawRect(position_x_spaceship_, position_y_spaceship_, 13, 4, FILL_WHITE); |
| evanso | 3:dee187b8b30c | 48 | } |
| evanso | 3:dee187b8b30c | 49 | |
| evanso | 6:12e8433382b3 | 50 | void Spaceship::off_screen_x_y_checker(){ |
| evanso | 6:12e8433382b3 | 51 | // checks y postion and alters y position |
| evanso | 6:12e8433382b3 | 52 | if (position_y_spaceship_ < 1) { |
| evanso | 6:12e8433382b3 | 53 | position_y_spaceship_ = 1; |
| evanso | 6:12e8433382b3 | 54 | } |
| evanso | 6:12e8433382b3 | 55 | if (position_y_spaceship_ > 44) { |
| evanso | 6:12e8433382b3 | 56 | position_y_spaceship_ = 44; |
| evanso | 6:12e8433382b3 | 57 | } |
| evanso | 6:12e8433382b3 | 58 | |
| evanso | 6:12e8433382b3 | 59 | if (position_x_spaceship_ > 52) { |
| evanso | 6:12e8433382b3 | 60 | position_x_spaceship_ = 52; |
| evanso | 6:12e8433382b3 | 61 | } |
| evanso | 6:12e8433382b3 | 62 | if (position_x_spaceship_ < 22) { |
| evanso | 6:12e8433382b3 | 63 | position_x_spaceship_ = 22; |
| evanso | 6:12e8433382b3 | 64 | } |
| evanso | 6:12e8433382b3 | 65 | } |
| evanso | 5:acd809cc824c | 66 | // NEED TO REDUCE FUNTION LENGTH ONCE WORKING |
| evanso | 4:0df2b85e47f1 | 67 | void Spaceship::movement(Gamepad &pad){ |
| evanso | 4:0df2b85e47f1 | 68 | Direction joystick_direction = pad.get_direction(); |
| evanso | 4:0df2b85e47f1 | 69 | // Changes x and y postion values depending on joystick input |
| evanso | 4:0df2b85e47f1 | 70 | if(joystick_direction == N){ |
| evanso | 4:0df2b85e47f1 | 71 | position_y_spaceship_+= -1; |
| evanso | 5:acd809cc824c | 72 | }else if(joystick_direction == NE){ |
| evanso | 6:12e8433382b3 | 73 | position_x_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 74 | position_y_spaceship_+= -1; |
| evanso | 5:acd809cc824c | 75 | spaceship_sprite_direction_ = true; |
| evanso | 4:0df2b85e47f1 | 76 | }else if(joystick_direction == E){ |
| evanso | 4:0df2b85e47f1 | 77 | position_x_spaceship_+= 2; |
| evanso | 5:acd809cc824c | 78 | spaceship_sprite_direction_ = true; |
| evanso | 5:acd809cc824c | 79 | }else if(joystick_direction == SE){ |
| evanso | 6:12e8433382b3 | 80 | position_x_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 81 | position_y_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 82 | spaceship_sprite_direction_ = true; |
| evanso | 5:acd809cc824c | 83 | }else if(joystick_direction == S){ |
| evanso | 5:acd809cc824c | 84 | position_y_spaceship_+= 1; |
| evanso | 5:acd809cc824c | 85 | }else if(joystick_direction == SW){ |
| evanso | 5:acd809cc824c | 86 | position_y_spaceship_+= 1; |
| evanso | 6:12e8433382b3 | 87 | position_x_spaceship_-= 1; |
| evanso | 5:acd809cc824c | 88 | spaceship_sprite_direction_ = false; |
| evanso | 4:0df2b85e47f1 | 89 | }else if(joystick_direction == W){ |
| evanso | 4:0df2b85e47f1 | 90 | position_x_spaceship_-= 2; |
| evanso | 5:acd809cc824c | 91 | spaceship_sprite_direction_ = false; |
| evanso | 5:acd809cc824c | 92 | }else if(joystick_direction == NW){ |
| evanso | 6:12e8433382b3 | 93 | position_x_spaceship_-= 1; |
| evanso | 5:acd809cc824c | 94 | position_y_spaceship_+= -1; |
| evanso | 5:acd809cc824c | 95 | spaceship_sprite_direction_ = false; |
| evanso | 6:12e8433382b3 | 96 | } |
| evanso | 4:0df2b85e47f1 | 97 | } |
| evanso | 4:0df2b85e47f1 | 98 |