daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Sep 25th, 2015 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This class controls the whole game, the scene and all other classes found in the game. It also is in charge of displaying data from
  3.  * the other classes on the screen.
  4.  */
  5.  
  6. #include "game.h"
  7. #include "plane.h"
  8. #include "engine.h"
  9.  
  10. #include <QGraphicsPixmapItem>
  11. #include <QFont>
  12. #include <cstdlib>
  13. #include <time.h>
  14.  
  15. int screenWidth = 1280;
  16. int screenHeight = 720;
  17. int sceneWidth = 5000;
  18. int welcomeScreenPick = 0;
  19. double viewX = 0; // will calculate distance to view border
  20.  
  21. extern double airspeed;
  22. extern int planeAngle;
  23. extern double RC;
  24. extern double altitude;
  25. extern int flapAngle;
  26. extern int spoiler;
  27. extern bool brakes;
  28. extern bool crash;
  29. extern bool succesfulLanding;
  30. extern Game *game;
  31.  
  32. Game::Game()
  33. {
  34.     srand(time(0));
  35.     welcomeScreenPick = rand() % 2;
  36.     scene = new QGraphicsScene();
  37.     scene->setSceneRect(0,0,screenWidth, screenHeight);
  38.  
  39.     playButton = new QPushButton("Play", this);
  40.     playButton->show();
  41.     controlsButton = new QPushButton("Controls", this);
  42.     controlsButton->show();
  43.     exitButton = new QPushButton("Exit", this);
  44.     exitButton->show();
  45.  
  46.     timer = new QTimer();
  47.     connect(timer, SIGNAL(timeout()), this, SLOT(drawBackground(QPainter*,QRectF)));
  48.     timer->start(1000);
  49.  
  50.     connect(playButton, SIGNAL(released()), this, SLOT(play()));
  51.     connect(controlsButton, SIGNAL(released()), this, SLOT(controls()));
  52.     connect(exitButton, SIGNAL(released()), this, SLOT(exit()));
  53.  
  54.     setFixedSize(screenWidth, screenHeight);
  55.     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  56.     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  57.     setScene(scene);
  58. }
  59.  
  60. void Game::drawBackground(QPainter *painter, const QRectF &rect){
  61.  
  62.  
  63.     if(welcomeScreenPick == 0){
  64.         painter->drawPixmap(0,0, screenWidth, screenHeight, QPixmap(":/Graphics/Graphics/MainMenuBackground1"));
  65.         playButton->setGeometry(screenWidth /2 - 37.5, screenHeight /2 -60, 75, 30);
  66.         controlsButton->setGeometry(screenWidth /2 - 37.5, screenHeight /2 -25, 75, 30);
  67.         exitButton->setGeometry(screenWidth /2 - 37.5, screenHeight /2 +10, 75, 30);
  68.     }
  69.     if(welcomeScreenPick == 1){
  70.         painter->drawPixmap(0,0, screenWidth, screenHeight, QPixmap(":/Graphics/Graphics/MainMenuBackground2"));
  71.         playButton->setGeometry(screenWidth /2 -150, screenHeight /2 -60, 75, 30);
  72.         controlsButton->setGeometry(screenWidth /2 -150, screenHeight /2 -25, 75, 30);
  73.         exitButton->setGeometry(screenWidth /2 -150, screenHeight /2 +10, 75, 30);
  74.     }
  75.  
  76.  
  77. }
  78.  
  79. void Game::updateText()
  80. {
  81.     viewX = plane->pos().x() - 600;
  82.     airspeedText->setPlainText(QString("Airspeed: ") + QString::number(airspeed));
  83.     if(viewX <= 0){airspeedText->setPos(0,2);}
  84.     else if(viewX > 0 && viewX < 3725){airspeedText->setPos(viewX, 2);}
  85.     else if(viewX >= 3720){airspeedText->setPos(3725, 2);}
  86.  
  87.     planeAngleText->setPlainText(QString("Pitch: ") + QString::number(-planeAngle) + QString("ยบ"));
  88.     if(viewX <= 0){planeAngleText->setPos(0, 18);}
  89.     else if(viewX > 0 && viewX < 3725){planeAngleText->setPos(viewX, 18);}
  90.     else if(viewX >= 3720){planeAngleText->setPos(3725, 18);}
  91.  
  92.     RCtext->setPlainText((QString("Rate Of Climb: ") + QString::number(-RC)));
  93.     if(viewX <= 0){RCtext->setPos(0,34);}
  94.     else if(viewX > 0 && viewX < 3725){RCtext->setPos(viewX, 34);}
  95.     else if(viewX >= 3720){RCtext->setPos(3725, 34);}
  96.  
  97.     altitudeText->setPlainText((QString("Altitude: ") + QString::number(screenHeight * 27.7 - altitude *27.7 -3000)));
  98.     if(viewX <= 0){altitudeText->setPos(0,50);}
  99.     else if(viewX > 0 && viewX < 3725){altitudeText->setPos(viewX, 50);}
  100.     else if(viewX >= 3720){altitudeText->setPos(3725, 50);}
  101.  
  102.     flapAngleText->setPlainText((QString("Flap Angle: ") + QString::number(flapAngle)));
  103.     if(viewX <= 0){flapAngleText->setPos(0,66);}
  104.     else if(viewX > 0 && viewX < 3725){flapAngleText->setPos(viewX, 66);}
  105.     else if(viewX >= 3720){flapAngleText->setPos(3725, 66);}
  106.  
  107.     if(spoiler == 0){spoilerPosText->setPlainText("Spoilers: Unarmed");}
  108.     else if(spoiler == 1){spoilerPosText->setPlainText("Spoilers: Flight");}
  109.     else if(spoiler == 2){spoilerPosText->setPlainText("Spoilers: Armed");}
  110.     if(viewX <= 0){spoilerPosText->setPos(0,82);}
  111.     else if(viewX > 0 && viewX < 3725){spoilerPosText->setPos(viewX, 82);}
  112.     else if(viewX >= 3720){spoilerPosText->setPos(3725, 82);}
  113.  
  114.     if(brakes == false){brakesText->setPlainText("Brakes: Off");}
  115.     if(brakes == true){brakesText->setPlainText("Brakes: On");}
  116.     if(viewX <= 0){brakesText->setPos(0,98);}
  117.     else if(viewX > 0 && viewX < 3725){brakesText->setPos(viewX, 98);}
  118.     else if(viewX >= 3720){brakesText->setPos(3725, 98);}
  119.  
  120.     if(succesfulLanding == true || crash == true){
  121.         if(succesfulLanding == true){
  122.         youWonText->setPlainText("You landed succesfuly!");
  123.         youWonText->setPos(4100, 300);
  124.         scene->addItem(youWonText);
  125.         }
  126.         if(crash == true){
  127.             scene->removeItem(plane);
  128.             youLostText->setPlainText("You crashed the airplane");
  129.             if(viewX <= 0){youLostText->setPos(350, 300);}
  130.             if(viewX > 0 && viewX < 3725){youLostText->setPos(viewX + 350, 300);}
  131.             if(viewX >= 3720){youLostText->setPos(4100, 300);}
  132.             scene->addItem(youLostText);
  133.         }
  134.  
  135.         updateTextTimer->stop();
  136.  
  137.         scene->removeItem(airspeedText);
  138.         scene->removeItem(RCtext);
  139.         scene->removeItem(planeAngleText);
  140.         scene->removeItem(altitudeText);
  141.         scene->removeItem(flapAngleText);
  142.         scene->removeItem(spoilerPosText);
  143.         scene->removeItem(brakesText);
  144.  
  145.         delete airspeedText;
  146.         delete RCtext;
  147.         delete planeAngleText;
  148.         delete altitudeText;
  149.         delete flapAngleText;
  150.         delete spoilerPosText;
  151.         delete brakesText;
  152.  
  153.         replayButton->show();
  154.         exitButton->show();
  155.  
  156.         return;
  157.     }
  158. }
  159.  
  160. void Game::play()
  161. {
  162.     playButton->hide();
  163.     controlsButton->hide();
  164.     exitButton->hide();
  165.  
  166.     scene->setSceneRect(0,0,sceneWidth, screenHeight);
  167.     landingStrip = new QGraphicsPixmapItem();
  168.     landingStrip->setPixmap(QPixmap(":/Graphics/Graphics/Runway.png"));
  169.     landingStrip->setPos(sceneWidth - 700, screenHeight -315 );
  170.     scene->addItem(landingStrip);
  171.  
  172.     engine = new Engine();
  173.  
  174.     plane = new Plane();
  175.     scene->addItem(plane);
  176.  
  177.  
  178.     //adding all relevant data to screen
  179.  
  180.     airspeedText = new QGraphicsTextItem();
  181.     airspeedText->setFont(QFont("times", 12));
  182.     scene->addItem(airspeedText);
  183.  
  184.     planeAngleText = new QGraphicsTextItem();
  185.     planeAngleText->setFont(QFont("times", 12));
  186.     scene->addItem(planeAngleText);
  187.  
  188.     RCtext = new QGraphicsTextItem();
  189.     RCtext->setFont(QFont("times", 12));
  190.     scene->addItem(RCtext);
  191.  
  192.     altitudeText = new QGraphicsTextItem();
  193.     altitudeText->setFont(QFont("times", 12));
  194.     scene->addItem(altitudeText);
  195.  
  196.     flapAngleText = new QGraphicsTextItem();
  197.     flapAngleText->setFont(QFont("times", 12));
  198.     scene->addItem(flapAngleText);
  199.  
  200.     spoilerPosText = new QGraphicsTextItem();
  201.     spoilerPosText->setFont(QFont("times", 12));
  202.     scene->addItem(spoilerPosText);
  203.  
  204.     brakesText = new QGraphicsTextItem();
  205.     brakesText->setFont(QFont("times", 12));
  206.     scene->addItem(brakesText);
  207.  
  208.     youLostText = new QGraphicsTextItem();
  209.     youLostText->setFont(QFont("times", 40));
  210.  
  211.     youWonText = new QGraphicsTextItem();
  212.     youWonText->setFont(QFont("times", 40));
  213.  
  214.     updateTextTimer = new QTimer();
  215.     connect(updateTextTimer, SIGNAL(timeout()), this, SLOT(updateText()));
  216.     updateTextTimer->start(1000/30);
  217.  
  218.     replayButton = new QPushButton("Replay", this);
  219.     replayButton->setGeometry(QRect(QPoint(pos().x() + 450, 400), QSize(50,25)));
  220.     connect(replayButton, SIGNAL(released()), this, SLOT(replay()));
  221.     replayButton->hide();
  222.  
  223.     //created at top for main menu
  224.     exitButton->setGeometry(QRect(QPoint(pos().x() + 700, 400), QSize(50,25)));
  225.     //connect(exitButton, SIGNAL(released()), this, SLOT(exit()));
  226.     exitButton->hide();
  227.  
  228.     show();
  229. }
  230.  
  231. void Game::controls()
  232. {
  233.  
  234. }
  235.  
  236. void Game::replay()
  237. {
  238.     game->close();
  239.  
  240.     delete plane;
  241.     delete engine;
  242.     scene->removeItem(landingStrip);
  243.     delete landingStrip;
  244.     delete exitButton;
  245.     delete replayButton;
  246.     scene->removeItem(youLostText);
  247.     delete youLostText;
  248.     delete scene;
  249.  
  250.     crash = false;
  251.  
  252.     game = new Game();
  253.     game->show();
  254. }
  255.  
  256. void Game::exit()
  257. {
  258.     delete plane;
  259.     scene->removeItem(landingStrip);
  260.     delete landingStrip;
  261.     delete engine;
  262.     delete exitButton;
  263.     delete replayButton;
  264.     scene->removeItem(youLostText);
  265.     delete youLostText;
  266.     delete scene;
  267.     exit();
  268. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top