Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving a sliding puzzle  (Read 2448 times)

0 Members and 1 Guest are viewing this topic.

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving a sliding puzzle
« on: June 06, 2021, 02:00:12 am »
So having trouble setting up this sliding puzzle game where I have one square that matches the background and trying to make the squares move. I know about the keyboard input and tried to implement it but I am unsure how to connect that to my squares? Should I make a new class to move these square or how should I approach it? How can I that connection for it?

this is my classes for the rest of the game I have made except for the buttons as they work fine and the text object as well
class States
{
private:
    std::vector<bool> objectStates;
public:
    States();
    enum states {HOVERED, NORMAL, HIDDEN, LAST};
    bool checkState(states state) const;
    void enableState(states state);
    void disableState(states state);

};

States::States()
{
    for(int i=0; i<LAST; i++)
        objectStates.push_back(false);
}
bool States::checkState(states state) const
{
    return objectStates[state];
}
void States::enableState(states state)
{
    objectStates[state] = true;
}
void States::disableState(states state)
{
    objectStates[state] = false;
}
 

#include <SFML/Graphics.hpp>
#include "States.h"
#include "LinkedListPiece.h"

class Rectangle : public sf::Drawable, public States
{

private:
    sf::RectangleShape background;
    sf::RectangleShape rectangle;
   
public:
    Rectangle();
    void center();
    void draw(sf::RenderTarget& window, sf::RenderStates states) const;
    void addEventHandler(sf::RenderWindow &window, sf::Event event);
    void update();

    void setSize(sf::Vector2f size);
    void setPosition(sf::Vector2f position);

    sf::FloatRect getGlobalBounds();
};

#include "Rectangle.h"

Rectangle::Rectangle() { enableState(HIDDEN);}

void Rectangle::update()
{

}

void Rectangle::draw(sf::RenderTarget& window, sf::RenderStates states) const
{
    if(!checkState(HIDDEN))
        window.draw(rectangle);
}

void Rectangle::center()
{

}

void Rectangle::addEventHandler(sf::RenderWindow &window, sf::Event event)
{

}

void Rectangle::setSize(sf::Vector2f size)
{
    rectangle.setSize(size);
}

void Rectangle::setPosition(sf::Vector2f position)
{
    rectangle.setPosition(position);
}

sf::FloatRect Rectangle::getGlobalBounds()
{
    return background.getGlobalBounds();
}
 


#include <SFML/Graphics.hpp>
#include "Button.h"
#include "ButtonTwo.h"
#include "Fonts.h"
#include "Rectangle.h"

class MainGame
{
private:
    sf::RenderWindow window;
    Button button;
    ButtonTwo exitButton;
    Rectangle r;
    Rectangle rTwo;
    Rectangle rThree;
    Rectangle rFour;
    sf::Text title;
    WindowDialog dialog;
public:
    MainGame();
    void run();

};

MainGame::MainGame() : window({1920, 1080, 32}, "SFML Final Project")
{

    button.setOrigin({button.getGlobalBounds().width/2,
                      button.getGlobalBounds().height/2});

    button.setPosition({window.getSize().x/2.f,
                        window.getSize().y/2.f});

    exitButton.setOrigin({button.getGlobalBounds().width/2,
                      button.getGlobalBounds().height/2});

    exitButton.setPosition({window.getSize().x/2.f,
                        window.getSize().y/1.5f});

    title.setFont(Fonts::getFont());
    title.setOrigin(title.getGlobalBounds().width/2,
                      title.getGlobalBounds().height/2);
    title.setPosition(window.getSize().x/5.5f,
                      window.getSize().y/9.5f);
    title.setFillColor(sf::Color::Blue);
    title.setCharacterSize(150);
    title.setString("Final Siding Puzzle Game");

    r.setSize({100, 100});
    r.setPosition({window.getSize().x / 2.f,
                   window.getSize().y / 2.f});

    rTwo.setSize({100,100});
    rTwo.setPosition({window.getSize().x/2.25f,
                      window.getSize().y/2.f});

    rThree.setSize({100, 100});
    rThree.setPosition({window.getSize().x / 2.f,
                   window.getSize().y / 2.48f});
    rFour.setSize({100,100});
    rFour.setPosition({window.getSize().x/2.25f,
                      window.getSize().y/2.48f});

}

void MainGame::run()
{

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();

            sf::FloatRect bounds = button.getGlobalBounds();
            sf::FloatRect boundsTwo = exitButton.getGlobalBounds();
            sf::Vector2f mPos = (sf::Vector2f) sf::Mouse::getPosition(window);
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && bounds.contains(mPos))
            {
                //this will set the rectangle to a fade state
                button.enableState(States::HIDDEN);
                exitButton.enableState(States::HIDDEN);
                r.disableState(States::HIDDEN);
                rTwo.disableState(States::HIDDEN);
                rThree.disableState(States::HIDDEN);
                rFour.disableState(States::HIDDEN);

            }
            else if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && boundsTwo.contains(mPos))
            {
                button.enableState(States::HIDDEN);
                exitButton.enableState(States::HIDDEN);
            }
            //other events
            button.addEventHandler(window, event);
            exitButton.addEventHandler(window, event);
        }

        button.update();
        window.clear(sf::Color(53, 189, 164));
        window.draw(button);
        window.draw(exitButton);
        window.draw(title);
        window.draw(r);
        window.draw(rTwo);
        window.draw(rThree);
        window.draw(rFour);
        window.display();
    }
}

 

and my main

#include <iostream>
#include <SFML/Graphics.hpp>
#include "MainGame.h"

int main()
{
    MainGame game;
    game.run();
    return 0;
}

 
« Last Edit: June 06, 2021, 04:44:05 pm by Daoxin »

Daoxin

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Moving a sliding puzzle
« Reply #1 on: June 08, 2021, 12:53:00 am »
Okay I did some more stuff to make it move but unsure what I need to do to make it move. I think what I have is right so far but unsure?

header file:

#include <SFML/Graphics.hpp>
#include "Button.h"
#include "ButtonTwo.h"
#include "Fonts.h"
#include "Rectangle.h"

class MainGame
{
private:
    sf::RenderWindow window;
    Button button;
    ButtonTwo exitButton;
    Rectangle r;
    Rectangle rTwo;
    Rectangle rThree;
    Rectangle rFour;
    sf::Text title;
    WindowDialog dialog;
public:
    enum boxCanMove{UP, LEFT, DOWN, RIGHT};
    MainGame();
    void run();

};
 

cpp file

MainGame::MainGame() : window({1920, 1080, 32}, "SFML Final Project")
{

    button.setOrigin({button.getGlobalBounds().width/2,
                      button.getGlobalBounds().height/2});

    button.setPosition({window.getSize().x/2.f,
                        window.getSize().y/2.f});

    exitButton.setOrigin({button.getGlobalBounds().width/2,
                      button.getGlobalBounds().height/2});

    exitButton.setPosition({window.getSize().x/2.f,
                        window.getSize().y/1.5f});

    title.setFont(Fonts::getFont());
    title.setOrigin(title.getGlobalBounds().width/2,
                      title.getGlobalBounds().height/2);
    title.setPosition(window.getSize().x/5.5f,
                      window.getSize().y/9.5f);
    title.setFillColor(sf::Color::Blue);
    title.setCharacterSize(150);
    title.setString("Final Siding Puzzle Game");

    r.setSize({100, 100});
    r.setPosition({window.getSize().x / 2.f,
                   window.getSize().y / 2.f});
    r.setFillColor(sf::Color(53, 189, 164));

    rTwo.setSize({100,100});
    rTwo.setPosition({window.getSize().x/2.25f,
                      window.getSize().y/2.f});
    rTwo.setFillColor(sf::Color::Yellow);

    rThree.setSize({100, 100});
    rThree.setPosition({window.getSize().x / 2.f,
                   window.getSize().y / 2.48f});
    rThree.setFillColor(sf::Color::Yellow);

    rFour.setSize({100,100});
    rFour.setPosition({window.getSize().x/2.25f,
                      window.getSize().y/2.48f});
    rFour.setFillColor(sf::Color::Yellow);

}

void MainGame::run()
{

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            sf::FloatRect bounds = button.getGlobalBounds();
            sf::FloatRect boundsTwo = exitButton.getGlobalBounds();
            sf::Vector2f mPos = (sf::Vector2f) sf::Mouse::getPosition(window);

            if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && bounds.contains(mPos))
            {
                //this will set the rectangle to a fade state
                button.enableState(States::HIDDEN);
                exitButton.enableState(States::HIDDEN);
                r.disableState(States::HIDDEN);
                rTwo.disableState(States::HIDDEN);
                rThree.disableState(States::HIDDEN);
                rFour.disableState(States::HIDDEN);

            }

            else if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && boundsTwo.contains(mPos))
            {
                exit(1);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                if(boxCanMove(UP))
                {
                    sf::FloatRect bounds = r.getGlobalBounds();

                    r.setPosition({
                                          bounds.left,
                                          bounds.top - bounds.height
                                  });
                }
            }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                if(boxCanMove(LEFT))
                {
                    sf::FloatRect bounds = r.getGlobalBounds();

                    r.setPosition({
                                          bounds.left + bounds.width,
                                          bounds.top
                                  });
                }
            }

            //other events
            button.addEventHandler(window, event);
            exitButton.addEventHandler(window, event);


        }

        button.update();
        exitButton.update();
        window.clear(sf::Color(53, 189, 164));
        window.draw(button);
        window.draw(exitButton);
        window.draw(title);
        window.draw(r);
        window.draw(rTwo);
        window.draw(rThree);
        window.draw(rFour);
        window.display();
    }
}


 

 

anything