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?

7
  • 2
    This does not exist in standard C++ but various platforms may provide their own ways. What platform and compiler are you using? Commented Mar 8, 2016 at 19:52
  • If you want to get the input as soon as possible (e.g. without waiting or <RET>) it's platform dependent. On Windows, I've used <conio.h>, but I have no idea what one would use on Linux. Maybe curses or similar. Commented Mar 8, 2016 at 19:52
  • 4
    Follow the track "non-blocking IO"... Commented Mar 8, 2016 at 19:55
  • Maybe you'd like to look into SDL for platform independent solutions (in case professional solutions matter to you) Commented Mar 8, 2016 at 19:56
  • 2
    Many possible solutions here: stackoverflow.com/questions/6171132/… . As @Jean-BaptisteYunès said, the keyword to google is non-blocking IO Commented Mar 8, 2016 at 20:19

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.