for disch - Snake beta - graphical

closed account (zwA4jE8b)
Here is the graphic version. beta obviously
just remove line 6 and 32 if you want to compile and test


snakemain.cpp
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
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Michael Ervin - Windows based graphical snake game - 7/29/2011
//////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <Windows.h>
#include "resource.h"
#include "snakengine.h"

HWND hWnd;
HDC hDC;

//////////////////
game _game;
//////////////////

LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	WNDCLASSEXA wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = MessageProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)); 
	wc.lpszClassName = "SNAKE";

	RegisterClassExA(&wc);

	hWnd = CreateWindowExA(NULL,
                          "SNAKE", "Snake",
                          WS_SYSMENU | WS_BORDER,
                          0, 45,
                          screen_width, screen_height,
                          NULL, NULL,
                          hInstance,
                          NULL);

	ShowWindow(hWnd, nCmdShow);
	hDC = GetDC(hWnd);

	MSG msg;

	_game.game_init(hDC, hWnd);

	while(TRUE)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			_game.game_play();
			Sleep(75);
		}
	}
	return msg.wParam;
}

LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
		case WM_KEYDOWN:
		{
			switch (wParam)
			{
			case _up:
			case _down:
			case _left:
			case _right:
				_game.game_input(wParam);
				break;
			case _pause:
				_game.game_pause();
				break;
			}
			return 0;
		} break;
		case 2345:
			_game.game_over();
			break;
		case 1234:
			_game.game_init(hDC, hWnd);
			break;
     }
     return DefWindowProcA(hWnd, message, wParam, lParam);
 }
closed account (zwA4jE8b)
snakengine.h

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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//Snake object header
#ifndef H_snakengine
#define H_snakengine

#define screen_width 800
#define screen_height 600

#include <vector>
#include <ctime>

#include <sstream>
#include <string>
std::string convstr(const int& t){std::stringstream itoa; itoa << t; return itoa.str();}

COLORREF _blue = RGB(0,0,255);
COLORREF _red = RGB(255,0,0);
COLORREF _white = RGB(255,255,255);
COLORREF _black = RGB(0,0,0);
COLORREF _green = RGB(0,255,0);
const int _rectwidth = 10, _rectheight = 10, _bordertop = 5, _borderleft = 5,
		  _borderbottom = screen_height - 65, _borderright = screen_width - 9;
const DWORD _up = 0x57, _down = 0x53, _left = 0x41, _right = 0x44, _pause = 0x50;
HBRUSH _bluebrush = CreateSolidBrush(_blue);
HBRUSH _whitebrush = CreateSolidBrush(_white);
HBRUSH _blackbrush = CreateSolidBrush(_black);
HBRUSH _greenbrush = CreateSolidBrush(_green);

std::vector<RECT> _foodgrid, _obstaclegrid;

class level
{
public:
	int _numfood, _numobstacle;

	level(HDC _hDC, int _lvl, int _scr):hDC(_hDC), _level(_lvl), _score(_scr){}

	void init_level()
	{
		draw_level();
		fill_foodobstaclegrid();
	}
	void update_info(int& _score)
	{
		std::string _slevel = convstr(_level);
		std::string _sscore = convstr(_score);
		TextOut(hDC, 60, screen_height - 55, _slevel.c_str(), _slevel.length());
		TextOut(hDC, 145, screen_height - 55, convstr(_score).c_str(), _sscore.length());
	}
private:
	int _level, _score;
	const HDC hDC;
	void draw_level()
	{
		RECT _back = {0,0,screen_width - 6,screen_height - 60};
		RECT _front = {10,10,screen_width - 10,screen_height - 70};
		FillRect(hDC, &_back, _blackbrush);
		FillRect(hDC, &_front, _whitebrush);
	}
	void fill_foodobstaclegrid()
	{
		_numfood = _level * 4;
		_numobstacle = _level * 3;
		int _top, _left, i;
		srand(time(0));

		for(i = 0; i < _numfood; i++)
		{
			_top = 10 + (rand() % (_borderbottom - 20));
			if(_top % 10 != 0)
				_top -= _top % 10;
			_left = 10 + (rand() % (_borderright - 20));
			if(_left % 10 != 0)
				_left -= _left % 10;
			RECT _food = {_left, _top, _left + 10, _top + 10};
			if(_foodgrid.empty())
				_foodgrid.push_back(_food);
			else
				for(int j = 0; j < _foodgrid.size(); j++)
					if(_top == _foodgrid[j].top && _left == _foodgrid[j].left)
					{
						i--;
						break;
					}
					else
					{
						_foodgrid.push_back(_food);
						break;
					}
			FillRect(hDC, &_food, _greenbrush);
		}

		for(i = 0; i < _numobstacle; i++)
		{
			_top = 10 + (rand() % (_borderbottom - 20));
			if(_top % 10 != 0)
				_top -= _top % 10;
			_left = 10 + (rand() % (_borderright - 21));
			if(_left % 10 != 0)
				_left -= _left % 10;
			RECT _obstacle = {_left, _top, _left + 10, _top + 10};
			if(_obstaclegrid.empty())
				_obstaclegrid.push_back(_obstacle);
			else
				for(int j = 0; j < _obstaclegrid.size(); j++)
					if(_top == _obstaclegrid[j].top && _left == _obstaclegrid[j].left)
					{
						i--;
						break;
					}
					else
					{
						_obstaclegrid.push_back(_obstacle);
						break;
					}
			FillRect(hDC, &_obstacle, _blackbrush);
		}
		TextOut(hDC, 15, screen_height - 55, "Level: ", 7);
		TextOut(hDC, 100, screen_height - 55, "Score: ", 7);
	}
	void end_game()
	{

	}
};

class snake
{
	int _lvl, _length, _counter;
	RECT _old;
	std::vector<RECT> _segments;
	
public:
	char _direction;
	snake(int _level):_lvl(_level), _length(_lvl*4), _direction(NULL), _counter(0){}

	void init_snake(int _ystart)
	{
		_segments.resize(0);
		for(int i = 0; i < _length; i++)
		{
			RECT _newseg = {390, _ystart, 390 + _rectwidth, _ystart + _rectheight};
			_segments.push_back(_newseg);
		}
	};
	void add_seg(int _x, int _y)
	{
		RECT _newseg = {_x, _y, _rectwidth, _rectheight};
		_segments.push_back(_newseg);
	}
	void show_head(const HDC& hDC)
	{
			FillRect(hDC, &_segments[0], _bluebrush); 
	}
	void next_pos()
	{
		int i;
		_old = _segments.back();

		for(i = _length - 1; i > 0; i--)
			_segments[i] = _segments[i - 1];
		
		switch (_direction)
		{
		case _right:
			_segments[0].left += _rectwidth;
			_segments[0].right += _rectwidth;
			break;
		case _left:
			_segments[0].left -= _rectwidth;
			_segments[0].right -= _rectwidth;
			break;
		case _up:
			_segments[0].top -= _rectheight;
			_segments[0].bottom -= _rectheight;
			break;
		case _down:
			_segments[0].top += _rectheight;
			_segments[0].bottom += _rectheight;
			break;
		}
	}
	void animate_snake(const HDC& hDC)
	{
		FillRect(hDC, &_segments[0], _bluebrush);

		if (_counter == (4 * _lvl) - 1)
			FillRect(hDC, &_old, _whitebrush);
		else
			_counter++;
	}
	void add_seg()
	{
		RECT _newseg = _segments[_length - 1];
		_segments.push_back(_newseg);
		_length++;
	}
	char collisions()
	{
		int i;
		char _ret = 'n';

		switch (_direction)
		{
		case _up:
			if(_segments[0].top <= _bordertop)
			_ret = 'o';
			break;
		case _down:
			if(_segments[0].bottom >= _borderbottom)
			_ret = 'o';
			break;
		case _left:
			if(_segments[0].left <= _borderleft)
			_ret = 'o';
			break;
		case _right:
			if(_segments[0].right >= _borderright)
			_ret = 'o';
			break;
		}

		for(i = 0; i < _foodgrid.size(); i ++)
			if(_segments[0].top == _foodgrid[i].top && _segments[0].left ==_foodgrid[i].left)
			{
				_ret = 'f';
				break;
			}

		for(i = 0; i < _obstaclegrid.size(); i ++)
			if(_segments[0].top == _obstaclegrid[i].top && _segments[0].left ==_obstaclegrid[i].left)
			{
				_ret = 'o';
				break;
			}

		for(i = 1; i < _length; i++)
			if(_segments[0].top == _segments[i].top && _segments[0].left ==_segments[i].left)
			{
				_ret = 'o';
				break;
			}

		return _ret;
	}
};

class game
{
	level* _LVL;
	snake* _SNK;
	HDC hDC;
	int _level, _score, _foodeaten;
	char _collision;
	HWND _hWnd;

public:
	game():_level(1), _score(0){}
	
	void game_init(HDC _hDC, HWND hWnd)
	{
		_hWnd = hWnd;
		hDC = _hDC;
		
		_foodeaten = 0;

		_LVL = new level(hDC, _level, _score);
		_LVL->init_level();
		_LVL->update_info(_score);

		_SNK = new snake(_level);
		_SNK->init_snake(300);
		_SNK->show_head(hDC);
	}
	void game_input(WPARAM _direction)
	{
		switch (_direction)
		{
		case _up:
			_SNK->_direction = _up;
			break;
		case _down:
			_SNK->_direction = _down;
			break;
		case _left:
			_SNK->_direction = _left;
			break;
		case _right:
			_SNK->_direction = _right;
			break;
		}
	}
	void game_play()
	{
		if(_SNK->_direction != NULL)
		{
				_SNK->next_pos();
				_SNK->animate_snake(hDC);
				switch (_SNK->collisions())
				{
				case 'f':
					_foodeaten++;
					_SNK->add_seg();
					_score += _level * 5;
					break;
				case 'o':
					PostMessage(_hWnd, 2345, 0, 0);
					break;
				case 'n':
					break;
				}
				_LVL->update_info(_score);
				if(_foodeaten == _LVL->_numfood)
				{
					_level++;
					_foodgrid.resize(0);
					_obstaclegrid.resize(0);
					delete _SNK;
					delete _LVL;
					PostMessage(_hWnd, 1234, 0, 0);
				}
		}
	}
	void game_pause()
	{
		
	}
	void game_over()
	{
		PostQuitMessage(0);
	}
};
#endif 
Topic archived. No new replies allowed.