how to make my snake move?

am trying to make snake game. I stuck with a small problem.

without making any input, program wont proceed for next step. I need to keep the keys pressing to maintain its moving. is there any function or any other thing which may solve my problem?
The program, it must not wait for the input, keep working/moving without any input and when i give input it must work with it.i am not using graphics.h

i am not using graphics.h
There's an infinite amount of things you might not be using; what are you using? What system, what library?
Your question as it is is all too vague, so any answer is going to be vague as well.

You need to find the OS specific function(s) that allow you to read a key asynchronously, so the program gets a key input whenever you hold a key down.
multi threading
Event loops do not require multi-threading.
im not like a pro or anything but i made a pong game like a week ago and the way i solved the issue was multi threading.

i used one thread to monitor keyboard input and do what needed to happen when a key was pressed. I used other threads to monitor various other aspects of the game. I'm not familiar with events in c++ but in my simple mind i'd imagine registering for an event just basically opens a thread that waits for the event to happen and then when it happens does what it needs to do. I have no idea if when an event happens if the main thread jumps into the function that registered for the event or whether the event runs in parallel with the main thread. Thats definetly an option tho. Something to look into.

the only thing i can say about my recent experience with multithreading is keep the threads relatively light bc i was running into issues where I was getting unexpected results just bc the thread had too much going on. And i'm not even talking about 50 or even 20 lines of code.

like going from 8 lines of code to 4 fixed it. Obviously the contents of the lines is important but i was like doing some simple checks like if a > this and b > this do this else if this this and this. But it was like the thread was failing to evaluate the code correctly or too slow to catch what was happening accurately enough

this is an example of what i did for keyboard input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
void monitor_player_input() {
		std::string in;

		if (GetKeyState(VK_RIGHT) < 0) {
			if (this->player1.pad.posX < (this->gameX - (this->player1.pad.size / 2)) - 2) {
				this->player1.pad.posX++;
			}
		}

		else if (GetKeyState(VK_LEFT) < 0) {
			if (this->player1.pad.posX > (2 + (this->player1.pad.size / 2))) {
				this->player1.pad.posX--;
			}

		}
		else if (GetKeyState(VK_ESCAPE) < 0) {  //this is broken need like System("pause") or something to get it to stop
			std::cout << "PAUSED.....for a list of commands type enter help" << std::endl;
			std::cin >> in;
			if (in == "help") {
				//do stuff 
			}

		}
	}

void ThreadPlayerInput(ball b) {
	while (b.reset == false) {
		b.monitor_player_input();
		Sleep(40);
	}

}

int main(){

ball _ball;

_ball.handle4 = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)ThreadPlayerInput, &_ball, 0, 0);

	if (!_ball.handle4) {
		std::cout << "error thread failed" << "\n";
		std::cin;
	}
while(1){
//main thread
}

CloseHandle(_ball.handle4);


}


probably not the textbook way of doing it but it works. mind you the actual function called by the thread loop was inside of a struct called ball and &_ball is pointer to the struct object used as a parameter that is sent to the thread function.
Last edited on
threading is a good answer. you can also use library non-blocking I/O routines to get the user input, which basically see if a key was pressed and if it was, handle it, if not, keep on going. That lets you do it without threading so long as the program is not too intense for one thread.

c++ does not have a standard non blocking keyboard I/O so you need to use something for your OS here.
Last edited on
I made an array as big as console, in which the snake floats.Used the keys like w,a,s,d for directions. to travel in a direction, i need to hold any key. i dont have an idea how to make it run itself. And have not heard about threading.
OS: windos 8 pro.
Compiler: turbo c++
header files: iostream for input, output; stdlib for random(); conio to colour the output.

i will see to what is threading.
Threading with Turbo C++ is going to be hard if not impossible. You won't be able to use the <thread> library in C++11. You might be able to use Boost's thread library, though it looks like that is C++03 and later.

If you can use the Win32 SDK there is Windows-based thread support available. As well as color support for the console.

Turbo C++ is going to seriously make the task you want to accomplish MUCH harder.
Last edited on
i found my self hard to handle other compilers,but not a problem...tutorials may manage it.But afraid to shift to any other, as i may confuse and stuck in the middle where i cant go or return back to my old compiler.

So i need to start using any modern compilers...
Last edited on
Idk anything about turbo c++ or how to solve your issues there. But when I was doing my display I just used the dimensions of the window as the x and y. Figured out what i wanted the objects to be printed out to look like then just saved their default positions in variables then cout the x and y in a loop as if it were an array and just printed the objects when the cursor position intersects with a the starting position of an object. I just figured it made more sense to update a handful of variables rather than update an array when something changed when the whole thing was just going to be updated then printed out anyways. Kinda saves a step. If you're updating the array then you already have the positions of the stuff you want.
Last edited on
So i need to start using any modern compilers...

That might be a good idea. No need to get rid of Turbo C++ as you test-drive various compilers/IDEs (integrated development environments).

There are several available. Free or paid.

Two common IDE choices for creating C/C++ programs:

1. Visual Studio 2019 Community (it's free).
https://visualstudio.microsoft.com/downloads/

2. Code::Blocks 20.03 (it's also free)
http://www.codeblocks.org/

There's an tutorial website, Learn C++, that can help you get over the hurdles of learning a new IDE. It has lessons on how to configure either Visual Studio or Code::Blocks:
https://www.learncpp.com/cpp-tutorial/installing-an-integrated-development-environment-ide/
https://www.learncpp.com/cpp-tutorial/writing-your-first-program/

An IDE is not necessary for creating programs, there is command-line compiling. That is not something for beginners, though IMO.
Pretty sure turbo has a nonblocking I/O in there. But yes, a modern compiler would be a huge deal to your studies if you can manage it. I also think borland has a threading library in there but I may be mixing turbo and builder. I know builder had something, but can't recall turbo.

a simple batch file can compile a half dozen cpp file homework project without any major woes. Its not that big a deal for small stuff, but large complicated compiles by command line are a bit rough.
Last edited on
Topic archived. No new replies allowed.