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
Revision 8:8287d2ef965d, committed 2020-04-29
- Comitter:
- el19zf
- Date:
- Wed Apr 29 15:20:34 2020 +0000
- Parent:
- 7:c49f3d3b672f
- Child:
- 9:62d6559f0d50
- Commit message:
- update class collision and game mechanics
Changed in this revision
--- a/Collision/Collision.cpp Wed Apr 29 05:10:57 2020 +0000 +++ b/Collision/Collision.cpp Wed Apr 29 15:20:34 2020 +0000 @@ -8,3 +8,48 @@ } +void Collision::init() { + _health = 3; + _check_index = 0; + +} + +bool Collision::check(N5110 &lcd) { + + _check_index = 0; + if(lcd.getPixel(_people_pos.x+1,_people_pos.y)) { _check_index = 1; }//check every sprite set points + if(lcd.getPixel(_people_pos.x+2,_people_pos.y)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x,_people_pos.y+1)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+1,_people_pos.y+1)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+2,_people_pos.y+1)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+3,_people_pos.y+1)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+1,_people_pos.y+2)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+2,_people_pos.y+2)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+1,_people_pos.y+3)) { _check_index = 1; } + if(lcd.getPixel(_people_pos.x+2,_people_pos.y+3)) { _check_index = 1; } + //printf("index = %d\n",_check_index); + //printf("people_pos = %f,%f\n",_people_pos.x+2,_people_pos.y+3); + if((_check_index == 1)&&(_health > 0)) { + //_check_index = 0; + _health = _health - 1; + } + return _check_index; + //printf("health is %d\n",_health); + +} + +void Collision::draw(N5110 &lcd) { + if(_health > 0) + lcd.drawRect(1,1,_health,2,FILL_TRANSPARENT); +} + +int Collision::get_health() { + return _health; +} + +void Collision::set_pos(Vector2D pos) { + _people_pos = pos; +} + + +
--- a/Collision/Collision.h Wed Apr 29 05:10:57 2020 +0000 +++ b/Collision/Collision.h Wed Apr 29 15:20:34 2020 +0000 @@ -19,11 +19,24 @@ Collision();//constructor ~Collision();//destructor + //init void init(); - void check(); + //check whether there is a collision + bool check(N5110 &lcd); + + void draw(N5110 &lcd); + //get health of people + int get_health(); + //accessors + void set_pos(Vector2D pos); + private: + int _health; + int _check_index; + + Vector2D _people_pos;//starting coordinate }; #endif
--- a/People/People.cpp Wed Apr 29 05:10:57 2020 +0000 +++ b/People/People.cpp Wed Apr 29 15:20:34 2020 +0000 @@ -1,6 +1,6 @@ #include "People.h" #define INIT_x 40 -#define INIT_y 20 +#define INIT_y 43 const int people_sprite[4][4] = { @@ -92,9 +92,9 @@ if(0.5f < _mag < 0.75f) { _x -= 3; _y += 3;} else { _x -= 4; _y += 4;}} //without going off screen - if (_x < 0) { _x = 0;} else - if (_x > 80) { _x = 80;} else - if (_y < 0) {_y = 0;} else - if (_y > 44) {_y = 44;} + 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
--- a/PeopleEngine/PeopleEngine.cpp Wed Apr 29 05:10:57 2020 +0000 +++ b/PeopleEngine/PeopleEngine.cpp Wed Apr 29 15:20:34 2020 +0000 @@ -34,4 +34,11 @@ //lcd.clear(); _people.draw(lcd); lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);//help our design -} \ No newline at end of file + lcd.drawCircle(WIDTH/2,0,6,FILL_TRANSPARENT);//destination + lcd.drawCircle(WIDTH/2,HEIGHT-1,6,FILL_TRANSPARENT);//starting point +} + +Vector2D PeopleEngine::get_pos() { + _p = _people.get_pos(); + return _p; +} \ No newline at end of file
--- a/PeopleEngine/PeopleEngine.h Wed Apr 29 05:10:57 2020 +0000 +++ b/PeopleEngine/PeopleEngine.h Wed Apr 29 15:20:34 2020 +0000 @@ -24,6 +24,8 @@ void update(); void draw(N5110 &lcd); + Vector2D get_pos(); + private:
--- a/main.cpp Wed Apr 29 05:10:57 2020 +0000 +++ b/main.cpp Wed Apr 29 15:20:34 2020 +0000 @@ -17,6 +17,7 @@ #include "People.h" #include "PeopleEngine.h" #include "shot.h" +#include "Collision.h" // objects @@ -24,6 +25,7 @@ N5110 lcd; PeopleEngine engine; shot shot; +Collision collision; //flag and triggers Ticker ticker; @@ -35,7 +37,7 @@ timer_flag = 1; } void init(); -void control_people(); +void control_check(); void shot_update(); @@ -43,27 +45,30 @@ { //initial init(); - - ticker.attach(&flip,5); + //set a ticker + ticker.attach(&flip,2); //a infinite loop to control position of the people, update the game state while(1) { if(timer_flag == 1) { timer_flag = 0; - if(shot._size < 30) - shot._size = shot._size + 2; + if(shot.get_size() < 30){ + int size = shot.get_size()+ 2; + shot.set_size(size); + } } lcd.clear(); - // people - control_people(); + // shot update shot_update(); + + // control people and check collision + control_check(); + lcd.refresh(); //printf("shot refresh\n"); //printf("size = %d\n",shot._size); wait_ms(200);//fps = 5 - } - } @@ -74,15 +79,24 @@ engine.init(); pad.init(); shot.init(); + collision.init(); lcd.refresh(); } -void control_people() +void control_check() { engine.read_input(pad); engine.update(); + collision.set_pos(engine.get_pos()); + if(collision.check(lcd)) { + engine.init(); + lcd.clear(); + shot.init(); + } + collision.draw(lcd); engine.draw(lcd); } + void shot_update() { shot.update();
--- a/shot/shot.cpp Wed Apr 29 05:10:57 2020 +0000 +++ b/shot/shot.cpp Wed Apr 29 15:20:34 2020 +0000 @@ -1,10 +1,10 @@ #include "shot.h" int shots[4][3][3] = { - { {1,1,0},{1,1,0},{0,0,1}, }, - { {1,0,0},{1,1,1},{1,0,0}, }, + { {0,1,0},{1,1,1},{0,1,0}, }, + { {1,1,1},{0,1,1},{0,0,1}, }, { {1,1,1},{0,1,0},{0,1,0}, }, - { {0,1,0},{1,1,1},{0,1,0}, } + { {1,1,0},{1,1,0},{0,0,1}, } }; shot::shot() @@ -74,8 +74,6 @@ } } - - void shot::update() { for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { @@ -114,10 +112,24 @@ { for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { // if beyoud border, delete it and generate new one, keep total number constant - if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)) { + if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)|| + //keep shots away from starting point + (((*i).x==33)&&((*i).y==3))||(((*i).x==48)&&((*i).y==3))||(((*i).x==33)&&((*i).y==45))||(((*i).x==48)&&((*i).y==45))) + { init_pos(i); (*i).type = ((*i).type + 2)%4; (*i).dir = ((*i).dir + 1)%6; // increase randomness } } } + +void shot::set_size(int size) { + _size = size; +} + +int shot::get_size() { + return _size; +} + + + \ No newline at end of file
--- a/shot/shot.h Wed Apr 29 05:10:57 2020 +0000 +++ b/shot/shot.h Wed Apr 29 15:20:34 2020 +0000 @@ -14,9 +14,6 @@ @13 April 2020 */ - - - struct shot_posandtype { int x; int y; @@ -32,25 +29,29 @@ shot(); //destructor ~shot(); - + //generate first 10 shots void init(); - + //random pos void init_pos(shot_posandtype* i); - + //increase number of shots along with time void gen_shot(); - + //moving function void update(); void draw(N5110 &lcd); - + //if beyoud border, delete it and generate new one, keep total number constant void delete_shot(); + //accessors + void set_size(int size); + ////mutators + int get_size(); - int _size;//use for resize number of vector + //int _size;//use for resize number of vector private: - std::vector<shot_posandtype> _p; + int _size; }; #endif