Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Map::blocked(float x, float y)
- {
- //x= columns
- //y =lines
- return array[(int) y][(int) x]!=0;
- }
- monsters::monsters():speed(5){
- startX=0;
- startY=0;
- qsrand(time(NULL));
- //Set a random start position in the map-> the array is [12][8]
- do{
- startX= qrand() % 12;
- startY= qrand() % 8;
- }while(!map->blocked(startX, startY));
- int arrayDirection [2]= {-50, 50};//first move= 1 direction among two -> left of right?
- int x= qrand() %2;
- direction= arrayDirection[x];
- }
- QRectF monsters::boundingRect() const{
- return QRectF(0,0, size_element, size_element);
- }
- void monsters::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
- painter->drawPixmap(0,0,size_element,size_element,QPixmap("path to the image"));
- }
- void monsters::moveOnMap(){
- QTimer *t = new QTimer(this);
- connect(t,SIGNAL(timeout()),this,SLOT(advance()));
- t->start(50);
- }
- void monsters::advance(){
- //my tile size is 50x50 so I have to divide by 50 my direction to get my array index
- switch(direction){
- case 50:
- if(!map->blocked((direction/50), startY)){
- moveBy(direction,startY);
- }
- else
- {
- moveBy(-(direction)/,startY);//move in the opposite directions
- }
- direction+=50;
- case -50:
- if(!map->blocked((direction/50), startY)){
- moveBy(direction,startY);
- }
- else
- {
- moveBy(-(direction),startY);
- }
- }
- }
- int nbMonsters=5;
- for(int i=0; i<nbMonsters; i++){
- monsters *monster= new monsters();
- monster->setPos(monster->getPostionX(), monster->getPostionY());
- myMap->getScene()->addItem(monster);
- }
- getPositionX and getPositionY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement