Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. class MovingObject : QObject
  2. {
  3.     Q_OBJECT
  4. public:
  5.     int _animationStep;
  6.     int _moveSpeed;
  7.  
  8.     int _moving;
  9.     int _stop;
  10.  
  11.     int _moveLeft;
  12.     int _moveUp;
  13.     int _moveRight;
  14.     int _moveDown;
  15.  
  16.     int _currentImage;
  17.     int _moveCounter;
  18.  
  19.     int _state;
  20.  
  21.     double _x;
  22.     double _y;
  23.  
  24.     double _imageX;
  25.     double _imageY;
  26.  
  27.     double _xDirection;
  28.     double _yDirection;
  29.  
  30.     QTimeLine *_timeLine;
  31.  
  32.     MovingObject()
  33.     {
  34.         _animationStep = 4;
  35.         _moveSpeed = MazeData()._gridGap/_animationStep;
  36.         _moving = 1;
  37.         _stop = 0;
  38.  
  39.         _moveLeft = 0;
  40.         _moveUp = 1;
  41.         _moveRight = 2;
  42.         _moveDown = 3;
  43.  
  44.         _currentImage = 0;
  45.         _moveCounter = 0;
  46.  
  47.         _imageX = 0;
  48.         _imageY = 0;
  49.  
  50.         _xDirection = 0;
  51.         _yDirection = 0;
  52.     }
  53.  
  54.     void Stop()
  55.     {
  56.         _timeLine->stop();
  57.     }
  58.  
  59.     void Pause()
  60.     {
  61.         _timeLine->setPaused(true);
  62.     }
  63.  
  64.     void Start()
  65.     {
  66.         _timeLine->start();
  67.     }
  68.  
  69.     void CreateTimeLine()
  70.     {
  71.         _timeLine->setLoopCount(-1);
  72.         _timeLine->setUpdateInterval(250);
  73.         _timeLine->setFrameRange(0, 3);
  74.         connect(_timeLine, SIGNAL(frameChanged(int)), this, SLOT(MoveOneStep()));
  75.     }
  76.  
  77.     ~MovingObject()
  78.     {
  79.         delete _timeLine;
  80.     }
  81.  
  82. public slots:
  83.     virtual void MoveOneStep()
  84.     {
  85.  
  86.     }
  87. };
  88.  
  89. class Pacman : public QGraphicsObject, public MovingObject
  90. {
  91.     Q_OBJECT
  92. public:
  93.     QPixmap *_defaultImage;
  94.     QPixmap *_secondImage;
  95.     QPixmap *_roundImage;
  96.     QList<QPixmap*> _images;
  97.  
  98.     Pacman(double x, double y)
  99.         : MovingObject()
  100.     {
  101.         _imageX = MazeData().CalcGridX(x);
  102.         _imageY = MazeData().CalcGridY(y);
  103.         _defaultImage = new QPixmap(":/images/left1.png");
  104.         _secondImage = new QPixmap(":/images/left2.png");
  105.         _roundImage = new QPixmap(":/images/round.png");
  106.  
  107.         _images.push_back(_defaultImage);
  108.         _images.push_back(_secondImage);
  109.         _images.push_back(_defaultImage);
  110.         _images.push_back(_roundImage);
  111.  
  112.         QGraphicsPixmapItem *pacmanFrame = new QGraphicsPixmapItem;
  113.         pacmanFrame->setParentItem(this);
  114.         pacmanFrame->setPos(_imageX - 13, _imageY - 13);
  115.         pacmanFrame->setPixmap(*_images.at(_currentImage));
  116.     }
  117. public slots:
  118.     void MoveOneStep()
  119.     {
  120.         if(_state == _moving)
  121.         {
  122.             if(_currentImage < _animationStep - 1) _currentImage++;
  123.             else _currentImage = 0;
  124.         }
  125.     }
  126. public:
  127.     ~Pacman()
  128.     {
  129.         delete _defaultImage;
  130.         delete _secondImage;
  131.         delete _roundImage;
  132.     }
  133.  
  134.     QRectF boundingRect() const
  135.     {
  136.         return QRectF();
  137.     }
  138.  
  139.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  140.     {
  141.         Q_UNUSED(painter);
  142.         Q_UNUSED(option);
  143.         Q_UNUSED(widget);
  144.     }
  145.  
  146. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement