Advertisement
Guest User

c++/sfml (tetris)

a guest
Oct 19th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. using namespace sf;
  4.  
  5. const int M = 20;
  6. const int N = 10;
  7.  
  8. int field[M][N] = {0};
  9.  
  10. struct Point
  11. {int x,y;
  12. } a[4], b[4];
  13.  
  14. int figures[7][4] =
  15. {
  16. 1,3,5,7, // I
  17. 2,4,5,7, // Z
  18. 3,5,4,6, // S
  19. 3,5,4,7, // T
  20. 2,3,5,7, // L
  21. 3,5,7,6, // J
  22. 2,3,4,5, // O
  23. };
  24.  
  25. bool check()
  26. {
  27. for (int i=0;i<4;i++)
  28. if (a.x<0 || a.x>=N || a.y>=M) return 0;
  29. else if (field[a.y][a.x]) return 0;
  30.  
  31. return 1;
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. srand(time(0));
  38.  
  39. RenderWindow window(VideoMode(320, 480), "The Game!");
  40.  
  41. Texture t1,t2,t3;
  42. t1.loadFromFile("images/tiles.png");
  43. t2.loadFromFile("images/background.png");
  44. t3.loadFromFile("images/frame.png");
  45.  
  46. Sprite s(t1), background(t2), frame(t3);
  47.  
  48. int dx=0; bool rotate=0; int colorNum=1;
  49. float timer=0,delay=0.3;
  50.  
  51. Clock clock;
  52.  
  53. while (window.isOpen())
  54. {
  55. float time = clock.getElapsedTime().asSeconds();
  56. clock.restart();
  57. timer+=time;
  58.  
  59. Event e;
  60. while (window.pollEvent(e))
  61. {
  62. if (e.type == Event::Closed)
  63. window.close();
  64.  
  65. if (e.type == Event::KeyPressed)
  66. if (e.key.code==Keyboard::Up) rotate=true;
  67. else if (e.key.code==Keyboard::Left) dx=-1;
  68. else if (e.key.code==Keyboard::Right) dx=1;
  69. }
  70.  
  71. if (Keyboard::isKeyPressed(Keyboard::Down)) delay=0.05;
  72.  
  73. //// <- Move -> ///
  74. for (int i=0;i<4;i++) { b=a; a.x+=dx; }
  75. if (!check()) for (int i=0;i<4;i++) a=b;
  76.  
  77. //////Rotate//////
  78. if (rotate)
  79. {
  80. Point p = a[1]; //center of rotation
  81. for (int i=0;i<4;i++)
  82. {
  83. int x = a.y-p.y;
  84. int y = a.x-p.x;
  85. a.x = p.x - x;
  86. a.y = p.y + y;
  87. }
  88. if (!check()) for (int i=0;i<4;i++) a=b;
  89. }
  90.  
  91. ///////Tick//////
  92. if (timer>delay)
  93. {
  94. for (int i=0;i<4;i++) { b=a; a.y+=1; }
  95.  
  96. if (!check())
  97. {
  98. for (int i=0;i<4;i++) field[b.y][b.x]=colorNum;
  99.  
  100. colorNum=1+rand()%7;
  101. int n=rand()%7;
  102. for (int i=0;i<4;i++)
  103. {
  104. a.x = figures[n] % 2;
  105. a.y = figures[n] / 2;
  106. }
  107. }
  108.  
  109. timer=0;
  110. }
  111.  
  112. ///////check lines//////////
  113. int k=M-1;
  114. for (int i=M-1;i>0;i--)
  115. {
  116. int count=0;
  117. for (int j=0;j<N;j++)
  118. {
  119. if (field[j]) count++;
  120. field[k][j]=field[j];
  121. }
  122. if (count<N) k--;
  123. }
  124.  
  125. dx=0; rotate=0; delay=0.3;
  126.  
  127. /////////draw//////////
  128. window.clear(Color::White);
  129. window.draw(background);
  130.  
  131. for (int i=0;i<M;i++)
  132. for (int j=0;j<N;j++)
  133. {
  134. if (field[j]==0) continue;
  135. s.setTextureRect(IntRect(field[j]18,0,18,18));
  136. s.setPosition(j18,i18);
  137. s.move(28,31); //offset
  138. window.draw(s);
  139. }
  140.  
  141. for (int i=0;i<4;i++)
  142. {
  143. s.setTextureRect(IntRect(colorNum18,0,18,18));
  144. s.setPosition(a.x18,a.y18);
  145. s.move(28,31); //offset
  146. window.draw(s);
  147. }
  148.  
  149. window.draw(frame);
  150. window.display();
  151. }
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement