1

im a c++ beginner and im trying to make a flappy game out of scratch, im doing good but don't know how to check input during the sleep function from window.h

while (GameStarted) {
        std::cin >> answer;
        Sleep(DurationOfATick);
        system("CLS");
        if (answer.size() != 1) {
            Player.SetPosition(Player.GetPosition().x, Player.GetPosition().y - 1);
        }
        else {
            Player.SetPosition(Player.GetPosition().x, Player.GetPosition().y + 1);
        }
        DrawFrame(Player, YPositionDrawn);
        TicksCounted++;
        std::cout << "Tick!" << TicksCounted << '\n';
    }

I tried to add a std::cin << answer but for some reason it paused until it had a input, any help??

  • 1
    This cannot be done in standard C++, but requires use of operating system-specific functions. But before getting into operating system-specific, low level APIs, doesn't it make sense to learn core C++ fundamentals first, like classes, inheritance, virtual inheritance, polymorphism, overloading, template, concepts, iterators, containers, algorithms, and plenty more? There's a lot to learn about core C++ fundamentals, first, before getting into specialized domains, like game development. – Sam Varshavchik Dec 25 '22 at 23:20
  • std::cin will not work for your window. It is standart input, used basically for terminal or console input. std::cin usually just suspend execution of the thread untill user press Enter in terminal. What library/API do you use to draw your window? Most likely you should have some functions from that lib to make an infinite event loop. Something like PoolEvent or GetMessage/TranslateMessage/DispatchMessage. And some callbacks that you may be able to define to process such events (resize, redraw, mouse move, key pressed). The thing you actually looking for is callback for KeyPressed event. – Yuri Dolotkazin Dec 26 '22 at 00:57

0 Answers0