question

hey guys...
you know console game right ?
well...how do I make the enemies move whithout me pressing any key( like snake, in the snake game, the snake move on its own and turn direction when you pressed a key)
Whoops, read question wrong
Last edited on
@Flaze07

Here's the beginning of the game I decided to write, based on your idea. The snake, so far just a set of eyes, will move continuously on screen in one direction, until you press a different arrow key. I don't yet have it to end a life if your snake hits the edges. For now, it just stops. Hope this small starting program, helps you out
( Oh, score and lives don't change either )

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Snake.cpp : main project file.

#include <iostream>
#include <conio.h>
#include <ctime>
#include <string>
#include <Windows.h>

using namespace System;

using std::cout;
using std::string;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

enum {
	black,          //  0
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

#define ESC 27
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

#define on , // So I can use the function - void text(text_color on background_color)
// To more easily remember which is text color vs background color

void Set_Screen(int score, int lives);
void Move_Snake();
void setcursor(bool visible, DWORD size);
void gotoXY(int x, int y);
void gotoXY(int x, int y, int v);
void gotoXY(int x, int y, string text);
void Box(int style, int across, int down, int amount, int rows, int b_color, int f_color, int shadow, int shadow_bc, int shadow_fc);
void BoxLineAcross(int style, int across, int down, int amount);
void WaitKey();
void ClearScreen();
void pause(unsigned int milliseconds);

void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

int width = 80;
int height = 40;

int main()
{
	setcursor(0,0); // Hide cursor
	SetConsoleTitle(TEXT("Snake Game - by whitenite1"));
	Console::SetWindowSize(width, height);
	int score=0,lives=3;
	Set_Screen(score, lives);
	Move_Snake();
       setcursor(1,10); // Restore cursor

}

void Set_Screen(int score, int lives)
{
	Box(1,0,0,width,height,black,light_gray,0,0,0);
	BoxLineAcross(1,0,height-3,width);
	gotoXY(8,height-2,"Score : ");
	cout << score;
	gotoXY(width-15,height-2,"Lives : ");
	cout << lives;
}

void Move_Snake()
{
	text(black on light_gray);
	string Snake[100];
	Snake[0]=":";
	Snake[1]= "\xB1";
	int ch,x=1,y=1;
	gotoXY(x, y, Snake[0]);
	do
	{
		ch = _getch();
		switch (ch)
		{
		case LEFT:
			if(x-1>0)
			{
				do
				{
					pause(100);
					text(light_gray on light_gray);
					gotoXY(x, y, " ");
					x--;
					text(black on light_gray);
					gotoXY(x, y, Snake[0]);
				}while(x-1>0 && !_kbhit());
			}
			break;
		case RIGHT:
			if(x+1< width-1)
			{
				do
				{
					pause(100);
					text(light_gray on light_gray);
					gotoXY(x, y, " ");
					x++;
					text(black on light_gray);
					gotoXY(x, y, Snake[0]);

				}while(x+1 < width-1 && !_kbhit());
			}
			break;
		case UP:
			if(y-1 > 0)
			{
				do
				{
					pause(100);
					text(light_gray on light_gray);
					gotoXY(x, y, " ");
					y--;
					text(black on light_gray);
					gotoXY(x, y, Snake[0]);

				}while(y-1>0  && !_kbhit());
			}
			break;
		case DOWN:
			if(y + 1 < height-3)
			{
				do
				{
					pause(100);
					text(light_gray on light_gray);
					gotoXY(x, y, " ");
					y++;
					text(black on light_gray);
					gotoXY(x, y, Snake[0]);
				}while(y+1 < height-3 && !_kbhit());
			}
			break;
		}
	}while(true);
}

void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
{
	if (size == 0)
	{
		size = 20;	// default cursor size
	}
	CONSOLE_CURSOR_INFO lpCursor;
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console, &lpCursor);
}


void gotoXY(int x, int y)
{
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
}

void gotoXY(int x, int y, int v)
{
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
	cout << v;
}

void gotoXY(int x, int y, string text)
{

	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
	cout << text;
}

void Box(int style, int across, int down, int amount, int rows, int f_color, int b_color, int shadow, int shadow_fc, int shadow_bc)
{
	char Shadow[4] = { ' ', '\xB0', '\xB1', '\xB2' };
	char  Style[5][11] = {
		{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
		{ ' ', '\xDA', '\xC4', '\xBF', '\xB3', '\xC0', '\xD9', '\xC3', '\xB4', '\xC2', '\xC1' },
		{ ' ', '\xC9', '\xCD', '\xBB', '\xBA', '\xC8', '\xBC', '\xCC', '\xB9', '\xCB', '\xCA' },
		{ ' ', '\xD5', '\xCD', '\xB8', '\xB3', '\xD4', '\xBE', '\xC6', '\xB5', '\xD1', '\xCF' },
		{ ' ', '\xD6', '\xC4', '\xB7', '\xBA', '\xD3', '\xBD', '\xC7', '\xB6', '\xD2', '\xD0' }
	};

	int x;
	string BoxLine(amount - 2, Style[style][2]);
	string BoxBody(amount - 2, ' ');
	string ShadowLine(amount, Shadow[shadow]);

	gotoXY(across, down);
	text(f_color on b_color);
	cout << Style[style][1] << BoxLine << Style[style][3];
	for (x = 1; x < rows; x++)
	{
		gotoXY(across, down + x);
		cout << Style[style][4] << BoxBody << Style[style][4];
		if (shadow)
		{
			text(shadow_fc on shadow_bc);
			cout << Shadow[shadow];
			text(f_color on b_color);
		}
	}
	gotoXY(across, down + rows - 1);
	cout << Style[style][5] << BoxLine << Style[style][6];
	if (shadow)
	{
		text(shadow_fc on shadow_bc);
		cout << Shadow[shadow];
		gotoXY(across + 1, down + rows);
		cout << ShadowLine;
		text(f_color on b_color);
	}
}

void BoxLineAcross(int style, int across, int down, int amount)
{
	char  Style[5][11] = {
		{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
		{ ' ', '\xDA', '\xC4', '\xBF', '\xB3', '\xC0', '\xD9', '\xC3', '\xB4', '\xC2', '\xC1' },
		{ ' ', '\xC9', '\xCD', '\xBB', '\xBA', '\xC8', '\xBC', '\xCC', '\xB9', '\xCB', '\xCA' },
		{ ' ', '\xD5', '\xCD', '\xB8', '\xB3', '\xD4', '\xBE', '\xC6', '\xB5', '\xD1', '\xCF' },
		{ ' ', '\xD6', '\xC4', '\xB7', '\xBA', '\xD3', '\xBD', '\xC7', '\xB6', '\xD2', '\xD0' }
	};
	gotoXY(across, down);
	cout << Style[style][7];
	for (int x = 1; x < amount - 1; x++)
		cout << Style[style][2];
	cout << Style[style][8];
}

void WaitKey()
{
	gotoXY(50, 50, "Press any key to close this program");
	while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}

void pause(unsigned int milliseconds)
{
	time_t final = milliseconds + clock();
	while (milliseconds < final)
	{
		milliseconds = clock();
	}
}

void ClearScreen()
{
	DWORD n;
	DWORD size;
	COORD coord = { 0 };
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(h, &csbi);
	size = csbi.dwSize.X * csbi.dwSize.Y;
	FillConsoleOutputCharacter(h, TEXT(' '), size, coord, &n);
	GetConsoleScreenBufferInfo(h, &csbi);
	FillConsoleOutputAttribute(h, csbi.wAttributes, size, coord, &n);
	SetConsoleCursorPosition(h, coord);
}
Last edited on
Topic archived. No new replies allowed.