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
Diff: PeopleEngine/People.cpp
- Revision:
- 13:eb60628db8bf
- Parent:
- 12:009895f6b6e4
- Child:
- 14:42b8a91e463c
diff -r 009895f6b6e4 -r eb60628db8bf PeopleEngine/People.cpp --- a/PeopleEngine/People.cpp Mon May 11 14:43:16 2020 +0000 +++ b/PeopleEngine/People.cpp Thu May 14 16:45:20 2020 +0000 @@ -1,33 +1,94 @@ #include "People.h" #define INIT_x 1 -#define INIT_y 23 +#define INIT_y 20 +const int people_sprite[8][5] = { + {0,1,1,1,0}, + {0,1,1,1,0}, + {0,0,1,0,0}, + {1,1,1,1,1}, + {0,0,1,0,0}, + {0,1,0,1,0}, + {0,1,0,1,0}, + {0,1,0,1,0}, +};//try to draw a people, but for the operability of game, it seems not a good implementation.. -const int people_sprite[4][4] = { - {0,1,1,0}, - {1,1,1,1}, - {0,1,1,0}, - {0,1,1,0}, -};//try to draw a people, but for the operability of game, seems not good.. +const int running_people_sprite[8][5] = { + {0,1,1,1,0}, + {0,1,1,1,0}, + {0,0,1,0,0}, + {1,1,1,1,1}, + {0,0,1,0,0}, + {0,1,0,1,0}, + {1,0,0,0,1}, + {1,0,0,0,1}, +}; -People::People() { +People::People() +{ } -People::~People() { +People::~People() +{ } -void People::init() { - +void People::init() +{ _x = INIT_x; _y = INIT_y;//Set initial postion of people + _index = 0; } -void People::draw(N5110 &lcd) { + +void People::draw(N5110 &lcd,int flag) +{ + if(int(_mag*5)!=0&&flag!=0){ + if(!_index){ + lcd.drawSprite(_x,_y,8,5,(int*)people_sprite);//Draw sprite to represent a people + // printf("drawSprite"); + _index = 1; + }else{ + lcd.drawSprite(_x,_y,8,5,(int*)running_people_sprite); + _index = 0; + } + }else{ + lcd.drawSprite(_x,_y,8,5,(int*)people_sprite); + } +} + +/*control people by Joystick in different angle and magnitude + */ +void People::update() +{ + if(_d == S) { + _x += 0; _y += int(_mag*5); + } else if(_d == SE){ + _x += int(_mag*5); _y += int(_mag*5); + } else if(_d == E){ + _x += int(_mag*5); _y += 0; + } else if(_d == NE){ + _x += int(_mag*5); _y -= int(_mag*5); + } else if(_d == N){ + _x -= 0; _y -= int(_mag*5); + } else if(_d == NW){ + _x -= int(_mag*5); _y -= int(_mag*5); + } else if(_d == W){ + _x -= int(_mag*5); _y -= 0; + } else if(_d == NW){ + _x -= int(_mag*5); _y += int(_mag*5); + } + //printf("Mag = %f,Direction = %d,Coordinate = %d, %d\n",_mag,_d,_x,_y); - //Draw sprite to represent a people - lcd.drawSprite(_x,_y,4,4,(int*)people_sprite); - // printf("drawSprite"); + control_lim();//without going off screen +} + +void People::control_lim() +{ + if (_x < 1) { _x = 1;}//left + else if (_x > 78) { _x = 78;}//right + else if (_y < 1) {_y = 1;}//top + else if (_y > 41) {_y = 41;}//bottom } void People::set_pos(Vector2D p) @@ -48,53 +109,4 @@ //printf("Coord = %f,%f\n",p.x,p.y); return p; } - -void People::update() -{ - if (_d == S) { - if(_mag < 0.25f) { _x += 0; _y += 1;} else - if(0.25f < _mag < 0.5f) { _x += 0; _y += 2;} else - if(0.5f < _mag < 0.75f) { _x += 0; _y += 3;} else - { _x += 0; _y += 4;}}else - if (_d == SE){ - if(_mag < 0.25f) { _x += 1; _y += 1;} else - if(0.25f < _mag < 0.5f) { _x += 2; _y += 2;} else - if(0.5f < _mag < 0.75f) { _x += 3; _y += 3;} else - { _x += 4; _y += 4;}}else - if (_d == E) { - if(_mag < 0.25f) { _x += 1; _y += 0;} else - if(0.25f < _mag < 0.5f) { _x += 2; _y += 0;} else - if(0.5f < _mag < 0.75f) { _x += 3; _y += 0;} else - { _x += 4; _y += 0;}}else - if (_d == NE){ - if(_mag < 0.25f) { _x += 1; _y -= 1;} else - if(0.25f < _mag < 0.5f) { _x += 2; _y -= 2;} else - if(0.5f < _mag < 0.75f) { _x += 3; _y -= 3;} else - { _x += 4; _y -= 4;}}else - if (_d == N) { - if(_mag < 0.25f) { _x += 0; _y -= 1;} else - if(0.25f < _mag < 0.5f) { _x += 0; _y -= 2;} else - if(0.5f < _mag < 0.75f) { _x += 0; _y -= 3;} else - { _x += 0; _y -= 4;}}else - if (_d == NW){ - if(_mag < 0.25f) { _x -= 1; _y -= 1;} else - if(0.25f < _mag < 0.5f) { _x -= 2; _y -= 2;} else - if(0.5f < _mag < 0.75f) { _x -= 3; _y -= 3;} else - { _x -= 4; _y -= 4;}}else - if (_d == W) { - if(_mag < 0.25f) { _x -= 1; _y -= 0;} else - if(0.25f < _mag < 0.5f) { _x -= 2; _y -= 0;} else - if(0.5f < _mag < 0.75f) { _x -= 3; _y -= 0;} else - { _x -= 4; _y -= 0;}}else - if (_d == NW){ - if(_mag < 0.25f) { _x -= 1; _y += 1;} else - if(0.25f < _mag < 0.5f) { _x -= 2; _y += 2;} else - if(0.5f < _mag < 0.75f) { _x -= 3; _y += 3;} else - { _x -= 4; _y += 4;}} - //without going off screen - if (_x < 1) { _x = 1;} else - if (_x > 79) { _x = 79;} else - if (_y < 1) {_y = 1;} else - if (_y > 43) {_y = 43;} -} - \ No newline at end of file + \ No newline at end of file