No Description

mainwindow.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QDebug>
  4. ///
  5. /// Default Constructor for the MainWindow. Some properties:
  6. /// * ui: Allows the user to access members of the ui
  7. /// * scene: Creates a new scene where the game will be "painted"
  8. /// * game: Creates a new game
  9. /// * globalTimer: Creates a new timer
  10. /// * score: Getes the number of the score in a string for future use
  11. ///
  12. MainWindow::MainWindow(QWidget *parent) :
  13. QMainWindow(parent),
  14. ui(new Ui::MainWindow)
  15. {
  16. /*
  17. * Sets the ui and the scene for future use.
  18. */
  19. ui->setupUi(this);
  20. scene= new QGraphicsScene(this);
  21. scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
  22. /*
  23. * Places the scene in the main view and then we move mainView
  24. * so that it is centralized
  25. */
  26. ui->mainView->setScene(scene);
  27. ui->mainView->setAlignment((Qt::AlignLeft | Qt::AlignTop));
  28. ui->mainView->resize(550,345);
  29. /*
  30. * Here we create a new game and set the racer to red, also
  31. * we create a timer and add the game to the scene
  32. */
  33. game = new play;
  34. game->setCar("red");
  35. globalTimer = new QTimer;
  36. scene->addWidget(game);
  37. /*
  38. * Get the score and store it as a string, then we just
  39. * move around the buttons and the labels to make the
  40. * ui more aesthetically pleasing
  41. */
  42. QString score = QString::number(game->getScore());
  43. ui->scoreLabel->setText("<h3><b>Score: </b></h3>");
  44. ui->label->setText(score);
  45. ui->racerLabel->setText("Racer: ");
  46. ui->trackLabel->setText("Track: ");
  47. ui->difficultyLabel->setText("Difficulty: ");
  48. ui->retryButton->setEnabled(false);
  49. setWindowTitle("Scrolling Racing Game");
  50. }
  51. ///
  52. /// Default destructor
  53. ///
  54. MainWindow::~MainWindow()
  55. {
  56. delete ui;
  57. delete scene;
  58. }
  59. ///
  60. /// Starts the timer for the score and plays the game
  61. /// using the run function from the game class
  62. ///
  63. void MainWindow::on_playButton_clicked()
  64. {
  65. game->run();
  66. connect(globalTimer,SIGNAL(timeout()),this,SLOT(score()));
  67. globalTimer->start(120);
  68. ui->playButton->setEnabled(false);
  69. }
  70. ///
  71. /// Sets the score of the game which is based on the timer
  72. ///
  73. void MainWindow::score()
  74. {
  75. if(game->playing == false) ui->retryButton->setEnabled(true);
  76. else ui->retryButton->setEnabled(false);
  77. QString num = QString::number(game->getScore());
  78. ui->label->setText(num);
  79. }
  80. ///
  81. /// When the retry button is pressed, the timer is
  82. /// deleted and a new one is created also runs the
  83. /// new game function from the class game.
  84. ///
  85. void MainWindow::on_retryButton_clicked()
  86. {
  87. game->newGame();
  88. delete globalTimer;
  89. globalTimer = new QTimer;
  90. connect(globalTimer,SIGNAL(timeout()),this,SLOT(score()));
  91. globalTimer->start(120);
  92. }
  93. ///
  94. /// Moves the racer up and down when the up, down arrows
  95. /// are pressed
  96. ///
  97. void MainWindow::keyPressEvent(QKeyEvent *event){
  98. game->keyPressEvent(event);
  99. }
  100. ///
  101. /// Sets which race car the player is using
  102. ///
  103. void MainWindow::on_comboBox_activated(const QString &arg1)
  104. {
  105. game->setCar(arg1.toStdString());
  106. repaint();
  107. }
  108. ///
  109. /// Sets which track the player is racing on
  110. ///
  111. void MainWindow::on_trackBox_activated(const QString &arg1)
  112. {
  113. play::Track track_type ;
  114. if(arg1 == "Day"){
  115. track_type = play::DAY ;
  116. }
  117. else if(arg1 == "Night"){
  118. track_type = play::NIGHT ;
  119. }
  120. else if(arg1 == "Beach"){
  121. track_type = play::BEACH ;
  122. }
  123. else{
  124. track_type = play::CANDYLAND ;
  125. }
  126. game->setTrack(track_type);
  127. repaint();
  128. }
  129. ///
  130. /// Sets the difficulty of the game and changes some
  131. /// boolean variables acoordingly
  132. ///
  133. void MainWindow::on_difficultyBox_activated(const QString &arg1)
  134. {
  135. game->level = play::EASY ;
  136. if(arg1 == "Easy"){
  137. game->level = play::EASY;
  138. }
  139. else if(arg1 == "Medium"){
  140. game->level = play::MEDIUM;
  141. }
  142. else if(arg1 == "Hard"){
  143. game->level = play::HARD;
  144. }
  145. }