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

Author Topic: turn-based countdown  (Read 115 times)

0 Members and 1 Guest are viewing this topic.

C_Worm

  • Newbie
  • *
  • Posts: 9
    • View Profile
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.

Sub

  • Full Member
  • ***
  • Posts: 143
    • View Profile
Re: turn-based countdown
« Reply #1 on: April 03, 2019, 01:46:10 am »
Each time you restart your clock, you're throwing away any time over 1 second.  This is going to accumulate more and more causing your countdown to be inaccurate. 

I modified your code to count down without any accumulation issues.  Also, you don't need the header guard in a .cpp file.

Code: [Select]
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
#include <cmath>

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::Text text;
text.setFillColor(sf::Color::Red);
text.setFont(font);

sf::Event events;
sf::Clock clock;
while (window.isOpen())
{
sf::Time time = clock.getElapsedTime();
const int countdownNumberOfSeconds = 10;
const int timeleft = countdownNumberOfSeconds - std::floor(time.asSeconds());

text.setString(std::to_string(timeleft));

if (timeleft <= 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();
}


}
« Last Edit: April 03, 2019, 01:48:52 am by Sub »