Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help with making a 'Simon' game on Qt?
Qt 6.11 is out! See what's new in the release blog

Help with making a 'Simon' game on Qt?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    acedawg45
    wrote on last edited by
    #1

    So, this game is pretty simple. There is a QLabel in the middle of the window, and every 3 seconds, the QLabel changes text to a different color. The first round, it does this 3 times. Second one, 4 times. Third, 5 times. And so on. I have three buttons under it labeled "Green", "Blue", and "Red".The color the QLabel changes to are one of those three. On round 1, after the colors are done changing, you click the buttons in the order the colors came on the QLabel. Here are the methods that I am using, so correct me if I should use something else (method meaning technique):
    *In a for loop, I am changing the label with setText() every 50 loops, or my estimated three seconds.
    *Setting a rand() from <cstdlib> to make sure the colors are random (and I did generate the random seed with srand(time(NULL)))

    So, anything you guys can help me with? This forum has so far proven to be very helpful.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      I think it's better to use a QTimer instead of a for loop. A for loop has disadvantages that while you're in it, no other code is executed. Also you have guess the number of iterations you need to get 3 seconds and this can depend on how fast or slow the computer you're running it on.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        Yeah, using a loop is probably the most inefficient and unreliable method to emulate a "sleep" command. I remember that I used this exactly same method back when I started coding on my old C64, because I didn't know better back at that time. If you need to wait for a certain amount of time, just use Sleep() on Windows or msleep() on Linux. This is much more reliable and it won't keep the CPU busy. But even that method will still block your thread and make your GUI "freeze". So you better use an event driven approach. As t3685 has suggested, you can use "QTimer":http://qt-project.org/doc/qt-4.8/qtimer.html for this purpose.

        @//Constructor
        MyWindow::MyWindow
        {
        m_timer = new QTimer(this)
        m_timer->setInterval(50);
        connect(m_timer, SIGNAL(timeout()), this, SLOT(timerDone()));
        m_timer->start();
        }

        //Slot function
        MyWindow::timerDone(void)
        {
        /* this will be called every 50 milliseconds! */
        }@

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • A Offline
          A Offline
          acedawg45
          wrote on last edited by
          #4

          Thanks. I remember hearing about timers, even thought it would be a good idea for this program. But I never knew what to do, so I figured I would just use a for loop. Obviously, like you said, it was inefficient

          1 Reply Last reply
          0
          • A Offline
            A Offline
            acedawg45
            wrote on last edited by
            #5

            Okay, so I tried a QTimer, and it isn't working. it says:
            QObject::connect: No such slot MainWindow::timerDone() in ..\Simon_Game\mainwindow.cpp:13

            I think it is because it isn't finding timerDone()? Any suggestions?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

              Of course you actually need to add a Slot of the name timerDone() to your class, if you which to connect to that Slot. Try like this:

              @/* main_window.h /
              class MainWindow
              {
              /
              [...] */

              public slots:
              void timeDone(void);

              /* [...] */
              };

              /* main_window.cpp */
              void MainWindow::timeDone(void)
              {
              //Your code here...
              }@

              See also:
              http://qt-project.org/doc/qt-4.8/signalsandslots.html

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xander84
                wrote on last edited by
                #7

                Did you declare your timerDone method as a slot? maybe show some code :)

                it should look like
                @
                private slots:
                void timerDone();
                @
                or something like that

                If you prefer you could also use the build in timer in every QObject (instead of a QTimer), see "QObject::startTimer":http://qt-project.org/doc/qt-5/qobject.html#startTimer.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  acedawg45
                  wrote on last edited by
                  #8

                  Oh. Yeah. Whoops. Brainfart haha. Thanks for the heads up. I completely forgot about doing that.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved