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
alien/alien.cpp@17:9da9d0d41fca, 2019-04-19 (annotated)
- Committer:
- fy14lkaa
- Date:
- Fri Apr 19 18:32:06 2019 +0000
- Revision:
- 17:9da9d0d41fca
- Parent:
- 16:cba54d0696dc
- Child:
- 18:1c1a3fb0efae
added a function [Vector2D alien::get_pos()] in alien.cpp. This function used to return alien's position.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| fy14lkaa | 10:13e6996223d5 | 1 | #include "alien.h" |
| fy14lkaa | 10:13e6996223d5 | 2 | |
| fy14lkaa | 10:13e6996223d5 | 3 | |
| fy14lkaa | 10:13e6996223d5 | 4 | alien::alien() |
| fy14lkaa | 10:13e6996223d5 | 5 | { |
| fy14lkaa | 10:13e6996223d5 | 6 | |
| fy14lkaa | 10:13e6996223d5 | 7 | } |
| fy14lkaa | 10:13e6996223d5 | 8 | |
| fy14lkaa | 10:13e6996223d5 | 9 | alien::~alien() |
| fy14lkaa | 10:13e6996223d5 | 10 | { |
| fy14lkaa | 10:13e6996223d5 | 11 | |
| fy14lkaa | 10:13e6996223d5 | 12 | } |
| fy14lkaa | 10:13e6996223d5 | 13 | |
| fy14lkaa | 10:13e6996223d5 | 14 | void alien::init(int size,int speed) |
| fy14lkaa | 10:13e6996223d5 | 15 | { |
| fy14lkaa | 10:13e6996223d5 | 16 | |
| fy14lkaa | 11:df8ce6637022 | 17 | _size = size; |
| fy14lkaa | 11:df8ce6637022 | 18 | |
| fy14lkaa | 11:df8ce6637022 | 19 | |
| fy14lkaa | 13:9d6ee753eca6 | 20 | |
| fy14lkaa | 11:df8ce6637022 | 21 | |
| fy14lkaa | 11:df8ce6637022 | 22 | |
| fy14lkaa | 11:df8ce6637022 | 23 | srand(time(NULL)); |
| fy14lkaa | 11:df8ce6637022 | 24 | int direction = rand() % 8; // randomise initial direction. |
| fy14lkaa | 11:df8ce6637022 | 25 | |
| fy14lkaa | 11:df8ce6637022 | 26 | // 4 possibilities. Get random modulo and set velocities accordingly |
| fy14lkaa | 11:df8ce6637022 | 27 | if (direction == 0) { |
| fy14lkaa | 11:df8ce6637022 | 28 | _velocity.x = speed; |
| fy14lkaa | 11:df8ce6637022 | 29 | _velocity.y = speed; |
| fy14lkaa | 11:df8ce6637022 | 30 | } else if (direction == 1) { |
| fy14lkaa | 11:df8ce6637022 | 31 | _velocity.x = speed; |
| fy14lkaa | 11:df8ce6637022 | 32 | _velocity.y = -speed; |
| fy14lkaa | 11:df8ce6637022 | 33 | } else if (direction == 2) { |
| fy14lkaa | 11:df8ce6637022 | 34 | _velocity.x = speed; |
| fy14lkaa | 11:df8ce6637022 | 35 | _velocity.y = speed; |
| fy14lkaa | 11:df8ce6637022 | 36 | } else { |
| fy14lkaa | 11:df8ce6637022 | 37 | _velocity.x = -speed; |
| fy14lkaa | 11:df8ce6637022 | 38 | _velocity.y = -speed; |
| fy14lkaa | 11:df8ce6637022 | 39 | } |
| fy14lkaa | 11:df8ce6637022 | 40 | } |
| fy14lkaa | 11:df8ce6637022 | 41 | |
| fy14lkaa | 11:df8ce6637022 | 42 | |
| fy14lkaa | 14:6c4bce33c1a1 | 43 | void alien::draw(N5110 &lcd) |
| fy14lkaa | 13:9d6ee753eca6 | 44 | { |
| fy14lkaa | 13:9d6ee753eca6 | 45 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
| fy14lkaa | 13:9d6ee753eca6 | 46 | } |
| fy14lkaa | 13:9d6ee753eca6 | 47 | |
| fy14lkaa | 14:6c4bce33c1a1 | 48 | void alien::update() |
| fy14lkaa | 14:6c4bce33c1a1 | 49 | { |
| fy14lkaa | 14:6c4bce33c1a1 | 50 | _x += _velocity.x; |
| fy14lkaa | 14:6c4bce33c1a1 | 51 | _y += _velocity.y; |
| fy14lkaa | 15:ebbd903455f0 | 52 | |
| fy14lkaa | 15:ebbd903455f0 | 53 | void alien::set_velocity(Vector2D v) |
| fy14lkaa | 15:ebbd903455f0 | 54 | { |
| fy14lkaa | 15:ebbd903455f0 | 55 | _velocity.x = v.x; |
| fy14lkaa | 15:ebbd903455f0 | 56 | _velocity.y = v.y; |
| fy14lkaa | 14:6c4bce33c1a1 | 57 | } |
| fy14lkaa | 16:cba54d0696dc | 58 | |
| fy14lkaa | 16:cba54d0696dc | 59 | Vector2D alien::get_velocity() |
| fy14lkaa | 16:cba54d0696dc | 60 | { |
| fy14lkaa | 16:cba54d0696dc | 61 | Vector2D v = {_velocity.x,_velocity.y}; |
| fy14lkaa | 16:cba54d0696dc | 62 | return v; |
| fy14lkaa | 15:ebbd903455f0 | 63 | } |
| fy14lkaa | 16:cba54d0696dc | 64 | |
| fy14lkaa | 17:9da9d0d41fca | 65 | Vector2D alien::get_pos() |
| fy14lkaa | 17:9da9d0d41fca | 66 | { |
| fy14lkaa | 17:9da9d0d41fca | 67 | Vector2D p = {_x,_y}; |
| fy14lkaa | 17:9da9d0d41fca | 68 | return p; |
| fy14lkaa | 17:9da9d0d41fca | 69 | } |