Nessuna descrizione

play.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "play.h"
  2. #include <QDebug>
  3. play::play(QWidget *parent) :
  4. QWidget(parent)
  5. {
  6. // Set the background (track) variables and coords.
  7. x1=0;
  8. x2=600;
  9. setTrack(play::DAY);
  10. repaint();
  11. // set player score to 0
  12. score=0;
  13. // Set the obstacles to cones
  14. for(int i = 0; i < 3; i++){
  15. obs[i].setObs("cone") ;
  16. }
  17. // Set Car to red car by default.
  18. car.setCar("red");
  19. // Set level to EASY
  20. level = EASY;
  21. // Obstacles that will be displayed if level is changed.
  22. obs[MEDIUM].setYObstacle(145);
  23. obs[HARD].setYObstacle(100);
  24. playing = false;
  25. // Set timer event to move the background
  26. myTimer= new QTimer(this);
  27. }
  28. play::~play(){
  29. delete myTimer;
  30. }
  31. void play::setCar(string arg){
  32. car.setCar(arg);
  33. }
  34. play::Track play::getTrack(){
  35. return tt;
  36. }
  37. void play::paintEvent(QPaintEvent *){
  38. QPainter p(this);
  39. QString scar = QString::fromStdString(car.getCar());
  40. QString check = QString::fromStdString(flag.name);
  41. QString road = QString::fromStdString(track);
  42. QString cone = QString::fromStdString(obs[0].getObs());
  43. // Check if there is a collision every time the car is moved
  44. collision();
  45. // Check if there is a collision with the flag
  46. flagCollision(flag, car);
  47. // Draws the track
  48. p.drawPixmap(x1,0,600,400,QPixmap(road));
  49. p.drawPixmap(x2,0,610,400,QPixmap(road));
  50. // Draws the obstacles.
  51. for(int i = 0; i <= level; i++){
  52. p.drawPixmap(obs[i].getXObstacle(),obs[i].getYObstacle() + i,35,35,QPixmap(cone));
  53. }
  54. //Draws the flag
  55. p.drawPixmap(flag.getXFlag(),flag.getYFlag(),50,50,QPixmap(check));
  56. // Draws the Car
  57. p.drawPixmap(10,car.getYCar(),80,50,QPixmap(scar));
  58. //Updates the obstacles always that there is a new scene
  59. if (x1 < -600){
  60. updateObs();
  61. x1 = 599;
  62. }
  63. // Same as above but with new background.
  64. if(x2 < -600){
  65. updateObs();
  66. x2 = 599;
  67. }
  68. // increase player score.
  69. setScore();
  70. }
  71. int play::randInt(int min, int max){
  72. return qrand() % ((max +1 )- min) + min ;
  73. }
  74. // Whenever there is collision with an obstacle the game
  75. // is stoped.
  76. void play::stopGame(){
  77. myTimer->stop();
  78. }
  79. // Function to increase the player score.
  80. void play::setScore(int n){
  81. score += n ;
  82. }
  83. // Returns the score.
  84. int play::getScore(){
  85. return score;
  86. }
  87. // Function called everytime a timer event ocurrs.
  88. // It repaints the graphics of the game.
  89. void play::mySlot(){
  90. //moves road, obstacles, flags
  91. x1 = x1-STEPS;
  92. x2 = x2-STEPS;
  93. for(int i = 0; i <= level; i++){
  94. obs[i].setXObstacle(obs[i].getXObstacle() - STEPS);
  95. }
  96. flag.setXFlag(flag.getXFlag()-STEPS);
  97. repaint();
  98. }
  99. // Restart the timer
  100. void play::run(){
  101. playing = true;
  102. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  103. myTimer->start(16);
  104. }
  105. // Resets the game to its initial state if the game is reset.
  106. void play::newGame(){
  107. x1 = 0;
  108. x2 = 600;
  109. score=0;
  110. myTimer = new QTimer(this);
  111. setTrack(getTrack());
  112. //moves road, obstacles, flags
  113. x1 = x1-STEPS;
  114. x2 = x2-STEPS;
  115. for(int i = 0; i <= level; i++){
  116. obs[i].setXObstacle(obs[i].getXObstacle() - STEPS);
  117. }
  118. flag.setXFlag(flag.getXFlag()-STEPS);
  119. repaint();
  120. run();
  121. }
  122. // Sets the images of the obstacles.
  123. void play::setObstaclesPixmap(string pixmap){
  124. for (int i = 0; i < 3; i++){
  125. obs[i].setObs(pixmap) ;
  126. }
  127. }
  128. // Event for the controls of the car.
  129. void play::keyPressEvent(QKeyEvent *event){
  130. if((event->key() == Qt::Key_Up) && car.getYCar() > 80){
  131. car.setYCar(car.getYCar()-30);
  132. }
  133. if((event->key() == Qt::Key_Down) && car.getYCar() < 280){
  134. car.setYCar(car.getYCar()+30);
  135. }
  136. }
  137. // Updates the obstacles position. Called from the Paint function when
  138. // the trac is finished.
  139. void play::updateObs(){
  140. int obstacles[4] ;
  141. int shuffle_start ;
  142. int xflag = x1 ;
  143. int xobs = x1;
  144. updateObstacleYCoordinate(obstacles[0], obstacles[1], obstacles[2], obstacles[3]);
  145. if (x1 < 600){
  146. xflag = 600;
  147. xobs = 550;
  148. }
  149. if( x1 < 600 || x2 < 600){
  150. shuffle_start = randInt(0,3) ;
  151. flag.setYFlag(obstacles[shuffle_start]);
  152. flag.setXFlag(xflag);
  153. for(int i = 0; i < 3; i++){
  154. obs[i].setYObstacle(obstacles[(shuffle_start + i + 1) % 4]);
  155. obs[i].setXObstacle(xobs);
  156. }
  157. }
  158. }
  159. // Function to detect collisions with the obstacles.
  160. void play::collision(){
  161. // There will be at least obstacle no matter the
  162. // game level, check collision with first obstacle
  163. if(obstacleCollision(obs[0], car)){
  164. //qDebug() << "Collision!!" ;
  165. stopGame();
  166. playing = false ;
  167. }
  168. // If there is no collision with first obstacle, but
  169. // there is a second obstacle, check collision
  170. else if(level > EASY && obstacleCollision(obs[1], car)){
  171. //qDebug() << "Collision!!" ;
  172. stopGame();
  173. playing = false ;
  174. }
  175. //If there is no collision with the previous 2 obstacles,
  176. //but there is a third obstacle, check collision
  177. else if(level == HARD && obstacleCollision(obs[2], car)){
  178. //qDebug() << "Collision!!" ;
  179. stopGame();
  180. playing = false ;
  181. }
  182. }
  183. void play::updateObstacleYCoordinate(int &obj1, int &obj2, int &obj3, int &obj4){
  184. // The game presents 3 bad obstacle and one safe obstacle every x distance.
  185. // In this function you will compute the Y coordinate of such obstacles.
  186. // You need make sure that no two obstacle overlap each other.
  187. // Each obstacle is 40px of height, the top coordinate of the racetrack is
  188. // 80px and the bottom coordinate of the racetrack is 280.
  189. // Hint randInt(min, max)
  190. obj1 = randInt(80, 245 - 40*4) ;
  191. obj2 = randInt(obj1 + 40, 245 - 40 * 3) ;
  192. obj3 = randInt(obj2 + 40, 245 - 40 * 2) ;
  193. obj4 = randInt(obj3 + 40, 245) ;
  194. //qDebug() << obj1 << obj2 << obj3 << obj4 ;
  195. }
  196. void play::setTrackPixmap(Track track_type){
  197. tt = track_type ;
  198. switch(track_type){
  199. case play::DAY:
  200. //All background picture follow the same path but with different names e.g. ":/resources/blahblah.png"
  201. track = ":/resources/coolroad.png" ;
  202. break ;
  203. case play::NIGHT:
  204. track = ":/resources/roadnight.png";
  205. break ;
  206. case play::BEACH:
  207. track = ":/resources/roadsand.png";
  208. break ;
  209. case play::CANDYLAND:
  210. track = ":/resources/candyroad.png";
  211. break ;
  212. } ;
  213. }
  214. string play::getTrackPixmap(){
  215. return track ;
  216. }