Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. bool Map::blocked(float x, float y)
  2. {
  3. //x= columns
  4. //y =lines
  5. return array[(int) y][(int) x]!=0;
  6. }
  7.  
  8. monsters::monsters():speed(5){
  9.  
  10. startX=0;
  11. startY=0;
  12.  
  13. qsrand(time(NULL));
  14. //Set a random start position in the map-> the array is [12][8]
  15. do{
  16. startX= qrand() % 12;
  17. startY= qrand() % 8;
  18.  
  19. }while(!map->blocked(startX, startY));
  20.  
  21.  
  22. int arrayDirection [2]= {-50, 50};//first move= 1 direction among two -> left of right?
  23. int x= qrand() %2;
  24. direction= arrayDirection[x];
  25.  
  26.  
  27.  
  28. }
  29.  
  30. QRectF monsters::boundingRect() const{
  31. return QRectF(0,0, size_element, size_element);
  32. }
  33.  
  34. void monsters::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
  35. painter->drawPixmap(0,0,size_element,size_element,QPixmap("path to the image"));
  36. }
  37.  
  38. void monsters::moveOnMap(){
  39.  
  40. QTimer *t = new QTimer(this);
  41. connect(t,SIGNAL(timeout()),this,SLOT(advance()));
  42. t->start(50);
  43.  
  44. }
  45.  
  46. void monsters::advance(){
  47.  
  48. //my tile size is 50x50 so I have to divide by 50 my direction to get my array index
  49.  
  50. switch(direction){
  51.  
  52. case 50:
  53.  
  54. if(!map->blocked((direction/50), startY)){
  55. moveBy(direction,startY);
  56. }
  57. else
  58. {
  59. moveBy(-(direction)/,startY);//move in the opposite directions
  60. }
  61.  
  62. direction+=50;
  63.  
  64. case -50:
  65.  
  66. if(!map->blocked((direction/50), startY)){
  67. moveBy(direction,startY);
  68. }
  69. else
  70. {
  71. moveBy(-(direction),startY);
  72. }
  73. }
  74.  
  75. }
  76.  
  77. int nbMonsters=5;
  78.  
  79. for(int i=0; i<nbMonsters; i++){
  80. monsters *monster= new monsters();
  81. monster->setPos(monster->getPostionX(), monster->getPostionY());
  82. myMap->getScene()->addItem(monster);
  83.  
  84. }
  85.  
  86. getPositionX and getPositionY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement