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@9:241a1a7d8527, 2019-04-09 (annotated)
- Committer:
- ikenna1
- Date:
- Tue Apr 09 05:14:07 2019 +0000
- Revision:
- 9:241a1a7d8527
- Parent:
- 6:8473dacbeb65
- Child:
- 11:73cd744ffa80
Had to work on this in another folder as this one got corrupted for some reason but it works once the name is changed so will work on it with name changed. New features include Menu class and Weapons class
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 | 6:8473dacbeb65 | 11 | // data needed for first ship: basic |
ikenna1 | 5:bb6edc5b5be3 | 12 | const int basic[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 | 6:8473dacbeb65 | 20 | /* |
ikenna1 | 5:bb6edc5b5be3 | 21 | int basic_width = 9; |
ikenna1 | 5:bb6edc5b5be3 | 22 | int basic_height = 6; |
ikenna1 | 5:bb6edc5b5be3 | 23 | int basic_speed = 5; |
ikenna1 | 5:bb6edc5b5be3 | 24 | */ |
ikenna1 | 5:bb6edc5b5be3 | 25 | |
ikenna1 | 5:bb6edc5b5be3 | 26 | // Set basic ship to be default |
ikenna1 | 9:241a1a7d8527 | 27 | void Ship::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos) |
ikenna1 | 5:bb6edc5b5be3 | 28 | { |
ikenna1 | 5:bb6edc5b5be3 | 29 | _ship_width = ship_width; |
ikenna1 | 5:bb6edc5b5be3 | 30 | _ship_height = ship_height; |
ikenna1 | 9:241a1a7d8527 | 31 | _ship_speed = ship_speed; |
ikenna1 | 5:bb6edc5b5be3 | 32 | _ship_xpos = ship_xpos; |
ikenna1 | 5:bb6edc5b5be3 | 33 | _ship_xpos = ship_ypos; |
ikenna1 | 6:8473dacbeb65 | 34 | // _ship_shape[_ship_height][_ship_width] = ship_shape; |
ikenna1 | 5:bb6edc5b5be3 | 35 | } |
ikenna1 | 5:bb6edc5b5be3 | 36 | // Draw the ship ***Note: figure out how to change ship type e.g from basic to devotion |
ikenna1 | 6:8473dacbeb65 | 37 | void Ship::draw_ship(N5110 &lcd) |
ikenna1 | 5:bb6edc5b5be3 | 38 | { |
ikenna1 | 5:bb6edc5b5be3 | 39 | lcd.drawSprite(_ship_xpos,_ship_ypos,6,9,(int *)basic); |
ikenna1 | 5:bb6edc5b5be3 | 40 | } |
ikenna1 | 5:bb6edc5b5be3 | 41 | |
ikenna1 | 9:241a1a7d8527 | 42 | void Ship::update_ship(float x_joystick,float y_joystick) |
ikenna1 | 9:241a1a7d8527 | 43 | { // Only change position if joystick is reasonably moved |
ikenna1 | 5:bb6edc5b5be3 | 44 | if(-0.25 > x_joystick || x_joystick > 0.25 || -0.25 > y_joystick || y_joystick > 0.25) { |
ikenna1 | 9:241a1a7d8527 | 45 | |
ikenna1 | 9:241a1a7d8527 | 46 | // Update positions using joystick and the intended ships speed |
ikenna1 | 9:241a1a7d8527 | 47 | _ship_xpos = _ship_xpos + (x_joystick*_ship_speed); |
ikenna1 | 9:241a1a7d8527 | 48 | _ship_ypos = _ship_ypos - (y_joystick*_ship_speed); |
ikenna1 | 9:241a1a7d8527 | 49 | |
ikenna1 | 5:bb6edc5b5be3 | 50 | // Dont let sprite move out of screen |
ikenna1 | 9:241a1a7d8527 | 51 | if(_ship_xpos < 0) { |
ikenna1 | 9:241a1a7d8527 | 52 | _ship_xpos = 0; |
ikenna1 | 9:241a1a7d8527 | 53 | } |
ikenna1 | 9:241a1a7d8527 | 54 | if(_ship_xpos > (84 - (_ship_width))) { |
ikenna1 | 9:241a1a7d8527 | 55 | _ship_xpos = 84 - _ship_width; |
ikenna1 | 9:241a1a7d8527 | 56 | } |
ikenna1 | 9:241a1a7d8527 | 57 | if(_ship_ypos < 0) { |
ikenna1 | 9:241a1a7d8527 | 58 | _ship_ypos = 0; |
ikenna1 | 9:241a1a7d8527 | 59 | } |
ikenna1 | 9:241a1a7d8527 | 60 | if(_ship_ypos > (48 - (_ship_height))) { |
ikenna1 | 9:241a1a7d8527 | 61 | _ship_ypos = 48 - _ship_height; |
ikenna1 | 5:bb6edc5b5be3 | 62 | } |
ikenna1 | 5:bb6edc5b5be3 | 63 | |
ikenna1 | 5:bb6edc5b5be3 | 64 | } |
ikenna1 | 9:241a1a7d8527 | 65 | // printf("y_joysticzk = %f\n",y_joystick); |
ikenna1 | 9:241a1a7d8527 | 66 | // printf("x_joystick = %f\n",x_joystick); |
ikenna1 | 5:bb6edc5b5be3 | 67 | } |
ikenna1 | 5:bb6edc5b5be3 | 68 | // returns the current ship position on screen as a vector |
ikenna1 | 6:8473dacbeb65 | 69 | Vector2D Ship::get_pos() |
ikenna1 | 5:bb6edc5b5be3 | 70 | { |
ikenna1 | 5:bb6edc5b5be3 | 71 | Vector2D ship_pos = {_ship_xpos,_ship_ypos}; |
ikenna1 | 5:bb6edc5b5be3 | 72 | return ship_pos; |
ikenna1 | 5:bb6edc5b5be3 | 73 | } |
ikenna1 | 5:bb6edc5b5be3 | 74 | |
ikenna1 | 5:bb6edc5b5be3 | 75 | /****Note: if you make all the ships the same size, you could then creare a enum for them ship.[ enum class Ship {basic, devotion}; ] |
ikenna1 | 5:bb6edc5b5be3 | 76 | 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 | 77 | the ship is correct and create an if function to draw the appropriate ship |
ikenna1 | 6:8473dacbeb65 | 78 | */ |