Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. RenderWindow window(VideoMode(320, 480), "The Game!");
  2.  
  3. Texture t1,t2,t3;
  4. t1.loadFromFile("images/tiles.png");
  5. t2.loadFromFile("images/background.png");
  6. t3.loadFromFile("images/frame.png");
  7.  
  8. Sprite s(t1), background(t2), frame(t3);
  9.  
  10. int dx=0; bool rotate=0; int colorNum=1;
  11. float timer=0,delay=0.3;
  12.  
  13. Clock clock;
  14.  
  15. while (window.isOpen())
  16. {
  17. float time = clock.getElapsedTime().asSeconds();
  18. clock.restart();
  19. timer+=time;
  20.  
  21. Event e;
  22. while (window.pollEvent(e))
  23. {
  24. if (e.type == Event::Closed)
  25. window.close();
  26.  
  27. if (e.type == Event::KeyPressed)
  28. if (e.key.code==Keyboard::Up) rotate=true;
  29. else if (e.key.code==Keyboard::Left) dx=-1;
  30. else if (e.key.code==Keyboard::Right) dx=1;
  31. }
  32.  
  33. if (Keyboard::isKeyPressed(Keyboard::Down)) delay=0.05;
  34.  
  35. //// <- Move -> ///
  36. for (int i=0;i<4;i++) { b[i]=a[i]; a[i].x+=dx; }
  37. if (!check()) for (int i=0;i<4;i++) a[i]=b[i];
  38.  
  39. //////Rotate//////
  40. if (rotate)
  41. {
  42. Point p = a[1]; //center of rotation
  43. for (int i=0;i<4;i++)
  44. {
  45. int x = a[i].y-p.y;
  46. int y = a[i].x-p.x;
  47. a[i].x = p.x - x;
  48. a[i].y = p.y + y;
  49. }
  50. if (!check()) for (int i=0;i<4;i++) a[i]=b[i];
  51. }
  52.  
  53. ///////Tick//////
  54. if (timer>delay)
  55. {
  56. for (int i=0;i<4;i++) { b[i]=a[i]; a[i].y+=1; }
  57.  
  58. if (!check())
  59. {
  60. for (int i=0;i<4;i++) field[b[i].y][b[i].x]=colorNum;
  61.  
  62. colorNum=1+rand()%7;
  63. int n=rand()%7;
  64. for (int i=0;i<4;i++)
  65. {
  66. a[i].x = figures[n][i] % 2;
  67. a[i].y = figures[n][i] / 2;
  68. }
  69. }
  70.  
  71. timer=0;
  72. }
  73.  
  74. ///////check lines//////////
  75. int k=M-1;
  76. for (int i=M-1;i>0;i--)
  77. {
  78. int count=0;
  79. for (int j=0;j<N;j++)
  80. {
  81. if (field[i][j]) count++;
  82. field[k][j]=field[i][j];
  83. }
  84. if (count<N) k--;
  85. }
  86.  
  87. dx=0; rotate=0; delay=0.3;
  88.  
  89. /////////draw//////////
  90. window.clear(Color::White);
  91. window.draw(background);
  92.  
  93. for (int i=0;i<M;i++)
  94. for (int j=0;j<N;j++)
  95. {
  96. if (field[i][j]==0) continue;
  97. s.setTextureRect(IntRect(field[i][j]*18,0,18,18));
  98. s.setPosition(j*18,i*18);
  99. s.move(28,31); //offset
  100. window.draw(s);
  101. }
  102.  
  103. for (int i=0;i<4;i++)
  104. {
  105. s.setTextureRect(IntRect(colorNum*18,0,18,18));
  106. s.setPosition(a[i].x*18,a[i].y*18);
  107. s.move(28,31); //offset
  108. window.draw(s);
  109. }
  110.  
  111. window.draw(frame);
  112. window.display();
  113. }
  114.  
  115. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement