16
General / turn-based countdown
« on: April 02, 2019, 10:25:21 pm »
#ifndef SFML_STATIC
#define SFML_STATIC
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "TITLE", sf::Style::Close | sf::Style::Resize);
window.setFramerateLimit(60);
sf::Font font;
if (!font.loadFromFile("Enchanted Land_0.otf"))
{
}
sf::Clock clock;
sf::Time time;
int seconds{ 10 };
std::string counter = std::to_string(seconds);
sf::Text text;
text.setFillColor(sf::Color::Red);
text.setString(counter);
text.setFont(font);
sf::Event events;
clock.restart();
while (window.isOpen())
{
time = clock.getElapsedTime();
std::cout << (int)time.asSeconds() << std::endl;
if ((int)time.asSeconds() == 1)
{
counter = std::to_string(seconds);
text.setString(counter);
--seconds;
clock.restart();
if (seconds < 0)
{
text.setString("GAME OVER");
text.setPosition(window.getSize().x / 2, window.getSize().y / 2);
}
}
while (window.pollEvent(events))
{
if (events.type == sf::Event::Closed)
{
window.close();
}
if (events.type == sf::Event::KeyPressed)
{
if (events.key.code == sf::Keyboard::A)
{
window.close();
}
}
}
window.clear(sf::Color::Blue);
window.draw(text);
window.display();
}
}
#endif
Does anyone know a better/more efficient way to make a countdown implementation?
Or am i on the right track with this?
The thing is that i need a countdown for a turn-based game state.
each player has 10 seconds at her disposal before the the turn changes to the second player.
the countdown should be visible somewhere on the screen like it is here.
#define SFML_STATIC
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "TITLE", sf::Style::Close | sf::Style::Resize);
window.setFramerateLimit(60);
sf::Font font;
if (!font.loadFromFile("Enchanted Land_0.otf"))
{
}
sf::Clock clock;
sf::Time time;
int seconds{ 10 };
std::string counter = std::to_string(seconds);
sf::Text text;
text.setFillColor(sf::Color::Red);
text.setString(counter);
text.setFont(font);
sf::Event events;
clock.restart();
while (window.isOpen())
{
time = clock.getElapsedTime();
std::cout << (int)time.asSeconds() << std::endl;
if ((int)time.asSeconds() == 1)
{
counter = std::to_string(seconds);
text.setString(counter);
--seconds;
clock.restart();
if (seconds < 0)
{
text.setString("GAME OVER");
text.setPosition(window.getSize().x / 2, window.getSize().y / 2);
}
}
while (window.pollEvent(events))
{
if (events.type == sf::Event::Closed)
{
window.close();
}
if (events.type == sf::Event::KeyPressed)
{
if (events.key.code == sf::Keyboard::A)
{
window.close();
}
}
}
window.clear(sf::Color::Blue);
window.draw(text);
window.display();
}
}
#endif
Does anyone know a better/more efficient way to make a countdown implementation?
Or am i on the right track with this?
The thing is that i need a countdown for a turn-based game state.
each player has 10 seconds at her disposal before the the turn changes to the second player.
the countdown should be visible somewhere on the screen like it is here.