tetromino.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "tetromino.hh"
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. Tetromino::Tetromino(Well* well):
  6. well(well),
  7. x(Constants::WIDTH/2 -5/2),
  8. y(-2),
  9. angle(0)
  10. {
  11. srand (time(NULL));
  12. type = Type(rand()%7);
  13. }
  14. void Tetromino::move(int direction)
  15. {
  16. checkRotation();
  17. if(direction == 0)
  18. {
  19. if(checkLeftSide())
  20. {
  21. x--;
  22. }
  23. }
  24. else
  25. {
  26. if(checkRightSide())
  27. {
  28. x++;
  29. }
  30. }
  31. }
  32. void Tetromino::rotate()
  33. {
  34. if(checkRotation())
  35. {
  36. if(angle == 3)
  37. angle = 0;
  38. else
  39. angle++;
  40. }
  41. }
  42. void Tetromino::reset()
  43. {
  44. for(auto j=0; j<5; ++j)
  45. for(auto i=0; i<5; ++i)
  46. {
  47. if(shapes[type][angle][j][i] != 0)
  48. {
  49. well->data[x+i][y+j] = shapes[type][angle][j][i];
  50. }
  51. }
  52. int scoreCounter = 0;
  53. for(auto j=0; j<Constants::HEIGHT; j++)
  54. {
  55. int counter = 0;
  56. for(auto i=0; i<Constants::WIDTH; i++)
  57. {
  58. if(well->data[i][j] != 0)
  59. {
  60. counter++;
  61. }
  62. }
  63. if(counter == Constants::WIDTH)
  64. {
  65. for(auto k=0; k<Constants::WIDTH; k++)
  66. well->data[k][j] = 0;
  67. for(auto n=j; n>1; n--)
  68. for(auto m=0; m<Constants::WIDTH; m++)
  69. {
  70. well->data[m][n] = well->data[m][n-1];
  71. }
  72. scoreCounter++;
  73. }
  74. }
  75. switch(scoreCounter)
  76. {
  77. case 1:
  78. score += 40;
  79. break;
  80. case 2:
  81. score += 100;
  82. break;
  83. case 3:
  84. score += 300;
  85. break;
  86. case 4:
  87. score += 1200;
  88. break;
  89. }
  90. x = Constants::WIDTH/2 -4/2;
  91. y = -2;
  92. angle = 0;
  93. type = Type(rand()%7);
  94. }
  95. bool Tetromino::checkRotation()
  96. {
  97. int check;
  98. if(angle==3)
  99. check = 0;
  100. else
  101. check = angle + 1;
  102. for(auto j=0; j<5; j++)
  103. for(auto i=0; i<5; i++)
  104. if(shapes[type][check][j][i] !=0)
  105. {
  106. if((x+i<0)||(x+i>=Constants::WIDTH))
  107. return false;
  108. if(well->data[x+i][y+j] != 0)
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool Tetromino::checkLeftSide()
  114. {
  115. for(auto j=0; j<5; j++)
  116. for(auto i=0; i<5; i++)
  117. {
  118. if(shapes[type][angle][j][i] != 0)
  119. {
  120. if(x+i == 0)
  121. return false;
  122. else if(well->data[x+i-1][y+j] != 0)
  123. {
  124. return false;
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. bool Tetromino::checkRightSide()
  131. {
  132. for(auto j=0; j<5; j++)
  133. for(auto i=0; i<5; i++)
  134. {
  135. if(shapes[type][angle][j][i] != 0)
  136. {
  137. if(x+i+1 == Constants::WIDTH)
  138. return false;
  139. else if(well->data[x+i+1][y+j] != 0)
  140. {
  141. return false;
  142. }
  143. }
  144. }
  145. return true;
  146. }
  147. bool Tetromino::checkDownCollision()
  148. {
  149. for(auto j=0; j<5; j++)
  150. for(auto i=0; i<5; i++)
  151. {
  152. if(shapes[type][angle][j][i] != 0)
  153. {
  154. if(j+y == Constants::HEIGHT-1)
  155. {
  156. reset();
  157. return true;
  158. }
  159. else if(well->data[i+x][j+y+1] != 0)
  160. {
  161. if(j+y>0)
  162. reset();
  163. else
  164. {
  165. for(auto k=0;k<Constants::WIDTH;k++)
  166. for(auto l=0; l<Constants::HEIGHT; l++)
  167. well->data[k][l] = 0;
  168. score = 0;
  169. }
  170. return true;
  171. }
  172. }
  173. }
  174. return false;
  175. }
  176. void Tetromino::update()
  177. {
  178. if(!checkDownCollision())
  179. {
  180. y++;
  181. }
  182. }
  183. void Tetromino::draw(SDL_Renderer* renderer, SDL_Texture* blocksTexture)
  184. {
  185. for(auto i=0; i<5; i++)
  186. for(auto j=0; j<5; j++)
  187. {
  188. if(shapes[type][angle][j][i] != 0)
  189. {
  190. SDL_Rect posRect{(x+i+1)*Constants::BLOCK_SIZE, (y+j)*Constants::BLOCK_SIZE, Constants::BLOCK_SIZE, Constants::BLOCK_SIZE};
  191. //SDL_Rect posRect{0, 0, Constants::BLOCK_SIZE, Constants::BLOCK_SIZE};
  192. SDL_Rect texRect{(type)*8, 0, 8, 8};
  193. SDL_RenderCopy(renderer, blocksTexture, &texRect, &posRect);
  194. }
  195. }
  196. }