Aucune description

frog.cpp 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. ///
  2. /// Modifications:
  3. /// - 2015-06-05 [Ian Davila] - Changed Frogger's location
  4. ///
  5. #include "frog.h"
  6. #include <QDebug>
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include <cassert>
  10. #include <QMessageBox>
  11. #include "mainwindow.h"
  12. #include "ui_mainwindow.h"
  13. /// \fn frog::frog(QWidget *parent)
  14. /// \~English
  15. /// \brief Default constructor.
  16. /// * Ysapo: Y coordinate of the frog, set at 190 so it sits on top of the leaf
  17. /// * Xsapo: X coordinate of the frog, a random number from 0 to -30
  18. /// * xnenu, xnenu2, xnenu3: X coordinates of the leafs
  19. /// * textBool: Boolean for the labels on the leafs
  20. /// * temp, temp2: X coordinate of the background
  21. /// \~Spanish
  22. /// \ brief Constructor por defecto.
  23. /// * Ysapo: Coordenada Y del sapo, inicializado a 190 para que aparesca encima del nenufar
  24. /// * Xsapo: Coordenada X del sapo, un numero aleatorio de 0 a -30
  25. /// * xnenu, xnenu2, xnenu3: coordenadas x de las hojas
  26. /// * textBool: Booleana para las etiquetas de los nenufares
  27. /// * temp, temp2: Coordenada X del fondo
  28. ///
  29. frog::frog(QWidget *parent) :
  30. QWidget(parent)
  31. {
  32. myTimer= new QTimer(this);
  33. flag = xFlag = backFlag = moved = textBool = false;
  34. Ysapo = 190;
  35. A = B = C = 0;
  36. temp = 580;
  37. temp2 = 0;
  38. xnenu = -(rand()%30);
  39. initialX = Xsapo = xnenu;
  40. xnenu2 = (rand() % 100) + 100;
  41. xnenu3 = xnenu2 + (rand() % 100) + 200;
  42. }
  43. /// \fn void frog::updatenenu()
  44. /// \~English
  45. /// \brief Function that updates the position of the frog and the leafs
  46. /// when the frog reaches the end of the screen when it's sliding back.
  47. /// \~Spanish
  48. /// \brief Funcion que actualiza la posicion del sapo y los nenufares
  49. /// cuando el sapo llega al final de la pantalla cuando se desliza hacia atras.
  50. ///
  51. void frog::updatenenu(){
  52. xnenu = -(rand() % 30);
  53. initialX = Xsapo = xnenu;
  54. xnenu2 = (rand() % 100) + 100;
  55. xnenu3 = xnenu2 + (rand() % 100) + 200;
  56. }
  57. /// \fn void frog::reset()
  58. /// \~English
  59. /// \brief Function that is invoked when the retry button is clicked, resets
  60. /// the frog and the leafs to a random starting position.
  61. /// \~Spanish
  62. /// \brief Funcion que es invocada cuando el boton de "retry" es presionado,
  63. /// restablece el sapo y los nenufares a una posicion de partida aleatoria.
  64. ///
  65. void frog::reset(){
  66. flag = xFlag = backFlag = moved = textBool = false;
  67. Ysapo = 190;
  68. A = B = C = 0;
  69. temp = 580;
  70. temp2 = 0;
  71. repaint();
  72. myTimer->stop();
  73. delete myTimer;
  74. myTimer = new QTimer;
  75. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  76. xnenu = -(rand() %30);
  77. initialX = Xsapo = xnenu;
  78. xnenu2 = (rand() % 100) + 100;
  79. xnenu3 = xnenu2 + (rand() % 100) + 200;
  80. }
  81. /// \fn void frog::paintEvent(QPaintEvent *event)
  82. /// \~English
  83. /// \brief Function that is invoked each time the widget or
  84. /// its parent receives a repaint signal.
  85. /// \~Spanish
  86. /// \brief Funcion que es invocada automaticamente cada vez que el widget
  87. /// o su padre recive una senal de repintar.
  88. ///
  89. void frog::paintEvent(QPaintEvent *){
  90. QPainter p(this);
  91. QString frogger = ":/images/Frogger1.png";
  92. QString frogger2 = ":/images/Frogger2.png";
  93. QString frogger3 = ":/images/Frogger3.png";
  94. QString leaf = ":/images/Nenufar1.png";
  95. QString back = ":/images/background.jpg";
  96. //Conditions that allow a margin of error (not landing in the exact x calculated)
  97. if(backFlag == true || (Xsapo <= (xnenu3 + 55) && Xsapo >= (xnenu3 - 55) && Ysapo == 190)
  98. || (Xsapo > xnenu && Ysapo == 190 && Xsapo < (xnenu2-55))
  99. || (Xsapo > xnenu2 && Ysapo == 190 && Xsapo < (xnenu3-55))
  100. || (Xsapo > xnenu3 && Ysapo == 190)){
  101. backFlag = true;
  102. myTimer->stop();
  103. delete myTimer;
  104. myTimer = new QTimer;
  105. QThread::msleep(5);
  106. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  107. // Draw background
  108. p.drawPixmap(temp2,-5,600,400,QPixmap(back));
  109. p.drawPixmap(temp,-5,600,400,QPixmap(back));
  110. // Draw leaves
  111. p.drawPixmap(xnenu,230,125,60,QPixmap(leaf));
  112. p.drawPixmap(xnenu2,230,125,60,QPixmap(leaf));
  113. p.drawPixmap(xnenu3,230,125,60,QPixmap(leaf));
  114. // Draw text labels
  115. p.drawText(xnenu2+45,310,"x1 = " + QString::number(xnenu2));
  116. p.drawText(xnenu3+45,310,"x2 = " + QString::number(xnenu3));
  117. // Moves everything on the screen back on the x-axis
  118. Xsapo=Xsapo-1;
  119. xnenu=xnenu-1;
  120. xnenu2=xnenu2-1;
  121. xnenu3=xnenu3-1;
  122. temp= temp - 1;
  123. temp2 = temp2 -1;
  124. repaint();
  125. // When the frog reaches the end of the screen reset everything to a start position
  126. if(temp == 0 || xnenu3 == -10 || Xsapo == 0){
  127. backFlag = false;
  128. temp = 580;
  129. temp2 = 0;
  130. moved = true;
  131. textBool = false;
  132. updatenenu();
  133. }
  134. }else{
  135. QFont font = p.font();
  136. font.setBold(true);
  137. p.setFont(font);
  138. // Draw background
  139. p.drawPixmap(temp2,-5,600,400,QPixmap(back));
  140. p.drawPixmap(temp,-5,600,400,QPixmap(back));
  141. // Draw leaves
  142. p.drawPixmap(xnenu,230,125,60,QPixmap(leaf));
  143. p.drawPixmap(xnenu2,230,125,60,QPixmap(leaf));
  144. p.drawPixmap(xnenu3,230,125,60,QPixmap(leaf));
  145. //Cambiada la condicion para que sea exacta la caida en el nenufar // Funciona con esta condición
  146. if(Xsapo < (xnenu2 + 55) && Ysapo == 190 && Xsapo > (xnenu2-55)){
  147. textBool = true;
  148. }
  149. // When the frog lands on the second leaf, change labels to next leaf
  150. if(Ysapo == 190 && Xsapo == xnenu2){
  151. textBool = true;
  152. }
  153. if(textBool){
  154. // Redraw the text labels
  155. p.drawText(xnenu2+35,310,"x1 = " + QString::number(xnenu2)); // de 290 a 310
  156. p.drawText(xnenu3+35,310,"x2 = " + QString::number(xnenu3));
  157. p.drawPixmap(xnenu,230,125,60,QPixmap(leaf)); // 250 a 230
  158. }else{
  159. p.drawText(xnenu2+35,310,"x2 = " + QString::number(xnenu2)); // de 290 a 310
  160. }
  161. }
  162. //Cambiada la condicion para que sea exacta la caida en el nenufar
  163. if((Xsapo > (xnenu + 55) && Ysapo == 190 && Xsapo < (xnenu2-55))
  164. || (Xsapo > (xnenu2 + 55) && Ysapo == 190 && Xsapo < (xnenu3-55))
  165. || (Xsapo > (xnenu3 + 55) && Ysapo == 190)){
  166. p.drawPixmap(Xsapo, Ysapo + 30, 100, 80, QPixmap(frogger3));
  167. }else if(Ysapo < 190){
  168. p.drawPixmap(Xsapo,Ysapo,100,100,QPixmap(frogger2));
  169. }else if(Ysapo == 190){
  170. qDebug() << "xsapo: " << Xsapo << "xnenu2: " << xnenu2;
  171. p.drawPixmap(Xsapo + 15,Ysapo - 20 ,100,100,QPixmap(frogger)); //+15 a la Xsapo, -20 a Ysapo
  172. if(!textBool){
  173. p.drawText(xnenu+45,310,"x1 = " + QString::number(xnenu+10));
  174. }
  175. }
  176. }
  177. /// \fun frog::~frog()
  178. /// \~English
  179. /// \brief Default destructor.
  180. /// \~Spanish
  181. /// \brief Destructor por defecto.
  182. ///
  183. frog::~frog(){
  184. delete myTimer;
  185. }
  186. /// \fun void frog::mySlot()
  187. /// \~English
  188. /// \brief Function connected to a timer, used to simulate the
  189. /// frog's jump.
  190. /// \~Spanish
  191. /// \brief Funcion conectada a un reloj, utilizada para simular
  192. /// el brinco del sapo
  193. ///
  194. void frog::mySlot(){
  195. float interX1 = 0;
  196. float interX2 = 0;
  197. float temp = 0;
  198. float y = 0;
  199. float xtemp = 0;
  200. float xEnd = 0;
  201. interX1 = QuadraticMinus(A,B,C);
  202. interX2 = QuadraticPlus(A,B,C);
  203. if(interX1 > interX2){
  204. temp = interX1;
  205. interX1 = interX2;
  206. interX2 = temp;
  207. }
  208. if(moved){
  209. moved = false;
  210. initialX = Xsapo;
  211. }
  212. xEnd = interX2 + initialX;
  213. y = 1;
  214. xtemp = 1;
  215. if(!xFlag){
  216. Ysapo = Ysapo - y;
  217. Xsapo = Xsapo + xtemp;
  218. }
  219. else if(xFlag){
  220. Ysapo = Ysapo + y;
  221. Xsapo = Xsapo + xtemp;
  222. }
  223. if(int(Xsapo) == int(xEnd/2)){
  224. xFlag = true;
  225. }
  226. repaint();
  227. if((int(Xsapo) == int(xEnd) && xFlag == true) || Ysapo == 190){
  228. Ysapo = 190;
  229. repaint();
  230. myTimer->stop();
  231. delete myTimer;
  232. myTimer = new QTimer;
  233. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  234. initialX = Xsapo;
  235. xFlag = false;
  236. }
  237. }
  238. /// \fn void frog::run(int aa, int bb, int cc)
  239. /// \~English
  240. /// \brief Function that is invoked when the jump button is
  241. /// pressed, makes the frog jump to the coordinates calculated
  242. /// with the a, b, c values provided by the user.
  243. /// \param aa value provided by user to complete the quadratic equation ax^2 + bx + c
  244. /// \param bb value provided by user to complete the quadratic equation ax^2 + bx + c
  245. /// \param cc value provided by user to complete the quadratic equation ax^2 + bx + c
  246. /// \~Spanish
  247. /// \brief Funcion que es invocada cuando el boton de brincar es
  248. /// presionado, hace que el sapo salte a las coordenadas calculadas
  249. /// con los valores de a, b, c proporcionados por el usuario.
  250. /// \param aa valor proporcionado por el usuario utilizado para completar la ecuacion cuadratica ax^2 + bx + c
  251. /// \param bb valor proporcionado por el usuario utilizado para completar la ecuacion cuadratica ax^2 + bx + c
  252. /// \param cc valor proporcionado por el usuario utilizado para completar la ecuacion cuadratica ax^2 + bx + c
  253. ///
  254. void frog::run(int aa, int bb, int cc){
  255. A = aa;
  256. B = bb;
  257. C = cc;
  258. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  259. myTimer->start(25);
  260. }