Flickering in Snake Game

I have created a snake game that works but I believe since I used the system("cls"); function it has to reload the game every second causing an extreme glitch. Every time the screen reloads, it flickers. How do I fix this? The part of the code I think is causing this issue is below.

void Draw()
{
system("cls"); //system("clear");
for (int i = 0; i < width +2; i++)
cout << "-";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "|";
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "o";
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "|";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "-";
cout << endl;
cout << "Score:" << score << endl;
cout << endl;
cout << "use arrow keys to control the snake";
}
You should use code tags to post code so that it's readable.

Aren't you writing spaces to all the blank parts of the screen anyway? Do you even need the system("cls")?

If you still get flickering, you can probably redraw the screen far more efficiently by only erasing what needs to be erased (e.g, the end of the tail) and writing only what needs to be drawn (e.g., the new head).

BTW, if you ignore people's responses, they will stop helping you.
To get rid of the flicker you need to use a non-standard library with support for double buffering such as curses/ncurses or the WinAPI.
or the (also nonstandard) gotoxy or similar function that let you update only the text that changed, and leave the rest in place, which is more efficient (less writing) and will clear up the flicker.
I think the gotoxy function would work well with this code, but I can't figure out how to fit it into my code. Would I add it in front of parts of the code that change when the screen updates (like the tail and food placement?)
I got rid of the glitching by deleting the system("cls") and adding a cin.get() but now the screen looks great, but the snake wont move at all. Any ideas? Thank you!
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <conio.h>
#include <windows.h>

#define KEY_UP 72

#define KEY_DOWN 80

#define KEY_LEFT 75

#define KEY_RIGHT 77

using namespace std;
bool gameOver;
const int width = 50;
const int height = 20;
int x;
int y;
int fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirecton dir;
void gotoxy(int x, int y) {
	

}
void Setup()
{
	gameOver = false;
	dir = STOP;
	gotoxy(width / 2, height / 2);
	fruitX = rand() % width;
	fruitY = rand() % height;
	score = 0;
}
void Draw()
{
	for (int i = 0; i < width +2; i++)
		cout << "-";
	cout << endl;
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if (j == 0)
				cout << "|";
			if (i == y && j == x)
				cout << "O";
			else if (i == fruitY && j == fruitX)
				cout << "o";
			else
			{
				bool print = false;
				for (int k = 0; k < nTail; k++)
				{
					if (tailX[k] == j && tailY[k] == i)
					{
						cout << "o";
						print = true;
					}
				}
				if (!print)
					cout << " ";
			}
			if (j == width - 1)
				cout << "|";
		}
		cout << endl;
	}
	for (int i = 0; i < width + 2; i++)
		cout << "-";
	cout << endl;
	cout << "Score:" << score << endl;
	cout << endl;
	cout << "Use arrow keys to control the snake";
}
void Input()
{
	if (_kbhit())
	{
		switch (_getch())
		{
		case KEY_LEFT:
			dir = LEFT;
			break;
		case KEY_RIGHT:
			dir = RIGHT;
			break;
		case KEY_UP:
			dir = UP;
			break;
		case KEY_DOWN:
			dir = DOWN;
			break;
		case 'x':
			gameOver = true;
			break;
		}
	}
}
void Logic()
{
	gotoxy(tailX[0], tailY[0]);
	int prevX = tailX[0];
	int prevY = tailY[0];
	int prev2X, prev2Y;
	tailX[0] = x;
	tailY[0] = y;
	for (int i = 1; i < nTail; i++)
	{
		prev2X = tailX[i];
		prev2Y = tailY[i];
		tailX[i] = prevX;
		tailY[i] = prevY;
		prevX = prev2X;
		prevY = prev2Y;
	}
	switch (dir)
	{
	case LEFT:
		x--;
		break;
	case RIGHT:
		x++;
		break;
	case UP:
		y--;
		break;
	case DOWN:
		y++;
		break;
	default:
		break;
	}
	//if (x > width || x < 0 || y > height || y < 0) 
	// gameOver = true; 
	if (x >= width) x = 0; else if (x < 0) x = width - 1;
	if (y >= height) y = 0; else if (y < 0) y = height - 1;
	for (int i = 0; i < nTail; i++)
		if (tailX[i] == x && tailY[i] == y)
			gameOver = true;
	if (x == fruitX && y == fruitY)
	{
		score += 10;
		fruitX = rand() % width;
		fruitY = rand() % height;
		nTail++;
	}
}
int main()
{
	Setup();
	while (!gameOver)
	{
		Draw();
		Input();
		Logic();
		Sleep(70); //speed of snake
		cin.get();
	}
	return 0;
}
Your gotoxy function is empty. And you don't explicitly initialize your global x and y variables, which means they will automatically be initialized to 0 (but maybe that's what you want).
What kind of function would go in gotoxy? Thank you
I don't use windows at the moment, but I thought that conio had a gotoxy (or maybe _gotoxy) function.
> and adding a cin.get()
Why did you add this?

On the face of it, you have to press enter every time you want to move, since cin is going to block the program until you do.
For Visual Studio

1
2
3
4
5
6
7
8
9
#include <windows.h> 

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Topic archived. No new replies allowed.