Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. const float SIZE = 48;
  6. sf::Vector2f WindowSize = {384.f, 768.f};
  7.  
  8. class Block
  9. {
  10. private:
  11. sf::RectangleShape hitbox;
  12. float gravity;
  13. public:
  14. Block(sf::Vector2f position, float gravity)
  15. {
  16. hitbox.setPosition(position);
  17. hitbox.setSize(sf::Vector2f(SIZE, SIZE));
  18. hitbox.setFillColor(sf::Color::Blue);
  19. this->gravity = gravity;
  20. }
  21. sf::RectangleShape GetHitbox() { return hitbox; }
  22. void UpdatePosition()
  23. {
  24. hitbox.move(0, gravity);
  25. if(hitbox.getPosition().y > WindowSize.y - SIZE)
  26. {
  27. hitbox.move(0, -SIZE);
  28. }
  29. }
  30. void Collision(sf::FloatRect r)
  31. {
  32. if(hitbox.getGlobalBounds().top == r.top)
  33. {
  34. std::cout << "collide";
  35. }
  36. }
  37. };
  38.  
  39. int main()
  40. {
  41. sf::RenderWindow window(sf::VideoMode((unsigned int)WindowSize.x, (unsigned int)WindowSize.y), "Tetris");
  42. window.setFramerateLimit(10);
  43. int CurrentFrame = 1;
  44. float gravity = SIZE;
  45.  
  46. std::vector<Block> BlockArray;
  47.  
  48. while (window.isOpen())
  49. {
  50. sf::Event event;
  51. while (window.pollEvent(event))
  52. {
  53. switch(event.type)
  54. {
  55. case sf::Event::Closed:
  56. window.close();
  57. }
  58. }
  59.  
  60. if(CurrentFrame % ((int)SIZE / (int)gravity) == 0)
  61. {
  62. BlockArray.push_back(Block(sf::Vector2f(rand()%((int)WindowSize.x / (int)SIZE)*SIZE, -SIZE), gravity));
  63. std::cout << CurrentFrame << std::endl;
  64. }
  65.  
  66. for(int i = 0; i < BlockArray.size(); i++)
  67. {
  68. BlockArray[i].UpdatePosition();
  69. for(int j = 0; j < BlockArray.size(); j++)
  70. {
  71. BlockArray[i].Collision(BlockArray[j].GetHitbox().getGlobalBounds());
  72. }
  73. }
  74.  
  75.  
  76. window.clear();
  77. for(int i = 0; i < BlockArray.size(); i++)
  78. {
  79. window.draw(BlockArray[i].GetHitbox());
  80. }
  81.  
  82. window.display();
  83.  
  84. if(CurrentFrame == 60) CurrentFrame = 1;
  85. else CurrentFrame++;
  86. }
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement