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
shot/shot.cpp
- Committer:
- el19zf
- Date:
- 2020-05-09
- Revision:
- 9:62d6559f0d50
- Parent:
- 8:8287d2ef965d
- Child:
- 10:02ab3324be6c
File content as of revision 9:62d6559f0d50:
#include "shot.h" int shots[4][3][3] = { { {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}, }, { {1,1,0},{1,1,0},{0,0,1}, } }; shot::shot() { } shot::~shot() { } void shot::init() { _size = 10; _p.resize(_size); srand(time(NULL)); for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { init_pos(i); (*i).type = rand() % 4;//randomise initial type (*i).dir = rand() % 6;//randomise initial direction } } void shot::init_pos(shot_posandtype* i) { int num_pos = rand() % 6;//randomise initial position if (num_pos == 0) { (*i).x = WIDTH/4; (*i).y = 0; }//top if (num_pos == 1) { (*i).x = WIDTH/2; (*i).y = 0; }//top if (num_pos == 2) { (*i).x = 3*WIDTH/4; (*i).y = 0; }//top if (num_pos == 3) { (*i).x = WIDTH/4; (*i).y = HEIGHT-3; }//bottom if (num_pos == 4) { (*i).x = WIDTH/2; (*i).y = HEIGHT-3; }//bottom if (num_pos == 5) { (*i).x = 3*WIDTH/4; (*i).y = HEIGHT-3; }//bottom } void shot::gen_shot() { _p.resize(_size); for(std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { if(((*i).x == 0)&&((*i).y == 0)) { init_pos(i); (*i).type = rand() % 4;//randomise initial type (*i).dir = rand() % 6;//randomise initial direction } } } void shot::update() { for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { if ((*i).dir == 0) { (*i).x +=1; (*i).y +=1;//SE } else if ((*i).dir == 1) { (*i).x +=0; (*i).y +=1;//S } else if ((*i).dir == 2) { (*i).x +=1; (*i).y -=1;//NE } else if ((*i).dir == 3) { (*i).x -=1; (*i).y -=1;//NW } else if ((*i).dir == 4) { (*i).x -=0; (*i).y -=1;//N } else if ((*i).dir == 5) { (*i).x -=1; (*i).y +=1;//SW } } } void shot::draw(N5110 &lcd) { for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) { lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]); //printf("coordinate = %d,%d\n",(*i).x,(*i).y); } } void shot::delete_shot() { 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)|| //keep shots away from starting point (((*i).x==3)&&((*i).y==27)) || (((*i).x==3)&&((*i).y==18))|| (((*i).x==80)&&((*i).y==17)) || (((*i).x==80)&&((*i).y==28))) { 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; }