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

Author Topic: How to create a simple pause if win condition is true?  (Read 1655 times)

0 Members and 1 Guest are viewing this topic.

enderboy768

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
How to create a simple pause if win condition is true?
« on: June 08, 2017, 03:18:00 pm »
I wanted to create a simple pause in my pong game so that if any of the player reach a certain score, the game will freeze and a text shows up saying which player has won the game and if a certain key is pressed the game will restart. I tried this: (sorry if it's too simple, I'm a complete newbie  :-[)

Code: [Select]
// ball went through bottom of the window
        if(ball.getPosition().top > windowHeight)
        {
            ball.initPos();
            bat1.initPos();
            bat2.initPos();
            lives1--; // p1's life is negated

            // life check, reset values if true
            if(lives1 < 1)
            {
                score2++;
                lives1 = 3;
            }
        }

        // ball went through top of the window
        if(ball.getPosition().top < 0)
        {
            ball.initPos();
            bat1.initPos();
            bat2.initPos();
            lives2--; // p2's life is negated

            // life check, reset values if true
            if(lives2 < 1)
            {
                score1++;
                lives2 = 3;
            }
        }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to create a simple pause if win condition is true?
« Reply #1 on: June 08, 2017, 03:27:46 pm »
Not sure what the posted code has to do with the question. You should make use of [code=cpp][/code] tags when posting C++ code, for nicer embedding and highlighting.

For your simple situation, you can introduce a boolean that tracks whether the game has ended.
Then you put an if block around the updating functionality for the ball and the paddle (and the AI).
And finally you put an if block after the paddle and ball drawing, to render the win/lose text.
As long as the game isn't finished, you update the paddle, ball and AI.
Once it is finished you draw the text over everything else.

A more general (and scalable) solution is to use some sort of a state machine.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How to create a simple pause if win condition is true?
« Reply #2 on: June 08, 2017, 04:36:14 pm »
Have a m_won or something boolean and if it's true do no updates except for event handling. Split updating and drawing into update() and draw() functions that do it all, quit from update() after handling events and so on and if m_win is true in draw() also draw that text, do not update any animations you had, etc.

You can take a look at my snake clone to see exactly what I mean and how easily, clearly and quickly it can be done:
https://en.sfml-dev.org/forums/index.php?topic=22048.0
Back to C++ gamedev with SFML in May 2023

 

anything