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.
People/People.cpp@36:27aa597db3d2, 2020-05-15 (annotated)
- Committer:
- evanso
- Date:
- Fri May 15 16:31:25 2020 +0000
- Revision:
- 36:27aa597db3d2
- Parent:
- 35:577c65bf914e
- Child:
- 40:71f947254fda
Created Parent Classes folder to hold all of the parent class files. Shortened function lengths in Game Engine class and organised function definitions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evanso | 33:7fedd8029473 | 1 | #include "People.h" |
evanso | 33:7fedd8029473 | 2 | |
evanso | 33:7fedd8029473 | 3 | const int k_people_sprite[5][4] = { |
evanso | 33:7fedd8029473 | 4 | { 0,1,1,0,}, |
evanso | 33:7fedd8029473 | 5 | { 1,1,1,1,}, |
evanso | 33:7fedd8029473 | 6 | { 1,1,1,1,}, |
evanso | 33:7fedd8029473 | 7 | { 0,1,1,0,}, |
evanso | 33:7fedd8029473 | 8 | { 0,1,1,0,}, |
evanso | 33:7fedd8029473 | 9 | }; |
evanso | 33:7fedd8029473 | 10 | |
evanso | 33:7fedd8029473 | 11 | People::People() { |
evanso | 33:7fedd8029473 | 12 | |
evanso | 33:7fedd8029473 | 13 | } |
evanso | 33:7fedd8029473 | 14 | |
evanso | 33:7fedd8029473 | 15 | People::~People() { |
evanso | 33:7fedd8029473 | 16 | |
evanso | 33:7fedd8029473 | 17 | } |
evanso | 33:7fedd8029473 | 18 | |
evanso | 33:7fedd8029473 | 19 | void People::init(Gamepad &pad, int position_x_start) { |
evanso | 36:27aa597db3d2 | 20 | // Define variables |
evanso | 35:577c65bf914e | 21 | sprite_x_length = 4; |
evanso | 35:577c65bf914e | 22 | sprite_y_length = 5; |
evanso | 33:7fedd8029473 | 23 | position_x_ = position_x_start; |
evanso | 33:7fedd8029473 | 24 | position_y_ = 43; |
evanso | 34:85ccc16f24d2 | 25 | alien_collision_flag = false; |
evanso | 34:85ccc16f24d2 | 26 | people_move_counter_ = 0; |
evanso | 34:85ccc16f24d2 | 27 | random_move_counter_ = 0; |
evanso | 33:7fedd8029473 | 28 | } |
evanso | 33:7fedd8029473 | 29 | |
evanso | 34:85ccc16f24d2 | 30 | void People::draw_people(N5110 &lcd, Direction d_, int map_length_, |
evanso | 34:85ccc16f24d2 | 31 | int position_x_map_){ |
evanso | 34:85ccc16f24d2 | 32 | position_x_ += calc_sprite_movement(d_); |
evanso | 34:85ccc16f24d2 | 33 | lcd.drawSprite(position_x_, position_y_, 5, 4, (int*)k_people_sprite); |
evanso | 34:85ccc16f24d2 | 34 | off_screen_x_y_checker(map_length_, position_x_map_); |
evanso | 34:85ccc16f24d2 | 35 | |
evanso | 34:85ccc16f24d2 | 36 | // move alien to top of screen if collision with alien |
evanso | 36:27aa597db3d2 | 37 | collision_with_alien(); |
evanso | 36:27aa597db3d2 | 38 | |
evanso | 34:85ccc16f24d2 | 39 | people_move_counter_++; |
evanso | 34:85ccc16f24d2 | 40 | } |
evanso | 34:85ccc16f24d2 | 41 | |
evanso | 36:27aa597db3d2 | 42 | void People::collision_with_alien(){ |
evanso | 36:27aa597db3d2 | 43 | if(alien_collision_flag){ |
evanso | 36:27aa597db3d2 | 44 | //people moves on its own but at half speed of spaceship |
evanso | 36:27aa597db3d2 | 45 | if (people_move_counter_%2 == 0) { |
evanso | 36:27aa597db3d2 | 46 | if (random_move_counter_ == 0){ |
evanso | 36:27aa597db3d2 | 47 | //move alien to top of screen |
evanso | 36:27aa597db3d2 | 48 | random_direction_ = 6; |
evanso | 36:27aa597db3d2 | 49 | random_move_counter_ = 43; |
evanso | 36:27aa597db3d2 | 50 | } |
evanso | 36:27aa597db3d2 | 51 | |
evanso | 36:27aa597db3d2 | 52 | //move poeple |
evanso | 36:27aa597db3d2 | 53 | move_direction(); |
evanso | 36:27aa597db3d2 | 54 | random_move_counter_ --; |
evanso | 36:27aa597db3d2 | 55 | } |
evanso | 36:27aa597db3d2 | 56 | } |
evanso | 36:27aa597db3d2 | 57 | } |
evanso | 36:27aa597db3d2 | 58 | |
evanso | 34:85ccc16f24d2 | 59 | void People::off_screen_x_y_checker(int map_length_, int position_x_map_){ |
evanso | 34:85ccc16f24d2 | 60 | // loops the people round if it reaches the end of the map |
evanso | 34:85ccc16f24d2 | 61 | if(position_x_ <= (84 - map_length_)){ |
evanso | 34:85ccc16f24d2 | 62 | position_x_ += map_length_; |
evanso | 34:85ccc16f24d2 | 63 | }else if (position_x_ >= map_length_){ |
evanso | 34:85ccc16f24d2 | 64 | position_x_ -= map_length_ + 10; |
evanso | 34:85ccc16f24d2 | 65 | } |
evanso | 34:85ccc16f24d2 | 66 | } |
evanso | 34:85ccc16f24d2 | 67 | |
evanso | 34:85ccc16f24d2 | 68 | bool People::get_alien_collision_flag(){ |
evanso | 34:85ccc16f24d2 | 69 | return alien_collision_flag; |
evanso | 34:85ccc16f24d2 | 70 | } |