0

First of all let me state that I am new to C++. I received an assignment to make a mini "snake" game meaning I need to make a grid and have a box move around in it. It is a simple 1x1 box, and no fruits to catch or anything.

I am able to create the grid, and have a box exist in it that I can move up, down, left, right using user input. My problem is I need the box to move automatically every second in the direction it was headed. I have no problems with the sleep command (I think), my problem is that when I am waiting for the user to enter his direction the program stops until that input is given. I need a way to have the box continue moving while I wait for input. For this I need either to just have the code continue while waiting for the input, or to redirect it to doing a function while waiting for input.

I have been googling this and trying things for some hours now and all I keep finding is multi-threading. I tried using thread, pthread, boost/thread.hpp but I can't get any of them to work.

In any case multi-threading is way more advanced than what my class is doing and there is no chance that is what my professor want's us to do. He said something about having a function of cin that allows such things, but I can't find it anywhere.

Is there a simple way of doing this?

Oha Noch
  • 374
  • 1
  • 7
  • 22
  • 2
    This does not exist in standard C++ but various platforms may provide their own ways. What platform and compiler are you using? – n. m. could be an AI Mar 08 '16 at 19:52
  • If you want to get the input as soon as possible (e.g. without waiting or ``) it's platform dependent. On Windows, I've used ``, but I have no idea what one would use on Linux. Maybe curses or similar. – Zeta Mar 08 '16 at 19:52
  • 4
    Follow the track "non-blocking IO"... – Jean-Baptiste Yunès Mar 08 '16 at 19:55
  • Maybe you'd like to look into SDL for platform independent solutions (in case professional solutions matter to you) – sperber Mar 08 '16 at 19:56
  • I am using g++ on Linux Mint. If you need me to be more specific please tell me how to get the information, because I don't know how to check it.. – Oha Noch Mar 08 '16 at 20:12
  • 2
    Many possible solutions here: http://stackoverflow.com/questions/6171132/non-blocking-console-input-c . As @Jean-BaptisteYunès said, the keyword to google is non-blocking IO – user4581301 Mar 08 '16 at 20:19
  • There is no function in cin that can do this. One might think peek() can be used but in case of cin it also blocks. – rustyx Mar 08 '16 at 21:04

1 Answers1

0

From what I'm understanding from the question, it seems that you need to implement a timer of sorts that will move the move the box every second. I'm not sure if g++ has something similar to System::Timers as visual studio does but here's a link that shows how to make a timer yourself (though it's written in C). With a timer in place (created, started, and your code to get input and moving of the box happens within the timer's tick function) you could use a following if statement to keeping moving the box. Here's a link to the .NET's timer for more information on how it works. This has code for a timer class that Matthew Hoggan has created for C++ in linux. Hope this helps.

if (user input)
{
    change direction
}
move the box
Community
  • 1
  • 1
Havik IV
  • 193
  • 4
  • 12
  • Thanks for the answer, but I still think I will be stuck with the same problem. My problem is not making it move every second, my problem is that I need to make it move every second while I am waiting for input. Even using you timers I will still have to use multi-threading to make them run in the background while I wait for the input, no? – Oha Noch Mar 09 '16 at 05:35
  • Yes, the timer will run on a separate thread, but it should just run automatically without you really having to do much other than including the class and creating a timer object and starting it. From looking at Matthew's code, you would have to create a function that does the moving of the box and check for input from the user, and then pass the function to the timer. Something like t.addTimer(seconds, function); – Havik IV Mar 09 '16 at 07:11