Advertisement
Felanpro

TETRIS IN DEVELOPMENT SFML

Apr 9th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Window.hpp>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. sf::RectangleShape player_squares[4];
  10. sf::Vector2f gravity(0, 100);
  11. sf::Vector2f second_gravity(0, 5);
  12. int timer = 0;
  13.  
  14. sf::RectangleShape objects[100];
  15.  
  16. void setShape1()
  17. {
  18.     for (int x = 0; x < 4; x++)
  19.     {
  20.         player_squares[x].setPosition(250 - 50, (x * 100));
  21.     }
  22. }
  23.  
  24. void setShape2()
  25. {
  26.  
  27. }
  28.  
  29. int main()
  30. {
  31.     sf::RenderWindow window(sf::VideoMode(500, 800), "In Development");
  32.     window.setFramerateLimit(70);
  33.     window.setVerticalSyncEnabled(true);
  34.     //Preparations for player_square objects
  35.     for (int x = 0; x < 4; x++)
  36.     {
  37.         player_squares[x].setSize(sf::Vector2f(100, 100));
  38.         player_squares[x].setFillColor(sf::Color::Blue);
  39.     }
  40.  
  41.     //Preparations for objects
  42.     for (int x = 0; x < 100; x++)
  43.     {
  44.         objects[x].setSize(sf::Vector2f(100, 100));
  45.         objects[x].setFillColor(sf::Color::Red);
  46.     }
  47.  
  48.     //Time handling
  49.     sf::Time time = sf::seconds(0);
  50.     sf::Clock clock; //Start the essential clock.
  51.  
  52.     /* ---TESTING SPECIMEN SECTION--- */
  53.     setShape1();
  54.     objects[4].setPosition(200, 500);
  55.     objects[88].setPosition(100, 300);
  56.     objects[80].setPosition(200, 200);
  57.  
  58.     sf::Event event;
  59.     while (window.isOpen())
  60.     {
  61.         while (window.pollEvent(event))
  62.         {
  63.             if (event.type == sf::Event::Closed)
  64.                 window.close();
  65.         }
  66.  
  67.         window.clear(sf::Color::White); //Clear
  68.  
  69.         //Check if the player squares are exceeding window height limit or touching another object and stop it
  70.         for (int x = 0; x < 4; x++)
  71.         {
  72.             if (player_squares[x].getPosition().y + 100 >= 800)
  73.                 gravity.y = 0;
  74.         }
  75.  
  76.         /*EXPERIENCING A BUG HERE CHECK IT OUT AND FIX IT*/
  77.         if (player_squares[4].getGlobalBounds().intersects(objects[4].getGlobalBounds())) //PROBABLY SOME ERROR HERE               
  78.         {
  79.             cout << "The player square is touching objects";
  80.         }
  81.  
  82.         //Draw the player squares
  83.         for (int x = 0; x < 4; x++)
  84.         {
  85.             window.draw(player_squares[x]);
  86.         }
  87.         //Move the player squares
  88.         time = clock.getElapsedTime();
  89.         if (time.asSeconds() > 1)
  90.         {
  91.             for (int x = 0; x < 4; x++)
  92.             {
  93.                 player_squares[x].move(gravity);
  94.             }
  95.             clock.restart();
  96.         }
  97.  
  98.         //Check if the objects are exceeding window height limit or touching another object and stop it
  99.         for (int x = 0; x < 100; x++)
  100.         {
  101.             if (objects[x].getPosition().y + 100 >= 800)
  102.             {
  103.                 objects[x].setPosition(objects[x].getPosition().x, 700);
  104.             }
  105.         }
  106.  
  107.         //Draw the objects
  108.         for (int x = 0; x < 100; x++)
  109.         {
  110.             if (objects[x].getPosition().x == 0 && objects[x].getPosition().x == 0)
  111.                 ;
  112.             else
  113.                 window.draw(objects[x]);
  114.         }
  115.         //Move objects
  116.         for (int x = 0; x < 100; x++)
  117.         {
  118.             objects[x].move(second_gravity);
  119.         }
  120.  
  121.         window.display(); //Display
  122.     }
  123.  
  124.     int pause; cin >> pause; //Pause the program
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement