TransWikia.com

How can I make the text flash?

Game Development Asked on December 20, 2021

I want to have text that flashes from green to white and back to green over and over until the window is closed? Below is what I have tried but i cant seem to get it to work, please help me.

int main()
{
    sf::RenderWindow window(sf::VideoMode(LAYOUT_WINDOW_WIDTH, LAYOUT_WINDOW_HEIGHT),
        "Tetris 2019 - A New Beginning");

    sf::Font myFont;
    if (!myFont.loadFromFile("cartoon relief.ttf")) {

    }

    window.setFramerateLimit(30);

    sf::Text gameOver;
    gameOver.setFont(myFont);
    gameOver.setFillColor(sf::Color::Green);
    gameOver.setStyle(sf::Text::Regular);
    gameOver.setString("GAME OVER!");
    gameOver.setCharacterSize(65);
    gameOver.setPosition(30, 60);

    //everything below gets copied into the "processSplash" function
    while (window.isOpen())
    {
        sf::Time elapsed_time;
        sf::Clock r;

        sf::Time delta_time = sf::seconds(1);

        sf::Text playAgian;
        playAgian.setFont(myFont);
        playAgian.setFillColor(sf::Color::Green);
        while (window.isOpen && elapsed_time >= delta_time)
        {
            if (elapsed_time == delta_time)
            {
                playAgian.setFillColor(sf::Color::Green);
                elapsed_time == r.restart();
            }
            else
                playAgian.setFillColor(sf::Color::White);
                elapsed_time += r.restart();
        }
        playAgian.setStyle(sf::Text::Regular);
        playAgian.setString("Press ENTER to play agian!");
        playAgian.setCharacterSize(25);
        playAgian.setPosition(80, 235);

        sf::Event event;

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

            if (event.type == sf::Event::KeyPressed
                && event.key.code == sf::Keyboard::Space)

                //close splash screen and start game
            {
                /*processSplash();*/
            }
        }

        window.draw(gameOver);
        window.draw(playAgian);

        window.display();
    }


    return 0;
}

One Answer

there were a bunch of bugs in your code. This is a working example, there are many way of getting this implemented. In principle you use the same method as for animating a sprite.

For animations you need to capture the time. Or you will not be able to calculate the duration per frame. And you need to store the current frame in a variable. I put some comment in the example code below.

Btw, if you are not used to game loops, have a look on https://gafferongames.com/post/fix_your_timestep/

#include <SFML/Graphics.hpp>

int main(){
    int LAYOUT_WINDOW_WIDTH = 800;
    int LAYOUT_WINDOW_HEIGHT = 600;

    sf::RenderWindow window(sf::VideoMode(LAYOUT_WINDOW_WIDTH, LAYOUT_WINDOW_HEIGHT),
        "Tetris 2019 - A New Beginning");

    sf::Font myFont;
    if (!myFont.loadFromFile("cartoon relief.ttf")) {

    }

    window.setFramerateLimit(30);

    sf::Text gameOver;
    gameOver.setFont(myFont);
    gameOver.setFillColor(sf::Color::Green);
    gameOver.setStyle(sf::Text::Regular);
    gameOver.setString("GAME OVER!");
    gameOver.setCharacterSize(65);
    gameOver.setPosition(30, 60);

    sf::Text playAgain;
    playAgain.setFont(myFont);
    playAgain.setFillColor(sf::Color::Green);
    playAgain.setStyle(sf::Text::Regular);
    playAgain.setString("Press ENTER to play Again!");
    playAgain.setCharacterSize(25);
    playAgain.setPosition(80, 235);

    // Clock object to measure overall timing
    sf::Clock clock;
    sf::Event event;

    // Duration to control animation speed
    int currentColor = 1;
    float duration = float();

    //everything below gets copied into the "processSplash" function
    while (window.isOpen()){

        // How much time since last loop?
        sf::Time dt = clock.restart();
        duration += dt.asSeconds();

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

            if (event.type == sf::Event::KeyPressed
                && event.key.code == sf::Keyboard::Space){
                //close splash screen and start game
                /*processSplash();*/
            }
        }

        // Animation duration per frame (0.1f) reached
        if (duration > 0.01f){
          // Restart calculation of the duration
          duration = 0;

          // Loop through the animation colors
          if (currentColor < 255){
            currentColor += 5;
          } else {
            // Start from first frame if last frame reached
            currentColor = 0;
          }
          playAgain.setFillColor(sf::Color(0, currentColor, 0));
        }

        window.draw(gameOver);
        window.draw(playAgain);

        window.display();
    }


    return 0;
}

Answered by Martin Sand on December 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP