説明なし

frog.cpp 5.6KB

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