AnswerBun.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!

Related Questions

How to move a character from 1 to 3 squares on a grid?

0  Asked on January 23, 2021 by shunkashuu

     

Unity 2017.3.of3 Raycast2D randomly misses objects

1  Asked on January 15, 2021 by hesitatetowonder

     

How to fix the vibration while colliding?

1  Asked on January 13, 2021

   

Render order in 3/4 view game

0  Asked on January 10, 2021 by chuan-li

 

How can I control ads frequency?

1  Asked on January 10, 2021 by ruslan-plastun

   

Attach movement to each list object

1  Asked on January 9, 2021 by mavish

     

How to make a map surface object

1  Asked on January 8, 2021 by anonymous-entity

     

Coding Spelunky Edge Roll Mechanic

0  Asked on January 5, 2021 by austin-weaver

     

Creating a lookup table of datamaps in Harlowe/Twine

0  Asked on January 4, 2021 by the_e

   

Unity 4 – Some Rigidbodies Won’t Fall Asleep

5  Asked on January 3, 2021 by ryan-berserkguard-nrby

       

Unity PackageCache keeps having errors

0  Asked on December 31, 2020 by christopher-perry

 

Phaser Tiled map importing

1  Asked on December 30, 2020 by kabuto178

     

Change the colour of all particles on screen

1  Asked on December 27, 2020 by super-potato

   

How can you make a jump to a point using the Character Controller?

0  Asked on December 25, 2020 by aimon-z

   

Trouble destroying an SDL2 texture from another class

1  Asked on December 21, 2020 by gamer1

 

How to hide objects behind an invisible plane?

1  Asked on December 18, 2020 by hatinacat2000

       

Ask a Question

Get help from others!

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