설명 없음

frog.cpp 5.6KB

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