1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
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();
}
}
|