Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. class MovingObject : public QGraphicsObject
  2. {
  3.  
  4. public:
  5.  
  6.     int _animationStep;
  7.     int _moveSpeed;
  8.  
  9.     int _moving;
  10.     int _stop;
  11.  
  12.     int _moveLeft;
  13.     int _moveUp;
  14.     int _moveRight;
  15.     int _moveDown;
  16.  
  17.     int _currentImage;
  18.     int _moveCounter;
  19.  
  20.     int _state;
  21.  
  22.     double _x;
  23.     double _y;
  24.  
  25.     double _imageX;
  26.     double _imageY;
  27.  
  28.     double _xDirection;
  29.     double _yDirection;
  30.  
  31.     MovingObject(QGraphicsObject *parent = 0)
  32.     {
  33.         _animationStep = 4;
  34.         _moveSpeed = MazeData()._gridGap/_animationStep;
  35.         _moving = 1;
  36.         _stop = 0;
  37.  
  38.         _moveLeft = 0;
  39.         _moveUp = 1;
  40.         _moveRight = 2;
  41.         _moveDown = 3;
  42.  
  43.         _currentImage = 0;
  44.         _moveCounter = 0;
  45.  
  46.         _state = 1;
  47.         _imageX = 0;
  48.         _imageY = 0;
  49.  
  50.         _xDirection = 0;
  51.         _yDirection = 0;
  52.  
  53.     }
  54.  
  55.  
  56.  
  57. };
  58.  
  59. class Pacman : public MovingObject
  60. {
  61.     Q_OBJECT
  62. public:
  63.     QList<QPixmap*> _images;
  64.     QGraphicsPixmapItem *_pacmanFrame;
  65.  
  66.     Pacman(double x, double y)
  67.         : MovingObject(this)
  68.     {
  69.         _imageX = MazeData().CalcGridX(x);
  70.         _imageY = MazeData().CalcGridY(y);
  71.         QPixmap *defaultImage = new QPixmap("images/newLeft1.png");
  72.         QPixmap *secondImage = new QPixmap("images/newLeft2.png");
  73.         QPixmap *roundImage = new QPixmap("images/newRound.png");
  74.  
  75.         _images.push_back(defaultImage);
  76.         _images.push_back(secondImage);
  77.         _images.push_back(defaultImage);
  78.         _images.push_back(roundImage);
  79.  
  80.  
  81.         _pacmanFrame = new QGraphicsPixmapItem;
  82.         _pacmanFrame->setParentItem(this);
  83.         _pacmanFrame->setPos(_imageX - 13, _imageY - 13);
  84.         _pacmanFrame->setPixmap(*defaultImage);
  85.  
  86.         QPropertyAnimation *pacmanMouth = new QPropertyAnimation(this);
  87.         pacmanMouth->setDuration(250);
  88.         pacmanMouth->setLoopCount(-1);
  89.         pacmanMouth->start();
  90.         connect(pacmanMouth, SIGNAL(currentLoopChanged(int)), this, SLOT(SwitchImage()));
  91.     }
  92.  
  93.     ~Pacman()
  94.     {
  95.     }
  96.  
  97.     QRectF boundingRect() const
  98.     {
  99.         return QRectF();
  100.     }
  101.  
  102.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  103.     {
  104.         Q_UNUSED(painter);
  105.         Q_UNUSED(option);
  106.         Q_UNUSED(widget);
  107.     }
  108.     public slot:
  109.     void SwitchImage()
  110.     {
  111.         if(_state == _moving)
  112.         {
  113.             if(_currentImage < _animationStep - 1) _currentImage++;
  114.             else _currentImage = 0;
  115.         }
  116.         _pacmanFrame->setPixmap(*_images.at(_currentImage));
  117.     }
  118.  
  119. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement