No Description

genericscrollingobject.cpp 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "genericscrollingobject.h"
  2. #include "maingamewindow.h"
  3. #include <QDebug>
  4. //
  5. // A class for the items that move.
  6. GenericScrollingObject::GenericScrollingObject(const std::string& filename, QGraphicsScene *scene,
  7. int x, int y, int w, int h,
  8. const QColor& transparency_color ,
  9. MainGameWindow *parent
  10. )
  11. {
  12. scene->addItem(this);
  13. myScene = scene;
  14. alive = true;
  15. // create a pixmap with invisible background
  16. QPixmap pixmap(filename.c_str());
  17. pixmap = pixmap.scaled(w,h);
  18. const QBitmap mask = pixmap.createMaskFromColor(transparency_color);
  19. pixmap.setMask(mask);
  20. this->setPixmap(pixmap);
  21. // set position and speed
  22. myX = x;
  23. myY = y;
  24. toX = toY = 0;
  25. myDirY = 0;
  26. myDirX = 0;
  27. myParent = parent;
  28. setPos(myX,myY);
  29. }
  30. //
  31. // This method that is automatically called on each tick
  32. //
  33. void GenericScrollingObject::advance(int) {
  34. myX = myX + myDirX;
  35. myY = myY + myDirY;
  36. if (myDirX != 0 && abs(myX-toX) < 10) { myDirX = 0; myX = toX;}
  37. if (myDirY != 0 && abs(myY-toY) < 10) { myDirY = 0; myY = toY;}
  38. this->setPos(myX,myY);
  39. }
  40. void GenericScrollingObject::kill() {
  41. alive = false;
  42. // Show the explosion, instead of the robot
  43. QPixmap pixmap(":explosion.png");
  44. pixmap = pixmap.scaled(boundingRect().width(),boundingRect().height(),Qt::IgnoreAspectRatio,Qt::FastTransformation);
  45. const QBitmap mask = pixmap.createMaskFromColor(QColor(Qt::black));
  46. pixmap.setMask(mask);
  47. setPixmap(pixmap);
  48. }