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@24:ff5af5a013b5, 2020-05-14 (annotated)
- Committer:
- josh_ohara
- Date:
- Thu May 14 17:35:35 2020 +0000
- Revision:
- 24:ff5af5a013b5
- Parent:
- 21:970807533b10
- Child:
- 27:eb755a345b1f
Added difficulty selection and difficulty progression with levels. FInishing touches now
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| josh_ohara | 3:8a140aa1ddcd | 1 | #include "Ship.h" |
| josh_ohara | 2:c2316b659b97 | 2 | |
| josh_ohara | 9:8e695df3cc36 | 3 | //N5110 lcdShip; used for testing |
| josh_ohara | 8:86cb9a9f8a73 | 4 | |
| josh_ohara | 8:86cb9a9f8a73 | 5 | Ship::Ship() |
| josh_ohara | 8:86cb9a9f8a73 | 6 | { |
| josh_ohara | 8:86cb9a9f8a73 | 7 | |
| josh_ohara | 8:86cb9a9f8a73 | 8 | } |
| josh_ohara | 8:86cb9a9f8a73 | 9 | |
| josh_ohara | 8:86cb9a9f8a73 | 10 | Ship::~Ship() |
| josh_ohara | 8:86cb9a9f8a73 | 11 | { |
| josh_ohara | 8:86cb9a9f8a73 | 12 | |
| josh_ohara | 8:86cb9a9f8a73 | 13 | } |
| josh_ohara | 8:86cb9a9f8a73 | 14 | |
| josh_ohara | 6:5bea67cc96f9 | 15 | void Ship::init(int height, int width) |
| josh_ohara | 2:c2316b659b97 | 16 | { |
| josh_ohara | 7:06a2558155f0 | 17 | X = WIDTH/2 - width/2; |
| josh_ohara | 8:86cb9a9f8a73 | 18 | Y = HEIGHT - 2; |
| josh_ohara | 6:5bea67cc96f9 | 19 | Height = height; |
| josh_ohara | 6:5bea67cc96f9 | 20 | Width = width; |
| josh_ohara | 7:06a2558155f0 | 21 | Speed = 0; |
| josh_ohara | 20:0b6f1cfc5be6 | 22 | Life = true; |
| josh_ohara | 21:970807533b10 | 23 | ship_bullet_vector.init(); |
| josh_ohara | 2:c2316b659b97 | 24 | } |
| josh_ohara | 2:c2316b659b97 | 25 | |
| josh_ohara | 8:86cb9a9f8a73 | 26 | |
| josh_ohara | 8:86cb9a9f8a73 | 27 | void Ship::render(N5110 &lcd) |
| josh_ohara | 8:86cb9a9f8a73 | 28 | { |
| josh_ohara | 21:970807533b10 | 29 | ship_bullet_vector.render(lcd); |
| josh_ohara | 20:0b6f1cfc5be6 | 30 | if(Life == true){ |
| josh_ohara | 2:c2316b659b97 | 31 | lcd.drawRect(X,Y,Width,Height,FILL_BLACK); |
| josh_ohara | 9:8e695df3cc36 | 32 | lcd.refresh(); |
| josh_ohara | 20:0b6f1cfc5be6 | 33 | } |
| josh_ohara | 2:c2316b659b97 | 34 | } |
| josh_ohara | 2:c2316b659b97 | 35 | |
| josh_ohara | 21:970807533b10 | 36 | void Ship::update(Direction d,float mag, Gamepad &pad, N5110 &lcd, int counter) |
| josh_ohara | 2:c2316b659b97 | 37 | { |
| josh_ohara | 24:ff5af5a013b5 | 38 | Speed = int(mag*5.0f); |
| josh_ohara | 2:c2316b659b97 | 39 | |
| josh_ohara | 2:c2316b659b97 | 40 | if (d == E) { |
| josh_ohara | 2:c2316b659b97 | 41 | X+=Speed; |
| josh_ohara | 9:8e695df3cc36 | 42 | } |
| josh_ohara | 9:8e695df3cc36 | 43 | else if (d == NE) { |
| josh_ohara | 2:c2316b659b97 | 44 | X+=0.5*Speed; |
| josh_ohara | 9:8e695df3cc36 | 45 | } |
| josh_ohara | 9:8e695df3cc36 | 46 | else if (d == SE) { |
| josh_ohara | 2:c2316b659b97 | 47 | X+=0.5*Speed; |
| josh_ohara | 9:8e695df3cc36 | 48 | } |
| josh_ohara | 9:8e695df3cc36 | 49 | else if (d == W) { |
| josh_ohara | 2:c2316b659b97 | 50 | X-=Speed; |
| josh_ohara | 9:8e695df3cc36 | 51 | } |
| josh_ohara | 9:8e695df3cc36 | 52 | else if (d == NW) { |
| josh_ohara | 2:c2316b659b97 | 53 | X-=0.5*Speed; |
| josh_ohara | 9:8e695df3cc36 | 54 | } |
| josh_ohara | 9:8e695df3cc36 | 55 | else if (d == SW) { |
| josh_ohara | 2:c2316b659b97 | 56 | X-=0.5*Speed; |
| josh_ohara | 9:8e695df3cc36 | 57 | } |
| josh_ohara | 2:c2316b659b97 | 58 | |
| josh_ohara | 2:c2316b659b97 | 59 | if (X < 1) { |
| josh_ohara | 2:c2316b659b97 | 60 | X = 1; |
| josh_ohara | 2:c2316b659b97 | 61 | } |
| josh_ohara | 8:86cb9a9f8a73 | 62 | if (X > WIDTH - Width - 1) { |
| josh_ohara | 8:86cb9a9f8a73 | 63 | X = WIDTH - Width - 1; |
| josh_ohara | 2:c2316b659b97 | 64 | } |
| josh_ohara | 21:970807533b10 | 65 | ship_bullet_vector.update(pad,lcd,X+4,Y,counter); |
| josh_ohara | 21:970807533b10 | 66 | if(Life == false){ |
| josh_ohara | 21:970807533b10 | 67 | pad.leds_on(); |
| josh_ohara | 21:970807533b10 | 68 | } |
| josh_ohara | 2:c2316b659b97 | 69 | } |
| josh_ohara | 2:c2316b659b97 | 70 | |
| josh_ohara | 2:c2316b659b97 | 71 | Vector2D Ship::get_position() { |
| josh_ohara | 2:c2316b659b97 | 72 | Vector2D p = {X,Y}; |
| josh_ohara | 2:c2316b659b97 | 73 | return p; |
| josh_ohara | 2:c2316b659b97 | 74 | } |
| josh_ohara | 3:8a140aa1ddcd | 75 | |
| josh_ohara | 3:8a140aa1ddcd | 76 | int Ship::get_height() { |
| josh_ohara | 3:8a140aa1ddcd | 77 | int height = Height; |
| josh_ohara | 3:8a140aa1ddcd | 78 | return height; |
| josh_ohara | 3:8a140aa1ddcd | 79 | } |
| josh_ohara | 3:8a140aa1ddcd | 80 | |
| josh_ohara | 3:8a140aa1ddcd | 81 | int Ship::get_width() { |
| josh_ohara | 3:8a140aa1ddcd | 82 | int width = Width; |
| josh_ohara | 3:8a140aa1ddcd | 83 | return width; |
| josh_ohara | 3:8a140aa1ddcd | 84 | } |
| josh_ohara | 4:18a1fc4c38e0 | 85 | |
| josh_ohara | 20:0b6f1cfc5be6 | 86 | void Ship::set_life(bool life){ |
| josh_ohara | 20:0b6f1cfc5be6 | 87 | Life = life; |
| josh_ohara | 20:0b6f1cfc5be6 | 88 | } |
| josh_ohara | 21:970807533b10 | 89 | |
| josh_ohara | 21:970807533b10 | 90 | bool Ship::get_life(){ |
| josh_ohara | 21:970807533b10 | 91 | return Life; |
| josh_ohara | 21:970807533b10 | 92 | } |
| josh_ohara | 20:0b6f1cfc5be6 | 93 | |
| josh_ohara | 21:970807533b10 | 94 | vector<Bullet> Ship::get_bullet_vector(){ |
| josh_ohara | 21:970807533b10 | 95 | vector<Bullet> v = ship_bullet_vector.get_vector(); |
| josh_ohara | 21:970807533b10 | 96 | return v; |
| josh_ohara | 21:970807533b10 | 97 | } |
| josh_ohara | 3:8a140aa1ddcd | 98 | |
| josh_ohara | 21:970807533b10 | 99 | void Ship::set_bullet_hit(int i, bool hit){ |
| josh_ohara | 21:970807533b10 | 100 | ship_bullet_vector.set_hit(i,hit); |
| josh_ohara | 21:970807533b10 | 101 | } |
| josh_ohara | 2:c2316b659b97 | 102 | |
| josh_ohara | 2:c2316b659b97 | 103 |