Below is the code I wrote, for taking the arrow key imput, just wanted to share it and get some feedback thanks :)

#include <iostream>
#include <conio.h>


using namespace std;

int main()
{
	int number = 0;
	int number2 = 0;
	
	
	while(true)
	{
	
	int arrow = getch();
	
		if (arrow == 77)
		{
				number = number + 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
		if (arrow == 80)
		{																	
			number2 = number2 + 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
			if (arrow == 75)
		{																	
			number = number - 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
		if (arrow == 72)
		{																	
			number2 = number2 - 1;
				system("cls");
				
				for (int i = 0; i < number2; i++)
				{
					cout << endl;
				}
				for (int i = 0; i < number; i++)
				{
					cout << " ";
				}
				cout << "*"  ;
		}
	}
	
	
}

Dani AI

Generated

Nice start from and a key correction from about special keys coming as a two-step read. Two quick improvements make this approach much more useful for a console "snake" prototype: avoid full-screen clears (system("cls")) to stop flicker, and separate input from movement so the star keeps moving on a timer instead of only when a key is pressed.

The snippet below shows a compact Windows-centric pattern that uses non-blocking input (_kbhit/_getch), reads arrow scancodes properly (first a prefix, then the real code), and updates the display by moving the console cursor rather than clearing the whole screen. It also clamps movement to screen bounds and uses a fixed tick rate so movement is smooth and predictable.

#include <windows.h>
#include <conio.h>
#include <iostream>

int main() {
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos = {40, 12};
    enum Dir { None, Up, Down, Left, Right } dir = None;
    const SHORT maxX = 79, maxY = 24;

    CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(h, &cci); cci.bVisible = FALSE; SetConsoleCursorInfo(h, &cci);
    SetConsoleCursorPosition(h, pos); std::cout << '*';

    for (;;) {
        if (_kbhit()) {
            int c = _getch();           // prefix: often 0 or 224 for special keys
            if (c == 0 || c == 224) {
                int k = _getch();      // actual code: 72/80/75/77 for arrows
                switch (k) { case 72: dir = Up; break; case 80: dir = Down; break;
                              case 75: dir = Left; break; case 77: dir = Right; break; }
            }
        }

        Sleep(80); // game tick
        SetConsoleCursorPosition(h, pos); std::cout << ' '; // erase old
        if (dir == Up && pos.Y > 0) pos.Y--;
        else if (dir == Down && pos.Y < maxY) pos.Y++;
        else if (dir == Left && pos.X > 0) pos.X--;
        else if (dir == Right && pos.X < maxX) pos.X++;
        SetConsoleCursorPosition(h, pos); std::cout << '*';
    }
    return 0;
}

Notes and further tips: prefer int for key codes (extended values can exceed char), avoid hard-coding magic numbers (query console size with GetConsoleScreenBufferInfo), and on non-Windows systems use ncurses or termios. For a full snake game add a deque/vector of segment coordinates, a direction-queue to prevent instant reversals, collision checks, and frame-time based movement rather than Sleep for smoother behavior.

Recommended Answers

All 2 Replies

getch() returns two bytes, not just one, when you press special keys such as arrows and function keys. I like to make the keys negative values so that the program can easily distinguish between special keys and normal keys, but there are other ways such as add 255 to the value return by getch().

int main(int argc, char* argv[])
{
    int c = getch();
    if( c == 0 || c == 224)
        c = -getch();
    
    printf("%d\n", c);
	return 0;
}

Thanks, I didn't think that the special keys would conflict with the normal key so thanks for the heads up :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.