Possible to add multiple bounding boxes to one sprite and resize them in SFML?

Viewed 76

I'm making a Pacman game using SFML. I have a background image for the maze that I'm using and I was wondering how I would be able to use multiple bounding boxes for the background so that when the yellow Pacman character collides with the walls, it stops him. Also, I was wondering if I could use multiple bounding boxes for the background maze image, if so, how?

My code:

#include <SFML/Graphics.hpp>
#include <iostream>


int main(int argc, char** argv)
{
    sf::RenderWindow renderWindow(sf::VideoMode(280, 480), "Pacman" /*, sf::Style::Titlebar | sf::Style::Close*/);
    sf::Event event;

    sf::Texture texture, texture2;
    texture.setSmooth (true);
    texture2.setSmooth (true);


    texture.loadFromFile("images/pacmanbkgd.png");

    sf::Sprite background_sprite(texture);
    background_sprite.setScale(sf::Vector2f(1.25, 1.25));


    texture2.loadFromFile("images/transparentPacman3Sprites.png");

    sf::IntRect rectSourceSprite(0, 0, 18, 16);
    sf::Sprite pacmanMC_sprite(texture2, rectSourceSprite); // sf::intrect(int rectleft, int recttop, int rectwidth, int rectheight)
    // pacmanMC_sprite.setScale(sf::Vector2f(1, 1));
    pacmanMC_sprite.move(sf::Vector2f(200, 345)); // set sprite at inital location point (140, 145)

    sf::Clock clock;

    //sets the origin of the sprite
    sf::Rect<float> size = pacmanMC_sprite.getGlobalBounds();
    pacmanMC_sprite.setOrigin(sf::Vector2f(size.width / 2, size.height / 2));

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

        if (clock.getElapsedTime().asSeconds() > 0.75f) // 0.07, used for making the mouth faster
        {
            if (rectSourceSprite.left == 34)
                rectSourceSprite.left = 0;
            else
                rectSourceSprite.left += 17; // pushes the sprite towards the left using x axis

            pacmanMC_sprite.setTextureRect(rectSourceSprite);
            clock.restart();
        }

        // pacmanMC_sprite movement
        // sprite keeps on moving to a different position when a the correct key is pressed for its rotation
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
        {
            pacmanMC_sprite.move(0.0f, -0.015f);
            pacmanMC_sprite.setRotation(-90.f);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
        {
            pacmanMC_sprite.move(0.0f, 0.015f);
            pacmanMC_sprite.setRotation(90.f);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
        {
            pacmanMC_sprite.move(-0.015f, 0.0f);
            pacmanMC_sprite.setRotation(-180.f);

        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
        {
            pacmanMC_sprite.move(0.015f, 0.0f);
            pacmanMC_sprite.setRotation(0);
        }

        renderWindow.clear();
        renderWindow.draw(background_sprite);
        renderWindow.draw(pacmanMC_sprite);
        renderWindow.display();
    }

    return 0;
}
0 Answers
Related