MixedView/mainwindow.cpp
changeset 8 a593fb7f78c0
parent 6 4c7de3e5789a
equal deleted inserted replaced
7:99c3932ca5e6 8:a593fb7f78c0
     8 #include <QResizeEvent>
     8 #include <QResizeEvent>
     9 #include <QFile>
     9 #include <QFile>
    10 #include <QStringList>
    10 #include <QStringList>
    11 #include <QSound>
    11 #include <QSound>
    12 
    12 
       
    13 // define the borders of the image
       
    14 #define LEFT_EDGE -103
       
    15 #define RIGHT_EDGE 180
       
    16 #define TOP_EDGE -156
       
    17 #define BOTTOM_EDGE 145
       
    18 // The number of Hits is always at least 2
       
    19 // since there is a visible and invisible image
       
    20 // on top of each other at the same coordinates
       
    21 #define HITS 2
    13 
    22 
    14 MainWindow::MainWindow(QWidget *parent) :
    23 MainWindow::MainWindow(QWidget *parent) :
    15     QMainWindow(parent),
    24     QMainWindow(parent),
    16     ui(new Ui::MainWindow)
    25     ui(new Ui::MainWindow)
    17 {
    26 {
    50     mPacman = new QGraphicsEllipseItem(0, 0, 18, 18);
    59     mPacman = new QGraphicsEllipseItem(0, 0, 18, 18);
    51     mPacman->setBrush(QBrush(Qt::yellow));
    60     mPacman->setBrush(QBrush(Qt::yellow));
    52     mPacman->setSpanAngle(4500);
    61     mPacman->setSpanAngle(4500);
    53     mScene->addItem(mPacman);
    62     mScene->addItem(mPacman);
    54 
    63 
    55 
       
    56 
       
    57     // lets place a pause button in the view
    64     // lets place a pause button in the view
    58     pauseButton = new QPushButton("Pause");
    65     pauseButton = new QPushButton("Pause");
    59     connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
    66     connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
    60     pauseButton->setMaximumHeight(40);
    67     pauseButton->setMaximumHeight(40);
    61     QGraphicsProxyWidget *buttonProxy = mScene->addWidget(pauseButton);
    68     QGraphicsProxyWidget *buttonProxy = mScene->addWidget(pauseButton);
    83 }
    90 }
    84 
    91 
    85 void MainWindow::updateGraphics() {
    92 void MainWindow::updateGraphics() {
    86     if (mPause)
    93     if (mPause)
    87         return;
    94         return;
    88 
       
    89 
       
    90     // update the pacman mouth state
    95     // update the pacman mouth state
    91     // will eventually change this to a graphic
    96     // will eventually change this to a graphic
    92     switch (mPacState) {
    97     switch (mPacState) {
    93     case 0:
    98     case 0:
    94         mPacman->setSpanAngle(5700);
    99         mPacman->setSpanAngle(5700);
   117     xAxis = (xAxis + mAccelerometer->reading()->x()) / 2;
   122     xAxis = (xAxis + mAccelerometer->reading()->x()) / 2;
   118     yAxis = (yAxis + mAccelerometer->reading()->y()) / 2;
   123     yAxis = (yAxis + mAccelerometer->reading()->y()) / 2;
   119 
   124 
   120 }
   125 }
   121 
   126 
       
   127 
   122 void MainWindow::checkCollisions() {
   128 void MainWindow::checkCollisions() {
   123 
   129 
       
   130 
   124     mNumHits = mPacCollider->collidingItems().count();
   131     mNumHits = mPacCollider->collidingItems().count();
   125     if (mNumHits == 2)
   132     if (mNumHits == HITS)
   126         mLastPt = mPacCollider->pos();
   133         mLastPt = mPacCollider->pos();
   127 
   134 
   128     QPointF pt =  mPacCollider->pos();
   135     QPointF pt =  mPacCollider->pos();
   129 
   136 
   130     pt.setX(pt.x()-xAxis);
   137     pt.setX(pt.x()-xAxis);
   131     pt.setY(pt.y()+yAxis);
   138     pt.setY(pt.y()+yAxis);
   132 
   139 
   133     if (pt.x()<-103)
   140     if (pt.x()< LEFT_EDGE)
   134         pt.setX(-103);
   141         pt.setX(LEFT_EDGE);
   135     else if (pt.x()>180)
   142     else if (pt.x()>RIGHT_EDGE)
   136         pt.setX(180);
   143         pt.setX(RIGHT_EDGE);
   137 
   144 
   138     if (pt.y()<-156)
   145     if (pt.y()<TOP_EDGE)
   139         pt.setY(-156);
   146         pt.setY(TOP_EDGE);
   140     else if (pt.y()>145)
   147     else if (pt.y()>BOTTOM_EDGE)
   141         pt.setY(145);
   148         pt.setY(BOTTOM_EDGE);
   142 
   149 
   143     if (mNumHits == 2) {
   150     if (mNumHits == HITS) {
   144         mPacCollider->setPos(pt);
   151         mPacCollider->setPos(pt);
   145        // mPacman->setPos(pt);
       
   146     }
   152     }
   147     else {
   153     else {
   148         mPacCollider->setPos(mLastPt);
   154         mPacCollider->setPos(mLastPt);
   149 
   155 
   150     }
   156     }