No Description

frog.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "frog.h"
  2. #include <QDebug>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cassert>
  6. #include <QMessageBox>
  7. #include "mainwindow.h"
  8. #include "ui_mainwindow.h"
  9. frog::frog(QWidget *parent) :
  10. QWidget(parent)
  11. {
  12. myTimer= new QTimer(this);
  13. flag = xFlag = false;
  14. Ysapo = 190;
  15. A = B = C = 0;
  16. temp = 580;
  17. temp2 = 0;
  18. backFlag = false;
  19. moved = false;
  20. textBool = false;
  21. buttonCounter = 0;
  22. xnenu = -(rand() %30);
  23. Xsapo = xnenu;
  24. initialX = Xsapo;
  25. xnenu2 = (rand() % 100) + 100;
  26. xnenu3 = xnenu2 + (rand() % 100) + 200;
  27. }
  28. void frog::updatenenu(){
  29. xnenu = -(rand() % 30);
  30. Xsapo = xnenu;
  31. initialX = Xsapo;
  32. xnenu2 = (rand() % 100) + 100;
  33. xnenu3 = xnenu2 + (rand() % 100) + 200;
  34. }
  35. void frog::reset(){
  36. flag = xFlag = false;
  37. Ysapo = 190;
  38. A = B = C = 0;
  39. temp = 580;
  40. temp2 = 0;
  41. backFlag = false;
  42. moved = false;
  43. buttonCounter = 0;
  44. textBool = false;
  45. repaint();
  46. myTimer->stop();
  47. delete myTimer;
  48. myTimer = new QTimer;
  49. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  50. xnenu = -(rand() %30);
  51. Xsapo = xnenu;
  52. initialX = Xsapo;
  53. xnenu2 = (rand() % 100) + 100;
  54. xnenu3 = xnenu2 + (rand() % 100) + 200;
  55. }
  56. void frog::paintEvent(QPaintEvent *event){
  57. QPainter p(this);
  58. QString frogger = ":/images/Frogger1.png";
  59. QString frogger2 = ":/images/Frogger2.png";
  60. QString frogger3 = ":/images/Frogger3.png";
  61. QString leaf = ":/images/Nenufar1.png";
  62. QString back = ":/images/background.jpg";
  63. // Condiciones que permitian un rango de error (no caer en la x exacta de los nenufares)
  64. if(backFlag == true || (Xsapo <= (xnenu3 + 55) && Xsapo >= (xnenu3 - 55) && Ysapo == 190)
  65. || (Xsapo > xnenu && Ysapo == 190 && Xsapo < (xnenu2-55))
  66. || (Xsapo > xnenu2 && Ysapo == 190 && Xsapo < (xnenu3-55))
  67. || (Xsapo > xnenu3 && Ysapo == 190)){
  68. // if(backFlag == true || (Ysapo == 190 && (Xsapo != xnenu && Xsapo != xnenu2))){
  69. if (inter1Label == NULL){
  70. delete inter1Label;
  71. }
  72. if (inter2Label == NULL){
  73. delete inter2Label;
  74. }
  75. backFlag = true;
  76. myTimer->stop();
  77. delete myTimer;
  78. myTimer = new QTimer;
  79. QThread::msleep(5);
  80. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  81. p.drawPixmap(temp2,-5,600,400,QPixmap(back));
  82. p.drawPixmap(temp,-5,600,400,QPixmap(back));
  83. p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
  84. p.drawPixmap(xnenu2,250,125,60,QPixmap(leaf));
  85. p.drawText(xnenu2+45,290,"x1 = " + QString::number(xnenu2));
  86. p.drawPixmap(xnenu3,250,125,60,QPixmap(leaf));
  87. p.drawText(xnenu3+45,290,"x2 = " + QString::number(xnenu3));
  88. Xsapo=Xsapo-1;
  89. xnenu=xnenu-1;
  90. xnenu2=xnenu2-1;
  91. xnenu3=xnenu3-1;
  92. temp= temp - 1;
  93. temp2 = temp2 -1;
  94. repaint();
  95. if(temp == 0 || xnenu3 == -10 || Xsapo == 0){
  96. backFlag = false;
  97. temp = 580;
  98. temp2 = 0;
  99. moved = true;
  100. textBool = false;
  101. updatenenu();
  102. }
  103. }
  104. else{
  105. QFont font = p.font();
  106. font.setBold(true);
  107. p.setFont(font);
  108. p.drawPixmap(temp2,-5,600,400,QPixmap(back));
  109. p.drawPixmap(temp,-5,600,400,QPixmap(back));
  110. p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
  111. p.drawPixmap(xnenu2,250,125,60,QPixmap(leaf));
  112. p.drawPixmap(xnenu3,250,125,60,QPixmap(leaf));
  113. // Cambiada la condicion para que sea exacta la caida en el nenufar
  114. // if(Xsapo < (xnenu2 + 55) && Ysapo == 190 && Xsapo > (xnenu2-55)){
  115. // textBool = true;
  116. // }
  117. if(Ysapo == 190 && Xsapo == xnenu2){
  118. textBool = true;
  119. }
  120. if(textBool){
  121. p.drawText(xnenu2+35,290,"x1 = " + QString::number(xnenu2));
  122. p.drawText(xnenu3+35,290,"x2 = " + QString::number(xnenu3));
  123. p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
  124. }
  125. else{
  126. p.drawText(xnenu2+35,290,"x2 = " + QString::number(xnenu2));
  127. }
  128. }
  129. // Cambiada la condicion para que sea exacta la caida en el nenufar
  130. if(Xsapo > (xnenu + 55) && Ysapo == 190 && Xsapo < (xnenu2-55)
  131. || (Xsapo > (xnenu2 + 55) && Ysapo == 190 && Xsapo < (xnenu3-55))
  132. || (Xsapo > (xnenu3 + 55) && Ysapo == 190)){
  133. // if(Ysapo == 190 && (Xsapo != xnenu && Xsapo != xnenu2 && Xsapo != xnenu3)){
  134. p.drawPixmap(Xsapo, Ysapo + 30, 100, 80, QPixmap(frogger3));
  135. }
  136. else if(Ysapo < 190){
  137. p.drawPixmap(Xsapo,Ysapo,100,100,QPixmap(frogger2));
  138. }else if(Ysapo == 190){
  139. qDebug() << "xsapo: " << Xsapo << "xnenu2: " << xnenu2;
  140. p.drawPixmap(Xsapo,Ysapo,100,100,QPixmap(frogger));
  141. if(!textBool){
  142. p.drawText(xnenu+45,290,"x1 = " + QString::number(xnenu+10));
  143. }
  144. }
  145. }
  146. frog::~frog(){
  147. delete myTimer;
  148. }
  149. void frog::mySlot(){
  150. float interX1 = 0;
  151. float interX2 = 0;
  152. float temp = 0;
  153. float y = 0;
  154. float xtemp = 0;
  155. float xEnd = 0;
  156. interX1 = QuadraticMinus(A,B,C);
  157. interX2 = QuadraticPlus(A,B,C);
  158. if(interX1 > interX2){
  159. temp = interX1;
  160. interX1 = interX2;
  161. interX2 = temp;
  162. }
  163. if(moved){
  164. moved = false;
  165. buttonCounter = 1;
  166. initialX = Xsapo;
  167. }
  168. xEnd = interX2 + initialX;
  169. y = 1;
  170. xtemp = 1;
  171. if(!xFlag){
  172. Ysapo = Ysapo - y;
  173. Xsapo = Xsapo + xtemp;
  174. }
  175. else if(xFlag){
  176. Ysapo = Ysapo + y;
  177. Xsapo = Xsapo + xtemp;
  178. }
  179. if(int(Xsapo) == int(xEnd/2)){
  180. xFlag = true;
  181. }
  182. repaint();
  183. if((int(Xsapo) == int(xEnd) && xFlag == true) || Ysapo == 190){
  184. Ysapo = 190;
  185. repaint();
  186. myTimer->stop();
  187. delete myTimer;
  188. myTimer = new QTimer;
  189. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  190. initialX = Xsapo;
  191. xFlag = false;
  192. }
  193. }
  194. void frog::run(int aa, int bb, int cc){
  195. A = aa;
  196. B = bb;
  197. C = cc;
  198. buttonCounter++;
  199. connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
  200. myTimer->start(25);
  201. }