SFML our first game

We are stuck on another issue. The program works fine except how do we make it so if someone is asked what is your name? They can't hit the letter Y or N to skip to the next question? Like I want when a question is asked, for only the keyboard presses to work while that slide is up. Any ideas? Kind hard to explain my question. Here's our code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int main()
{
    sf::Event event;
    sf::Font font;
    if (!font.loadFromFile("arialbd.ttf"))
    {
        cout << "Could not load the font";
    }
    sf::RenderWindow Window(sf::VideoMode(1200, 800), "Getting Text On The Screen");
      sf::String sentence;
        sf::Text text(sentence, font, 40);
        text.setColor(sf::Color::Blue);
        text.setFont(font);
        text.setString("What is your name?");
    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;
            case sf::Event::TextEntered:
                if(Event.text.unicode >= 32 &&Event.text.unicode <= 126)
                    sentence += (char)Event.text.unicode;
                else if(Event.text.unicode == 8 && sentence.getSize() > 0)
                    sentence.erase(sentence.getSize() - 1, sentence.getSize());
                text.setString(sentence);
                break;
            }
        }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
              text.setFont(font);
        text.setString("It's nice to meet you Chris!\n Would you like to go on an adventure with me?\n\n Type 'Y' for yes or 'N' for no");
}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Y)){
                text.setFont(font);
        text.setString("That's great news! Let's get started!");
            }
             if (sf::Keyboard::isKeyPressed(sf::Keyboard::N)){
                text.setFont(font);
        text.setString("Well that's no fun! I'll see you later.");
             }
        Window.clear(sf::Color::Black);
        Window.draw(text);
        Window.display();
    }
    }
Last edited on
Bump
Still stuck on this. It's been 3 days and we can't figure it out.
This is a very simple logic issue that requires you to track one piece of state. Namely, what are you doing right now? If you're doing thing a, don't do thing b. If you're doing thing b, don't do thing a.

I have a hard time believing you actually spent 3 days trying to figure it out (and have no updated code to show for it.)
We have tried so many different things I used to be able to get this to work using threads but I was informed that threads are really bad to use.

I feel like I'm close with bools but everytime I change something, another goes haywire.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

bool phase1 = true;
bool phase2 = false;

int main()
{
    sf::Event event;
    sf::Font font;
    if (!font.loadFromFile("arialbd.ttf"))
    {
        cout << "Could not load the font";
    }
    sf::RenderWindow Window(sf::VideoMode(900, 700), "Getting Text On The Screen");
      sf::String sentence;
        sf::Text text(sentence, font, 40);
        text.setColor(sf::Color::Blue);
        text.setFont(font);
        text.setString("It's nice to meet you!\nWould you like to go on an adventure with me?\n\n Type 'Y' for yes or 'N' for no");
    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;
            case sf::Event::TextEntered:
                if(Event.text.unicode >= 32 &&Event.text.unicode <= 126)
                    sentence += (char)Event.text.unicode;
                else if(Event.text.unicode == 8 && sentence.getSize() > 0)
                    sentence.erase(sentence.getSize() - 1, sentence.getSize());
                text.setString(sentence);
                break;
            }
        }
        if (phase1){
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Y)){
                text.setFont(font);
        text.setString("That's great news! Let's get started!");
        phase1 = false;
        phase2 = true;
            }

             if (sf::Keyboard::isKeyPressed(sf::Keyboard::N)){
                text.setFont(font);
        text.setString("Well that's no fun! I'll see you later.");
             }

        }

        if (phase2){

            text.setString("When do you want to start the trip?");


        }




        Window.clear(sf::Color::Black);
        Window.draw(text);
        Window.display();
    }
    }
Topic archived. No new replies allowed.