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: shot/shot.cpp
- Revision:
- 5:6774b7272e85
- Parent:
- 4:b12a49f0b730
- Child:
- 6:dce38fe4e092
--- a/shot/shot.cpp Tue Apr 14 05:47:23 2020 +0000 +++ b/shot/shot.cpp Tue Apr 14 06:39:11 2020 +0000 @@ -1,25 +1,22 @@ #include "shot.h" - -const int shot1[3][3] = { +const int shots[4][3][3] = { +{ {1,1,0}, {1,1,0}, - {0,0,1}, -}; -const int shot2[3][3] = { + {0,0,1}, }, +{ {1,0,0}, {1,1,1}, - {1,0,0}, -}; -const int shot3[3][3] = { + {1,0,0}, }, +{ {1,1,1}, {0,1,0}, - {0,1,0}, -}; -const int shot4[3][3] = { + {0,1,0}, }, +{ {0,1,0}, {1,1,1}, - {0,1,0}, + {0,1,0}, } }; @@ -31,13 +28,32 @@ } -void shot::init() { - +void shot::init(N5110 &lcd) { + _p.resize(50); + int index = rand_type(); + for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { + int num_dir = rand()%4; + if (num_dir == 0) { (*i).x = (WIDTH/2-1); + (*i).y = 0; } + if (num_dir == 1) { (*i).x = (WIDTH/2-1); + (*i).y = (HEIGHT-3); } + if (num_dir == 2) { (*i).x = 0; + (*i).y = (HEIGHT/2-1);} + if (num_dir == 3) { (*i).x = (WIDTH-3); + (*i).y = (HEIGHT/2-1);} + printf("pos = %d,%d\n",(*i).x,(*i).y); + + } + for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { + (*i).type = rand() % 4; + (*i).dir = rand() % 8; + lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]); +} } int shot::rand_dir() { - srand(time(NULL)); + //srand(time(NULL)); /*int d_num = rand() % 8; //randomise initial direction if (d_num == 0) { d = N; } else if (d_num == 1) { d = NE; } else @@ -47,8 +63,8 @@ if (d_num == 5) { d = SW; } else if (d_num == 6) { d = W; } else { d = NW;}*/ - int d = rand() % 4;//randomise initial direction - return d; + //int d = rand() % 4;//randomise initial direction + //return d; } int shot::rand_type() { @@ -74,7 +90,7 @@ //printf("random num = %d\n",num_dir);*/ _p.resize(10); int index = rand_type(); - for (std::vector<Vector2D>::iterator i = _p.begin(); i < _p.end(); i++) { + for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { int num_dir = rand()%4; if (num_dir == 0) { (*i).x = (WIDTH/2-1); (*i).y = 0; } @@ -84,30 +100,35 @@ (*i).y = (HEIGHT/2-1);} if (num_dir == 3) { (*i).x = (WIDTH-3); (*i).y = (HEIGHT/2-1);} - printf("pos = %f,%f\n",(*i).x,(*i).y); + printf("pos = %d,%d\n",(*i).x,(*i).y); } } -void shot::shot_move() { - for (std::vector<Vector2D>::iterator i = _p.begin(); i < _p.end(); i++) { - int num_dir = rand() % 8; - if (num_dir == 0) { (*i).x +=0; (*i).y +=1; } else - if (num_dir == 1) { (*i).x +=1; (*i).y +=1; } else - if (num_dir == 2) { (*i).x +=1; (*i).y +=0; } else - if (num_dir == 3) { (*i).x +=1; (*i).y -=1; } else - if (num_dir == 4) { (*i).x +=0; (*i).y -=1; } else - if (num_dir == 5) { (*i).x -=1; (*i).y -=1; } else - if (num_dir == 6) { (*i).x -=1; (*i).y -=0; } else - if (num_dir == 7) { (*i).x -=1; (*i).y +=1; } - } + + +void shot::update(N5110 &lcd) { + for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { + if ((*i).dir == 0) { (*i).x +=0; (*i).y +=1; } else + if ((*i).dir == 1) { (*i).x +=1; (*i).y +=1; } else + if ((*i).dir == 2) { (*i).x +=1; (*i).y +=0; } else + if ((*i).dir == 3) { (*i).x +=1; (*i).y -=1; } else + if ((*i).dir == 4) { (*i).x +=0; (*i).y -=1; } else + if ((*i).dir == 5) { (*i).x -=1; (*i).y -=1; } else + if ((*i).dir == 6) { (*i).x -=1; (*i).y -=0; } else + if ((*i).dir == 7) { (*i).x -=1; (*i).y +=1; } + lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]); + } } + + void shot::draw(N5110 &lcd){ - for (std::vector<Vector2D>::iterator i = _p.begin(); i < _p.end(); i++) { - int num_type = rand() % 4; - if (num_type == 0) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot1);} - if (num_type == 1) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot2);} - if (num_type == 2) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot3);} - if (num_type == 3) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot4);} - } + /*for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { + (*i).type = rand() % 4; + (*i).dir = rand() % 8; + lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]); + //if ((*i).type == 1) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot2);} + //if ((*i).type == 2) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot3);} + //if ((*i).type == 3) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shot4);} + }*/ }