Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <cstdlib>
- const float SIZE = 48;
- sf::Vector2f WindowSize = {384.f, 768.f};
- class Block
- {
- private:
- sf::RectangleShape hitbox;
- float gravity;
- public:
- Block(sf::Vector2f position, float gravity)
- {
- hitbox.setPosition(position);
- hitbox.setSize(sf::Vector2f(SIZE, SIZE));
- hitbox.setFillColor(sf::Color::Blue);
- this->gravity = gravity;
- }
- sf::RectangleShape GetHitbox() { return hitbox; }
- void UpdatePosition()
- {
- hitbox.move(0, gravity);
- if(hitbox.getPosition().y > WindowSize.y - SIZE)
- {
- hitbox.move(0, -SIZE);
- }
- }
- void Collision(sf::FloatRect r)
- {
- if(hitbox.getGlobalBounds().top == r.top)
- {
- std::cout << "collide";
- }
- }
- };
- int main()
- {
- sf::RenderWindow window(sf::VideoMode((unsigned int)WindowSize.x, (unsigned int)WindowSize.y), "Tetris");
- window.setFramerateLimit(10);
- int CurrentFrame = 1;
- float gravity = SIZE;
- std::vector<Block> BlockArray;
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- switch(event.type)
- {
- case sf::Event::Closed:
- window.close();
- }
- }
- if(CurrentFrame % ((int)SIZE / (int)gravity) == 0)
- {
- BlockArray.push_back(Block(sf::Vector2f(rand()%((int)WindowSize.x / (int)SIZE)*SIZE, -SIZE), gravity));
- std::cout << CurrentFrame << std::endl;
- }
- for(int i = 0; i < BlockArray.size(); i++)
- {
- BlockArray[i].UpdatePosition();
- for(int j = 0; j < BlockArray.size(); j++)
- {
- BlockArray[i].Collision(BlockArray[j].GetHitbox().getGlobalBounds());
- }
- }
- window.clear();
- for(int i = 0; i < BlockArray.size(); i++)
- {
- window.draw(BlockArray[i].GetHitbox());
- }
- window.display();
- if(CurrentFrame == 60) CurrentFrame = 1;
- else CurrentFrame++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement