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

Author Topic: Need help! Weird flickering....  (Read 2532 times)

0 Members and 1 Guest are viewing this topic.

LazyLazuli

  • Newbie
  • *
  • Posts: 4
    • View Profile
Need help! Weird flickering....
« on: October 11, 2011, 12:59:48 am »
Hello! I really need help with my program....

Basically, it is a computer program version of Rock, Paper, Scissors, Lizard, Spock (BBT fans will know what I'm talking about XD)

However, something very weird happens and I've done all sort of things to fix it, but none of them work....

Here is the code:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    int x = 23;
    int Stage = 0;
    int PlayerChoice;
    int OpponentChoice;

    sf::Clock Clock;
    float ElapsedTime = Clock.GetElapsedTime();

    sf::RenderWindow Window(sf::VideoMode(800,600), "Rock, Paper, Scissors, Lizard, Spock");
    sf::Image iRock;
    if (!iRock.LoadFromFile("Rock.png"))
        return EXIT_FAILURE;

    sf::Image iPaper;
    if (!iPaper.LoadFromFile("Paper.png"))
        return EXIT_FAILURE;

    sf::Image iScissors;
    if (!iScissors.LoadFromFile("Scissors.png"))
        return EXIT_FAILURE;

    sf::Image iLizard;
    if (!iLizard.LoadFromFile("Lizard.png"))
        return EXIT_FAILURE;

    sf::Image iSpock;
    if (!iSpock.LoadFromFile("Spock.png"))
        return EXIT_FAILURE;

    sf::Image iWin;
    if (!iWin.LoadFromFile("Win.png"))
       return EXIT_FAILURE;

    sf::Image iLose;
    if (!iLose.LoadFromFile("Lose.png"))
        return EXIT_FAILURE;

    sf::Image iTie;
    if (!iTie.LoadFromFile("Tie.png"))
        return EXIT_FAILURE;

    sf::Sprite sRock (iRock);
    sf::Sprite sPaper (iPaper);
    sf::Sprite sScissors (iScissors);
    sf::Sprite sLizard (iLizard);
    sf::Sprite sSpock (iSpock);

    sf::Sprite sOpponentRock (iRock);
    sf::Sprite sOpponentPaper (iPaper);
    sf::Sprite sOpponentScissors (iScissors);
    sf::Sprite sOpponentLizard (iLizard);
    sf::Sprite sOpponentSpock (iSpock);

    sf::Sprite sWin (iWin);
    sf::Sprite sLose (iLose);
    sf::Sprite sTie (iTie);

    sRock.SetCenter(75.f,75.f);
    sPaper.SetCenter(75.f,75.f);
    sScissors.SetCenter(75.f,75.f);
    sLizard.SetCenter(75.f,75.f);
    sSpock.SetCenter(75.f,75.f);

    sOpponentRock.SetCenter(75.f,75.f);
    sOpponentPaper.SetCenter(75.f,75.f);
    sOpponentScissors.SetCenter(75.f,75.f);
    sOpponentLizard.SetCenter(75.f,75.f);
    sOpponentSpock.SetCenter(75.f,75.f);

    sWin.SetCenter(200,100);
    sLose.SetCenter(200,100);
    sTie.SetCenter(200,100);

    sWin.SetPosition(400,300);
    sLose.SetPosition(400,300);
    sTie.SetPosition(400,300);

    //Creates starting words
    sf::String Text1;
    Text1.SetText("Choose Rock, Paper, Scissors,\nLizard or Spock to begin playing");
    Text1.SetColor(sf::Color(0,0,0));
    Text1.SetSize(30);

    //Text used to declare "You have chose...."
    sf::String Text2;
    Text2.SetColor(sf::Color(0,0,0));
    Text2.SetSize(30);

    //Text used to declare "The opponent has chosen..."
    sf::String Text3;
    Text3.SetColor(sf::Color(0,0,0));
    Text3.SetSize(30);
    Text3.SetPosition(500,0);

    //Text used to declared "try again?"
    sf::String Text4;
    Text4.SetText("Press Enter to try again");
    Text4.SetColor(sf::Color(0,0,0));
    Text4.SetPosition(200,500);


    while ((Window.IsOpened()))

    {
        sf::Event Event;
        while (Window.GetEvent(Event))
        {
            //If the close button is pressed, the window closes
            if (Event.Type == sf::Event::Closed)
                Window.Close();
        }

        //create variables to hold coordinates for X and Y (used later for buttons)
        int MouseX = Window.GetInput().GetMouseX();
        int MouseY = Window.GetInput().GetMouseY();

        float ElapsedTime = Clock.GetElapsedTime();

        switch (Stage)
        {
            case 0:
            {
                //Clear window to White
                Window.Clear(sf::Color(255,255,255));

                //Display StartText
                Window.Draw(Text1);

                //set Positions so that sprites line up along bottom edge
                sRock.SetPosition(83.f,517.f);
                sPaper.SetPosition(239.f,517.f);
                sScissors.SetPosition(395.f,517.f);
                sLizard.SetPosition(551.f,517.f);
                sSpock.SetPosition(707.f,517.f);

                //Draw all 5 sprites
                Window.Draw(sRock);
                Window.Draw(sPaper);
                Window.Draw(sScissors);
                Window.Draw(sLizard);
                Window.Draw(sSpock);

                //Each Sprite is 150 x 150
                //if Rock is selected (Rock is located at 82, 517)
                if ((Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && (MouseX > 8) && (MouseX < 158) && (MouseY > 442) && (MouseY < 592))
                {
                    PlayerChoice = 1;
                    Stage = 1;
                }

                //if Paper is selected (Paper is located at 239, 517)
                if ((Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && (MouseX > 164) && (MouseX < 314) && (MouseY > 442) && (MouseY < 592))
                {
                    PlayerChoice = 2;
                    Stage = 1;
                }

                //if Scissors is selected (Scissors is located at 395, 517)
                if ((Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && (MouseX > 320) && (MouseX < 470) && (MouseY > 442) && (MouseY < 592))
                {
                    PlayerChoice = 3;
                    Stage = 1;
                }

                //if Lizared is selected (Lizard is located at 551, 517)
                if ((Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && (MouseX > 476) && (MouseX < 626) && (MouseY > 442) && (MouseY < 592))
                {
                    PlayerChoice = 4;
                    Stage = 1;
                }

                //if Spock is selected (Spock is located at 707, 517)
                if ((Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) && (MouseX > 632) && (MouseX < 782) && (MouseY > 442) && (MouseY < 592))
                {
                    PlayerChoice = 5;
                    Stage = 1;
                }

                Window.Display();

                break;
            }//switch: case 0 closing brace (first part, where we choose which option)

            case 1:
            {
                //clear window to white
                Window.Clear(sf::Color(255,255,255));

                //determins which option player has chosen, and display text based on choice
                switch (PlayerChoice)
                {
                    case 1:
                    {
                        Text2.SetText("You have chosen Rock");
                        Window.Draw(Text2);
                        sRock.SetPosition(100,300);
                        Window.Draw(sRock);
                        OpponentChoice = sf::Randomizer::Random(1,5);
                        break;
                    }

                    case 2:
                    {
                        Text2.SetText("You have chosen Paper");
                        Window.Draw(Text2);
                        sPaper.SetPosition(100,300);
                        Window.Draw(sPaper);
                        OpponentChoice = sf::Randomizer::Random(1,5);
                        break;
                    }

                    case 3:
                    {
                        Text2.SetText("You have chosen Scissors");
                        Window.Draw(Text2);
                        sScissors.SetPosition(100,300);
                        Window.Draw(sScissors);
                        OpponentChoice = sf::Randomizer::Random(1,5);
                        break;
                    }

                   case 4:
                   {
                       Text2.SetText("You have chosen Lizard");
                       Window.Draw(Text2);
                       sLizard.SetPosition(100,300);
                       Window.Draw(sLizard);
                       OpponentChoice = sf::Randomizer::Random(1,5);
                       break;
                   }

                   case 5:
                   {
                       Text2.SetText("You have chosen Spock");
                        Window.Draw(Text2);
                       sSpock.SetPosition(100,300);
                        Window.Draw(sSpock);
                        OpponentChoice = sf::Randomizer::Random(1,5);
                       break;
                    }
                }//switch (player choice) closing brace, for display text of which choice
                Stage = 2;
               break;
            }//switch: case 1 closing brace (second part, display player choice)

            case 2:
            {
                switch (OpponentChoice)
                {
                    case 1:
                    {
                        Text3.SetText("Your Opponent has \nchosen Rock");
                        Window.Draw(Text3);
                        sOpponentRock.SetPosition(700,300);
                        Window.Draw(sOpponentRock);
                        break;
                    }

                    case 2:
                    {
                        Text3.SetText("Your Opponent has \nchosen Paper");
                        Window.Draw(Text3);
                        sOpponentPaper.SetPosition(700,300);
                        Window.Draw(sOpponentPaper);
                        break;
                    }

                    case 3:
                    {
                        Text3.SetText("Your Opponent has \nchosen Scissors");
                        Window.Draw(Text3);
                        sOpponentScissors.SetPosition(700,300);
                        Window.Draw(sOpponentScissors);
                        break;
                    }

                    case 4:
                    {
                        Text3.SetText("Your Opponent has \nchosen Lizard");
                        Window.Draw(Text3);
                        sOpponentLizard.SetPosition(700,300);
                        Window.Draw(sOpponentLizard);
                        break;
                    }

                    case 5:
                    {
                        Text3.SetText("Your Opponent has \nchosen Spock");
                        Window.Draw(Text3);
                        sOpponentSpock.SetPosition(700,300);
                        Window.Draw(sOpponentSpock);
                        break;
                    }
                }//(switch opponent choice for when determining what Opponent has chosen)

                Stage = 3;

                break;
            }//switch: case 2 closing brace (3rd part, where display opponent choice)

            case 3:
            {
                //Both players choose same option, it is a tie
                if (PlayerChoice == OpponentChoice)
                {
                    Window.Draw(sTie);
                }

                else
                {
                    switch (PlayerChoice)
                    {
                        case 1: //if Player chooses Rock
                        {
                            switch (OpponentChoice) //for what opponent chooses if player chooses rock
                            {
                                case 2: //Opponent chooses Paper
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 3: //Opponent chooses Scissors
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 4: //Opponent chooses Lizard
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 5: //Opponent chooses Spock
                                {
                                    Window.Draw(sLose);
                                    break;
                                }
                            }//Switch (opponentchoice) for if player chooses rock)
                            break;
                        }//  case 1 closing brace for if player chose rock

                        case 2: //if Player chooses Paper
                        {
                            switch (OpponentChoice)
                            {
                                case 1: //Opponent choose Rock
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 3://Oppoent chosoes Scissors
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 4: //Opponent chooses Lizard
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 5: // Oppoent chooses Spock
                                {
                                    Window.Draw(sWin);
                                    break;
                                }
                            }//Switch (opponentchose) for if player chose paper
                            break;
                        }//case 2 for if player choose paper

                        case 3: //if player chooses Scissors
                        {
                            switch (OpponentChoice)
                            {
                                case 1: //if player choose Rock
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 2: //if player chooses scissors
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 4: //f player chooses lizard
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 5:// if player chooses spock
                                {
                                    Window.Draw(sLose);
                                    break;
                                }
                            }//switch closing brace for if player chooses paper
                            break;
                        }//case 3 for if player chooses paper

                        case 4: //if player chooses lizard
                        {
                            switch (OpponentChoice)
                            {
                                case 1: //oppoennt Rock
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 2: //opponent paper
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 3: //Opponent Scissors
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 5: //Opponent Spock
                                {
                                    Window.Draw(sWin);
                                    break;
                                }
                            }//switch for if player chooses lizard
                            break;
                        }//case 4 for if player chooses lizard

                        case 5: //if player chooses spock
                        {
                            switch (OpponentChoice)
                            {
                                case 1: //Oppoent rock
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 2: //Opponent PAper
                                {
                                    Window.Draw(sLose);
                                    break;
                                }

                                case 3: // Oppoennt Scissors
                                {
                                    Window.Draw(sWin);
                                    break;
                                }

                                case 4: // Opponent Lizard
                                {
                                    Window.Draw(sLose);
                                    break;
                                }
                            }//switch for if player chooses spock
                            break;
                        }//case 5 if player chooses spock
                    }//Switch (playerchoice) for determining what optiong player choice
                }//else closing brace (for if player choice doesn't == opponent choice
                Stage = 4;
                Window.Display();

            }// case 3 closing brace (4th part, determining whether player wins or loses)

            case 4:
            {

                Window.Draw(Text4);
                Window.Display();
                if ((Window.GetInput().IsKeyDown(sf::Key::Return)))
                {
                    Stage = 0;
                }
         
                Window.Display();
                break;

            }//case 4 closing brace (5th part, try again)

            default:
            {
                Window.Clear(sf::Color(255,0,0));
            }
        }//Switch (stage) closing brace
    }//while Window.IsOpened() closing brace
    return 0;
}//int main closing brace


So, when I load the program, it starts on the first part of the Switch (stage) part, Case 0. Then I can click either Rock ,Paper, Scissors, Lizard or Spock, and then it moves on to case 1, 2, and 3 where it displays "You have chosen...", "Your Opponent has chosen..." and whether I won, lost, or tied. However, at that point, it does this weird thing where it keeps flickering really really fast. I've screenshotted, and from what I can tell, it keeps switching from displaying the  "please choose...." part and the "you have chosen... your opponent has chosen.... you win!" part really really fast. I can see the text for "Press enter to try again", and when I press enter, it goes back to the beginning of the program normally, but then it does the flickering thing again.

Please help!

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Need help! Weird flickering....
« Reply #1 on: October 11, 2011, 05:28:19 am »
That's a lot of nested switch statements there.

You should step through your code with the debugger then you'll know exactly what's happening.

Also you really should split that main into smaller functions, so that your switch statements are less confusing.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Need help! Weird flickering....
« Reply #2 on: October 11, 2011, 07:56:27 am »
Your program is very confusing, your logic and drawing codes are mixed together. You should separate them clearly: write a simple game loop, with event handling and drawing, and then put your logic in a separate function/class. This way you can maintain both more easily.

That should solve your problem (which is most likely that Window.Display() isn't called at every iteration of the main loop).

By the way, your AI is broken: it should always choose Spoke :lol:
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Need help! Weird flickering....
« Reply #3 on: October 18, 2011, 02:38:29 pm »
A HUGE recommendation is learning object-oriented C++, that is, using classes, structs and namespaces for you application. It creates structure and is much easier and more effiecent to work with.

To begin with, create a baseclass wich has a public Run function, and private Update and Draw functions, and private objects like VideoManager, ResourceManager etc. That way your main.cpp will look like this;

Code: [Select]
#include "include/MyStuff.hpp"
int main(){
   MyStuff Root;
   Root.Run();
   return 0;
}


And the deeper you get, the more highlevel stuff you can change (Like a players position).

 

anything