My first Game - Snake

closed account (zwA4jE8b)
This is my first attempt at a game.

It is a classic, snake.

If anyone has any pointers or constructive criticism that would be much appreciated. If so, please keep it relatively simple. I do not want to rewrite the entire game, yet.

Some things I need to add...
-Splash screen w/ control info.
-Remove Sleep() and replace with maybe iterative loop?

When game starts press any key to begin.

Keys:
w s a d - Movement
x - Quit

Objects:
@ - Food
X - Obstacle

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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//Michael Ervin - Snake game
//Inspiration and some structural design from http://www.simonhuggins.com/courses/cbasics/course_notes/snake.htm

#include <conio.h>
#include <vector>
#include <Windows.h>
#include <iostream>
#include <ctime>
#include <fstream>

struct _coordinates
{
	int _x;
	int _y;
};

HANDLE _handle;

int _level, _score, _highscore, _numfood, _numobstacle, _speed;
const int _rows = 30, _columns = 80;
char _grid[_rows][_columns];
const char _right = 'd', _left = 'a', _up = 'w', _down = 's';
SMALL_RECT _size = {0, 0, 79, 32};
COORD _pos = {0,0};
_CONSOLE_CURSOR_INFO _cinfo = {100, false};

//Snake object
struct snake
{
	int _length, _foodeaten;
	bool _deadsnake;
	char _direction;
	std::vector<_coordinates*> snake_pos;

	snake(int _level):_length(4*_level), _direction(_right), _foodeaten(0), _deadsnake(false) {init_snake();}
	void init_snake() 
	{
		for(int i = 0; i < _length; i++)
		{
			_coordinates* _new = new _coordinates;
			_new->_x = 40 - i;
			_new->_y = 16;
			snake_pos.push_back(_new);
		}
	};
	void next_pos()
	{
		int i;
		_old._x = snake_pos[_length - 1]->_x;
		_old._y = snake_pos[_length - 1]->_y;
		
		switch (_direction)
		{
		case _right:
			for(i = _length - 1; i >= 1; i--)
				*snake_pos[i] = *snake_pos[i - 1];
			snake_pos[0]->_x += 1;
			break;
		case _left:
			for(i = _length - 1; i > 0; i--)
				*snake_pos[i] = *snake_pos[i - 1];
			snake_pos[0]->_x -= 1;
			break;
		case _up:
			for(i = _length - 1; i > 0; i--)
				*snake_pos[i] = *snake_pos[i - 1];
			snake_pos[0]->_y -= 1;
			break;
		case _down:
			for(i = _length - 1; i > 0; i--)
				*snake_pos[i] = *snake_pos[i - 1];
			snake_pos[0]->_y += 1;
			break;
		}
	}
	void draw_snake()
	{
		int i;
		
		if (!_deadsnake)
			SetConsoleTextAttribute(_handle, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
		else
			SetConsoleTextAttribute(_handle, FOREGROUND_RED);

		for(i = 0; i < _length; i++)
		{
			_pos.X = snake_pos[i]->_x; _pos.Y = snake_pos[i]->_y;
			SetConsoleCursorPosition(_handle, _pos);
			_putch('O');
		}
		_pos.X = _old._x; _pos.Y = _old._y;
		SetConsoleCursorPosition(_handle, _pos);
		_putch(' ');
	}
	void add_seg()
	{
		_coordinates* _new = new _coordinates;
		*_new = *snake_pos.back();
		snake_pos.push_back(_new);
		_length++;
	}
private:
	_coordinates _old;
};

void init_lvl(snake&);
void update_info();
char collision(snake&);
bool self_collision(snake&);
void dead_snake(snake&);
void end_game();
void read_score();
void write_score();

int main()
{
	_handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleWindowInfo(_handle, true, &_size);
	SetConsoleCursorInfo(_handle, &_cinfo);
	srand(time(0));

	char _input = '\0';

	_level = 1;
	_score = 0;
	_speed = 250;
	read_score();
	snake _snake1(_level);
	init_lvl(_snake1);

	//Main game loop
	while (_input != 'x')
	{
		if (_kbhit())
		{
			_input = _getch();
			if (_input == _up || _input == _down || _input == _right || _input == _left)
				_snake1._direction = _input;
		}
		
		_snake1.next_pos();
		_input = collision(_snake1);
		_snake1.draw_snake();
		if (_snake1._foodeaten == _numfood)
		{
			_level++;
			_snake1._foodeaten = 0;
			init_lvl(_snake1);
		}
		Sleep(_speed);
	}
	end_game();
	return 0;
}

void init_lvl(snake& _sn0)
{
	int i, j;
	_coordinates _stuff;
	_numfood = _level * 5;
	_numobstacle = _level * 4;
	_speed-= 25;

	SetConsoleTextAttribute(_handle, FOREGROUND_RED | FOREGROUND_INTENSITY);

	//Fill grid with blanks
	for(i = 0; i < _rows; i++)
		for(j = 0; j < _columns; j++)
			_grid[i][j] = ' ';
	
	//Fill grid with borders
	for(i = 1; i < _columns - 1; i++)
	{
		_grid[0][i] = '=';
		_grid[_rows-1][i] = '=';
	}
	for(i = 0; i < _rows; i++)
	{
		_grid[i][0] = '|';
		_grid[i][_columns - 1] = '|';
	}

	//Fill grid with food and obstacles
	for(i = 0; i < _numfood; i++)
	{
		_stuff._y = (rand()%(_rows - 2)) + 1;
		_stuff._x = (rand()%(_columns - 2)) + 1;
		_grid[_stuff._y][_stuff._x] = '@';
	}

	for(i = 0; i < _numfood; i++)
	{
		_stuff._y = (rand()%(_rows - 2)) + 1;
		_stuff._x = (rand()%(_columns - 2)) + 1;
		if (_grid[_stuff._y][_stuff._x] != ' ')
			i--;
		else
			_grid[_stuff._y][_stuff._x] = 'X';
	}

	//Draw grid
	for(i = 0; i < _rows; i++)
		for(j = 0; j < _columns; j++)
		{
			_pos.X = j; _pos.Y = i;
			SetConsoleCursorPosition(_handle, _pos);
			_putch(_grid[i][j]);
		}

	_pos.X = 3; _pos.Y = 31;
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("Level: ");

	_pos.X = 20; _pos.Y = 31;
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("Score: ");

	_pos.X = 40; _pos.Y = 31;
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("High Score: ");

	_sn0.draw_snake();
	update_info();

	while(!_kbhit()){}
}

void update_info()
{
	SetConsoleTextAttribute(_handle, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	//Show level
	_pos.X = 10; _pos.Y = 31;
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("%d", _level);

	//Show score
	_pos.X = 27; _pos.Y = 31;
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("%d", _score);

	//Show highscore
	_pos.X = 52; _pos.Y = 31;
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("%d", _highscore);
}

char collision(snake& _sn1)
{
	_coordinates _check = *_sn1.snake_pos[0];
	
	//Check for border collisions
	//Check self collisions
	//Check food collisions
	//Check obstacle collisions
	if (_grid[_check._y][_check._x] == '=' || _grid[_check._y][_check._x] == '|' || _grid[_check._y][_check._x] == 'X' || self_collision(_sn1))
	{
		_sn1._deadsnake = true;
		update_info();
		return 'x';
	}
	else if (_grid[_check._y][_check._x] == '@')
	{
		_sn1.add_seg();
		_sn1._foodeaten++;
		_score += 5;
		update_info();
	}
}

bool self_collision(snake& _sn2)
{
	bool _temp = false;
	for(int i = 1; i < _sn2._length - 1; i++)
		if (_sn2.snake_pos[0]->_x == _sn2.snake_pos[i]->_x && _sn2.snake_pos[0]->_y == _sn2.snake_pos[i]->_y)
			_temp = true;
	return _temp;
}

//End of game procedure - check if highscore
void end_game()
{
	if (_score > _highscore)
		write_score();
	_pos.X = (_columns / 2) - 4; _pos.Y = (_rows / 2);
	SetConsoleTextAttribute(_handle, FOREGROUND_GREEN);
	SetConsoleCursorPosition(_handle, _pos);
	_cprintf("GAME OVER");
	while(!_kbhit()){}
}

//Write highscore to file
void write_score()
{
	std::ofstream _outf("highscore.sco");
	_outf << _score;
	_outf.close();
}

//Read in highscore
void read_score()
{
	std::ifstream _inf("highscore.sco");
	if (_inf.is_open())
		_inf >> _highscore;
	else
		_highscore = 0;
	_inf.close();
}
Cool. Way better than my first snake game.
Last edited on
very nice
console? =(
closed account (zwA4jE8b)
thanks creekist.

come on disch. I said it was my first game.
Besides now that I understand the structure and flow, converting to a graphical game will be much easier.
come on disch. I said it was my first game.


I didn't say it was bad. I'm just sad it's console is all.
Topic archived. No new replies allowed.