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.
RandomMovement/RandomMovement.cpp@34:85ccc16f24d2, 2020-05-15 (annotated)
- Committer:
- evanso
- Date:
- Fri May 15 12:05:33 2020 +0000
- Revision:
- 34:85ccc16f24d2
- Parent:
- 28:a5958497d5ce
Added when aliens collide with people, the people are abducted to the top of the screen. Then that alien tracks the spaceship and moves towards it rather than randomly moving.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| evanso | 28:a5958497d5ce | 1 | #include "RandomMovement.h" |
| evanso | 28:a5958497d5ce | 2 | |
| evanso | 28:a5958497d5ce | 3 | void RandomMovement::move_direction(){ |
| evanso | 28:a5958497d5ce | 4 | switch (random_direction_) { |
| evanso | 28:a5958497d5ce | 5 | case 0: set_sprite_direction(1, 0); break; |
| evanso | 28:a5958497d5ce | 6 | case 1: set_sprite_direction(1, 1); break; |
| evanso | 28:a5958497d5ce | 7 | case 2: set_sprite_direction(1, -1); break; |
| evanso | 28:a5958497d5ce | 8 | case 3: set_sprite_direction(-1, 0); break; |
| evanso | 28:a5958497d5ce | 9 | case 4: set_sprite_direction(-1, 1); break; |
| evanso | 28:a5958497d5ce | 10 | case 5: set_sprite_direction(-1, -1); break; |
| evanso | 34:85ccc16f24d2 | 11 | case 6: set_sprite_direction(0, -1); break; |
| evanso | 28:a5958497d5ce | 12 | } |
| evanso | 28:a5958497d5ce | 13 | } |
| evanso | 28:a5958497d5ce | 14 | |
| evanso | 28:a5958497d5ce | 15 | void RandomMovement::set_sprite_direction(int x_change,int y_change){ |
| evanso | 28:a5958497d5ce | 16 | position_x_ += x_change; |
| evanso | 28:a5958497d5ce | 17 | position_y_ += y_change; |
| evanso | 28:a5958497d5ce | 18 | } |
| evanso | 28:a5958497d5ce | 19 | |
| evanso | 28:a5958497d5ce | 20 | void RandomMovement::set_random_move(){ |
| evanso | 28:a5958497d5ce | 21 | // sprite only moves in one general direction |
| evanso | 28:a5958497d5ce | 22 | if(direction_){ |
| evanso | 28:a5958497d5ce | 23 | random_direction_ = rand() % 3; |
| evanso | 28:a5958497d5ce | 24 | }else{ |
| evanso | 28:a5958497d5ce | 25 | random_direction_ = rand() % 3 + 3; |
| evanso | 28:a5958497d5ce | 26 | //printf("\random_direction_ = %d\n", random_direction_); |
| evanso | 28:a5958497d5ce | 27 | } |
| evanso | 28:a5958497d5ce | 28 | random_move_counter_ = rand() % 10 + 20; |
| evanso | 34:85ccc16f24d2 | 29 | } |
| evanso | 34:85ccc16f24d2 | 30 | |
| evanso | 34:85ccc16f24d2 | 31 | int RandomMovement::calc_sprite_movement(Direction d_){ |
| evanso | 34:85ccc16f24d2 | 32 | // moves the people in oposite direction to spaceship when it's position is |
| evanso | 34:85ccc16f24d2 | 33 | // at min and max x positions and joystick has direction |
| evanso | 34:85ccc16f24d2 | 34 | if (d_ == W || d_ == NW || d_ == SW){ |
| evanso | 34:85ccc16f24d2 | 35 | return 2; |
| evanso | 34:85ccc16f24d2 | 36 | }else if (d_ == E || d_ == NE || d_ == SE){ |
| evanso | 34:85ccc16f24d2 | 37 | return -2; |
| evanso | 34:85ccc16f24d2 | 38 | }else { |
| evanso | 34:85ccc16f24d2 | 39 | return 0; |
| evanso | 34:85ccc16f24d2 | 40 | } |
| evanso | 34:85ccc16f24d2 | 41 | } |