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.
Dependencies: mbed
Ship/Ship.cpp@31:c7bd3ed16840, 2019-04-20 (annotated)
- Committer:
- ikenna1
- Date:
- Sat Apr 20 03:52:22 2019 +0000
- Revision:
- 31:c7bd3ed16840
- Parent:
- 26:a53d41adf40b
- Child:
- 42:ee13e1d103d8
- Child:
- 43:500b8cff3715
Added more complex shooter movement
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ikenna1 | 5:bb6edc5b5be3 | 1 | #include "Ship.h" |
ikenna1 | 5:bb6edc5b5be3 | 2 | Ship::Ship() |
ikenna1 | 5:bb6edc5b5be3 | 3 | { |
ikenna1 | 5:bb6edc5b5be3 | 4 | |
ikenna1 | 5:bb6edc5b5be3 | 5 | } |
ikenna1 | 5:bb6edc5b5be3 | 6 | |
ikenna1 | 5:bb6edc5b5be3 | 7 | Ship::~Ship() |
ikenna1 | 5:bb6edc5b5be3 | 8 | { |
ikenna1 | 5:bb6edc5b5be3 | 9 | |
ikenna1 | 5:bb6edc5b5be3 | 10 | } |
ikenna1 | 26:a53d41adf40b | 11 | // data needed for first ship: kestrel |
ikenna1 | 26:a53d41adf40b | 12 | const int kestrel[6][9] = { |
ikenna1 | 5:bb6edc5b5be3 | 13 | { 0,0,0,0,1,0,0,0,0 }, |
ikenna1 | 5:bb6edc5b5be3 | 14 | { 0,0,0,1,1,1,0,0,0 }, |
ikenna1 | 5:bb6edc5b5be3 | 15 | { 0,0,1,1,0,1,1,0,0 }, |
ikenna1 | 5:bb6edc5b5be3 | 16 | { 0,0,1,1,0,1,1,0,0 }, |
ikenna1 | 5:bb6edc5b5be3 | 17 | { 1,0,1,1,0,1,1,0,1 }, |
ikenna1 | 5:bb6edc5b5be3 | 18 | { 0,1,1,1,1,1,1,1,0 }, |
ikenna1 | 5:bb6edc5b5be3 | 19 | }; |
ikenna1 | 11:73cd744ffa80 | 20 | const int imperion[10][7] = { |
ikenna1 | 11:73cd744ffa80 | 21 | {0,1,0,0,0,1,0}, |
ikenna1 | 11:73cd744ffa80 | 22 | {0,1,0,0,0,1,0}, |
ikenna1 | 11:73cd744ffa80 | 23 | {1,1,0,0,0,1,1}, |
ikenna1 | 11:73cd744ffa80 | 24 | {0,1,0,0,0,1,0}, |
ikenna1 | 11:73cd744ffa80 | 25 | {0,1,0,0,0,1,0}, |
ikenna1 | 11:73cd744ffa80 | 26 | {0,1,0,0,0,1,0}, |
ikenna1 | 11:73cd744ffa80 | 27 | {0,1,1,0,1,1,0}, |
ikenna1 | 11:73cd744ffa80 | 28 | {1,0,0,1,0,0,1}, |
ikenna1 | 11:73cd744ffa80 | 29 | {0,0,1,0,1,0,0}, |
ikenna1 | 26:a53d41adf40b | 30 | {0,0,0,1,0,0,0}, |
ikenna1 | 26:a53d41adf40b | 31 | }; |
ikenna1 | 26:a53d41adf40b | 32 | const int thor[10][7] = { |
ikenna1 | 26:a53d41adf40b | 33 | {0,0,0,1,0,0,0}, |
ikenna1 | 26:a53d41adf40b | 34 | {0,0,1,0,1,0,0}, |
ikenna1 | 26:a53d41adf40b | 35 | {0,1,0,0,0,1,0}, |
ikenna1 | 26:a53d41adf40b | 36 | {1,0,0,1,0,0,1}, |
ikenna1 | 26:a53d41adf40b | 37 | {0,1,0,0,0,1,0}, |
ikenna1 | 26:a53d41adf40b | 38 | {0,0,1,0,1,0,0}, |
ikenna1 | 26:a53d41adf40b | 39 | {1,0,0,1,0,0,1}, |
ikenna1 | 26:a53d41adf40b | 40 | {1,0,1,0,1,0,1}, |
ikenna1 | 26:a53d41adf40b | 41 | {1,1,0,0,0,1,1}, |
ikenna1 | 26:a53d41adf40b | 42 | {1,0,0,0,0,0,1}, |
ikenna1 | 26:a53d41adf40b | 43 | |
ikenna1 | 11:73cd744ffa80 | 44 | }; |
ikenna1 | 6:8473dacbeb65 | 45 | /* |
ikenna1 | 26:a53d41adf40b | 46 | int kestrel_width = 9; |
ikenna1 | 26:a53d41adf40b | 47 | int kestrel_height = 6; |
ikenna1 | 26:a53d41adf40b | 48 | int kestrel_speed = 5; |
ikenna1 | 5:bb6edc5b5be3 | 49 | */ |
ikenna1 | 5:bb6edc5b5be3 | 50 | |
ikenna1 | 26:a53d41adf40b | 51 | // Set kestrel ship to be default |
ikenna1 | 9:241a1a7d8527 | 52 | void Ship::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos) |
ikenna1 | 5:bb6edc5b5be3 | 53 | { |
ikenna1 | 5:bb6edc5b5be3 | 54 | _ship_width = ship_width; |
ikenna1 | 5:bb6edc5b5be3 | 55 | _ship_height = ship_height; |
ikenna1 | 9:241a1a7d8527 | 56 | _ship_speed = ship_speed; |
ikenna1 | 5:bb6edc5b5be3 | 57 | _ship_xpos = ship_xpos; |
ikenna1 | 5:bb6edc5b5be3 | 58 | _ship_xpos = ship_ypos; |
ikenna1 | 6:8473dacbeb65 | 59 | // _ship_shape[_ship_height][_ship_width] = ship_shape; |
ikenna1 | 5:bb6edc5b5be3 | 60 | } |
ikenna1 | 26:a53d41adf40b | 61 | // Draw the ship ***Note: figure out how to change ship type e.g from kestrel to devotion |
ikenna1 | 26:a53d41adf40b | 62 | void Ship::draw_ship(N5110 &lcd, int shipno) |
ikenna1 | 5:bb6edc5b5be3 | 63 | { |
ikenna1 | 26:a53d41adf40b | 64 | if(shipno == 0) { |
ikenna1 | 26:a53d41adf40b | 65 | lcd.drawSprite(_ship_xpos,_ship_ypos,6,9,(int *)kestrel); |
ikenna1 | 26:a53d41adf40b | 66 | } |
ikenna1 | 26:a53d41adf40b | 67 | if(shipno == 1) { |
ikenna1 | 26:a53d41adf40b | 68 | lcd.drawSprite(_ship_xpos,_ship_ypos,10,7,(int *)imperion); |
ikenna1 | 26:a53d41adf40b | 69 | } |
ikenna1 | 26:a53d41adf40b | 70 | if(shipno == 2) { |
ikenna1 | 26:a53d41adf40b | 71 | lcd.drawSprite(_ship_xpos,_ship_ypos,10,7,(int *)thor); |
ikenna1 | 26:a53d41adf40b | 72 | } |
ikenna1 | 11:73cd744ffa80 | 73 | } |
ikenna1 | 5:bb6edc5b5be3 | 74 | |
ikenna1 | 9:241a1a7d8527 | 75 | void Ship::update_ship(float x_joystick,float y_joystick) |
ikenna1 | 26:a53d41adf40b | 76 | { |
ikenna1 | 31:c7bd3ed16840 | 77 | int border = 1; |
ikenna1 | 26:a53d41adf40b | 78 | // Only change position if joystick is reasonably moved |
ikenna1 | 5:bb6edc5b5be3 | 79 | if(-0.25 > x_joystick || x_joystick > 0.25 || -0.25 > y_joystick || y_joystick > 0.25) { |
ikenna1 | 9:241a1a7d8527 | 80 | // Update positions using joystick and the intended ships speed |
ikenna1 | 9:241a1a7d8527 | 81 | _ship_xpos = _ship_xpos + (x_joystick*_ship_speed); |
ikenna1 | 9:241a1a7d8527 | 82 | _ship_ypos = _ship_ypos - (y_joystick*_ship_speed); |
ikenna1 | 9:241a1a7d8527 | 83 | |
ikenna1 | 5:bb6edc5b5be3 | 84 | // Dont let sprite move out of screen |
ikenna1 | 31:c7bd3ed16840 | 85 | if(_ship_xpos < border) { |
ikenna1 | 31:c7bd3ed16840 | 86 | _ship_xpos = border; |
ikenna1 | 9:241a1a7d8527 | 87 | } |
ikenna1 | 31:c7bd3ed16840 | 88 | if(_ship_xpos > (84 - (_ship_width)- 7)) { |
ikenna1 | 31:c7bd3ed16840 | 89 | _ship_xpos = 84 - _ship_width - 7; |
ikenna1 | 9:241a1a7d8527 | 90 | } |
ikenna1 | 31:c7bd3ed16840 | 91 | if(_ship_ypos < border) { |
ikenna1 | 31:c7bd3ed16840 | 92 | _ship_ypos = border; |
ikenna1 | 9:241a1a7d8527 | 93 | } |
ikenna1 | 31:c7bd3ed16840 | 94 | if(_ship_ypos > (48 - (_ship_height) - border)) { |
ikenna1 | 31:c7bd3ed16840 | 95 | _ship_ypos = 48 - _ship_height - border; |
ikenna1 | 5:bb6edc5b5be3 | 96 | } |
ikenna1 | 5:bb6edc5b5be3 | 97 | } |
ikenna1 | 9:241a1a7d8527 | 98 | // printf("y_joysticzk = %f\n",y_joystick); |
ikenna1 | 9:241a1a7d8527 | 99 | // printf("x_joystick = %f\n",x_joystick); |
ikenna1 | 5:bb6edc5b5be3 | 100 | } |
ikenna1 | 5:bb6edc5b5be3 | 101 | // returns the current ship position on screen as a vector |
ikenna1 | 6:8473dacbeb65 | 102 | Vector2D Ship::get_pos() |
ikenna1 | 5:bb6edc5b5be3 | 103 | { |
ikenna1 | 5:bb6edc5b5be3 | 104 | Vector2D ship_pos = {_ship_xpos,_ship_ypos}; |
ikenna1 | 5:bb6edc5b5be3 | 105 | return ship_pos; |
ikenna1 | 5:bb6edc5b5be3 | 106 | } |
ikenna1 | 5:bb6edc5b5be3 | 107 | |
ikenna1 | 14:88ca5b1a111a | 108 | void Ship::set_dimensions(int ship_width, int ship_height) |
ikenna1 | 11:73cd744ffa80 | 109 | { |
ikenna1 | 11:73cd744ffa80 | 110 | _ship_width = ship_width; |
ikenna1 | 11:73cd744ffa80 | 111 | _ship_height = ship_height; |
ikenna1 | 11:73cd744ffa80 | 112 | } |
ikenna1 | 11:73cd744ffa80 | 113 | |
ikenna1 | 26:a53d41adf40b | 114 | /****Note: if you make all the ships the same size, you could then creare a enum for them ship.[ enum class Ship {kestrel, devotion}; ] |
ikenna1 | 5:bb6edc5b5be3 | 115 | then, you can declare a new variable belonging to the enum Ship, ship. then to set a ships sprite simply use the variable to test if |
ikenna1 | 6:8473dacbeb65 | 116 | the ship is correct and create an if function to draw the appropriate ship |
ikenna1 | 6:8473dacbeb65 | 117 | */ |