Guest User

Untitled

a guest
Feb 22nd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. #include <iostream>
  4. using namespace std;
  5. using namespace sf;
  6.  
  7. const int M = 20;
  8. const int N = 10;
  9. int field[M][N] = { 0 };
  10.  
  11. struct Point
  12. {
  13. int x, y;
  14. } a[4], b[4];
  15.  
  16. int figures[7][4] =
  17. {
  18. 1,3,5,7, // I
  19. 2,4,5,7, // Z
  20. 3,5,4,6, // S
  21. 3,5,4,7, // T
  22. 2,3,5,7, // L
  23. 3,5,7,6, // J
  24. 2,3,4,5, // O
  25. };
  26.  
  27. bool check()
  28. {
  29. for (int i = 0; i<4; i++)
  30. if (a[i].x<0 || a[i].x >= N || a[i].y >= M) return 0;
  31. else if (field[a[i].y][a[i].x]) return 0;
  32.  
  33. return 1;
  34. };
  35.  
  36. void menu(RenderWindow&window)
  37. {
  38. Texture menuTexture1, menuTexture3;
  39. menuTexture1.loadFromFile("images/111.png");
  40. menuTexture3.loadFromFile("images/333.png");
  41.  
  42. Sprite menu1(menuTexture1), menu3(menuTexture3);
  43. bool isMenu = 1;
  44. int menuNum = 0;
  45.  
  46. menu1.setPosition(75, 50);
  47. menu3.setPosition(73, 150);
  48.  
  49.  
  50. while (isMenu)
  51. {
  52. menu1.setColor(Color::White);
  53. menu3.setColor(Color::White);
  54. menuNum = 0;
  55. window.clear(Color(129, 181, 221));
  56.  
  57. if (IntRect(100, 30, 300, 50).contains(Mouse::getPosition(window))) { menu1.setColor(Color::Blue); menuNum = 1; }
  58. if (IntRect(100, 150, 300, 50).contains(Mouse::getPosition(window))) { menu3.setColor(Color::Blue); menuNum = 3; }
  59.  
  60. if (Mouse::isButtonPressed(Mouse::Left))
  61. {
  62. if (menuNum == 1) isMenu = false;
  63. if (menuNum == 3) { window.close(); isMenu = false; }
  64.  
  65.  
  66. }
  67.  
  68. window.draw(menu1);
  69. window.draw(menu3);
  70. window.display();
  71.  
  72. }
  73. }
  74.  
  75. int main()
  76. {
  77. srand(time(0));
  78.  
  79. RenderWindow window(VideoMode(310, 405), "Tetris");
  80. menu(window);//вызов меню
  81. Texture t1, t2,t3;
  82. t1.loadFromFile("images/tiles.png");
  83. t2.loadFromFile("images/background.png");
  84. t3.loadFromFile("images/frame.png");
  85.  
  86. Font font;
  87. font.loadFromFile("shrift/CyrilicOld.ttf");
  88. Text text("", font, 20);
  89. //text.setColor(Color::Green);
  90. text.setStyle(sf::Text::Bold | sf::Text::Underlined);
  91. text.setString("Ваши Очки:");
  92. text.setPosition(0,0);
  93. window.draw(text);
  94.  
  95. Sprite s(t1), background(t2),frame(t3);;
  96.  
  97. int dx = 0; bool rotate = 0; int colorNum = 1;
  98. float timer = 0, delay = 0.3;
  99.  
  100. Clock clock;
  101.  
  102.  
  103. while (window.isOpen())
  104. {
  105. float time = clock.getElapsedTime().asSeconds();
  106. clock.restart();
  107. timer += time;
  108.  
  109. Event e;
  110. while (window.pollEvent(e))
  111. {
  112. if (e.type == Event::Closed)
  113. window.close();
  114. if (Keyboard::isKeyPressed(Keyboard::Escape)) { menu(window);}
  115. if (e.type == Event::KeyPressed)
  116. if (e.key.code == Keyboard::Up) rotate = true;
  117. else if (e.key.code == Keyboard::Left) dx = -1;
  118. else if (e.key.code == Keyboard::Right) dx = 1;
  119. }
  120.  
  121. if (Keyboard::isKeyPressed(Keyboard::Down)) delay = 0.05;
  122.  
  123. //// <- Move -> ///
  124. for (int i = 0; i<4; i++) { b[i] = a[i]; a[i].x += dx; }
  125. if (!check()) for (int i = 0; i<4; i++) a[i] = b[i];
  126.  
  127. //////Rotate//////
  128. if (rotate)
  129. {
  130. Point p = a[1]; //center of rotation
  131. for (int i = 0; i<4; i++)
  132. {
  133. int x = a[i].y - p.y;
  134. int y = a[i].x - p.x;
  135. a[i].x = p.x - x;
  136. a[i].y = p.y + y;
  137. }
  138. if (!check()) for (int i = 0; i<4; i++) a[i] = b[i];
  139. }
  140.  
  141. ///////Tick//////
  142. if (timer>delay)
  143. {
  144. for (int i = 0; i<4; i++) { b[i] = a[i]; a[i].y += 1; }
  145.  
  146. if (!check())
  147. {
  148. for (int i = 0; i<4; i++) field[b[i].y][b[i].x] = colorNum;
  149.  
  150. colorNum = 1 + rand() % 7;
  151. int n = rand() % 7;
  152. for (int i = 0; i<4; i++)
  153. {
  154. a[i].x = figures[n][i] % 2;
  155. a[i].y = figures[n][i] / 2;
  156. }
  157. }
  158.  
  159. timer = 0;
  160. }
  161.  
  162. ///////check lines//////////
  163.  
  164. int k = M - 1;
  165.  
  166.  
  167. for (int i = M - 1; i>0; i--)
  168. {
  169.  
  170. int count = 0;
  171. for (int j = 0; j<N; j++)
  172. {
  173. if (field[i][j]) count++;
  174. field[k][j] = field[i][j];
  175. }
  176.  
  177. if (count<N) k--;
  178.  
  179. }
  180. dx = 0; rotate = 0; delay = 0.3;
  181.  
  182. /////////draw//////////
  183. window.clear(Color::White);
  184. window.draw(background);
  185.  
  186. for (int i = 0; i<M; i++)
  187. for (int j = 0; j<N; j++)
  188. {
  189. if (field[i][j] == 0) continue;
  190. s.setTextureRect(IntRect(field[i][j] * 18, 0, 18, 18));
  191. s.setPosition(j * 18, i * 18);
  192. s.move(28, 31); //offset
  193. window.draw(s);
  194. }
  195.  
  196. for (int i = 0; i<4; i++)
  197. {
  198. s.setTextureRect(IntRect(colorNum * 18, 0, 18, 18));
  199. s.setPosition(a[i].x * 18, a[i].y * 18);
  200. s.move(28, 31); //offset
  201. window.draw(s);
  202. }
  203.  
  204.  
  205. window.display();
  206. }
  207.  
  208. return 0;
  209. }
Add Comment
Please, Sign In to add comment