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
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Michael Ervin - Snake game - Windows
//Inspiration and some structural design from http://www.simonhuggins.com/courses/cbasics/course_notes/snake.htm
//The main game loop is as follows:
// 1)get user input, if valid set direction or pause
// 2)get the snakes next position
// 3)check for collisions
// 4)animate the snake (no collision - yellow, dead snake - red)
// 5)check if level should be advanced
//Sleep(10) is used in the while(true) waiting loops to keep the processor usage at or near 0%
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <conio.h>
#include <vector>
#include <Windows.h>
#include <iostream>
#include <ctime>
#include <fstream>
//Define colors not defined in windows
#define FOREGROUND_YELLOW 0x0006
#define FOREGROUND_WHITE 0x0007
#define FOREGROUND_AQUA 0x0003
struct _coordinates
{
int _x;
int _y;
};
//Global variables
HANDLE _handle;
int _level, _score, _highscore, _numfood, _numobstacle, _speed;
const int _rows = 30, _columns = 80;
char _grid[_rows + 2][_columns], _leveltype;
const char _right = 'd', _left = 'a', _up = 'w', _down = 's';
SMALL_RECT _size = {0, 0, 79, 32};
COORD _pos = {0,0};
//Fixed level arrays
COORD _l1[30] = {25,14,26,14,27,14,28,14,29,14,30,14,31,14,32,14,33,14,34,14,35,14,36,14,37,14,38,14,39,14,40,14,
41,14,42,14,43,14,44,14,45,14,46,14,47,14,48,14,49,14,50,14,51,14,52,14,53,14,54,14};
COORD _l2[52] = {26,7,26,8,26,9,26,10,26,11,26,12,26,13,26,14,26,15,26,16,26,17,26,18,26,19,
27,19,28,19,29,19,30,19,31,19,32,19,33,19,34,19,35,19,36,19,37,19,38,19,39,19,40,19,
41,19,42,19,43,19,44,19,45,19,46,19,47,19,48,19,49,19,50,19,51,19,52,19,53,19,
53,18,53,17,53,16,53,15,53,14,53,13,53,12,53,11,53,10,53,9,53,8,53,7};
COORD _l3[30] = {20,6,20,7,20,8,20,9,20,10,20,11,20,12,20,13,20,14,20,15,
40,17,40,18,40,19,40,20,40,21,40,22,40,23,40,24,40,25,40,26,
60,6,60,7,60,8,60,9,60,10,60,11,60,12,60,13,60,14,60,15};
COORD _l4[117] = {17,7,18,7,19,7,20,7,21,7,22,7,23,7,24,7,25,7,26,7,27,7,28,7,29,7,30,7,31,7,32,7,33,7,34,7,35,7,36,7,37,7,38,7,39,7,40,7,41,7,
41,8,41,9,41,10,41,11,41,12,41,13,41,14,41,15,
42,15,43,15,44,15,45,15,46,15,47,15,48,15,49,15,50,15,51,15,52,15,53,15,54,15,55,15,56,15,57,15,58,15,59,15,60,15,61,15,62,15,63,15,64,15,65,15,66,15,
17,11,18,11,19,11,20,11,21,11,22,11,23,11,24,11,25,11,26,11,27,11,28,11,29,11,30,11,31,11,32,11,33,11,34,11,35,11,36,11,37,11,
37,12,37,12,37,13,37,14,37,15,37,16,37,17,37,18,37,19,
38,19,39,19,40,19,41,19,42,19,43,19,44,19,45,19,46,19,47,19,48,19,49,19,50,19,51,19,52,19,53,19,54,19,55,19,56,19,57,19,58,19,59,19,60,19,61,19,62,19,63,19,64,19,65,19,66,19};
COORD _l5[76] = {2,7,3,7,4,7,5,7,6,7,7,7,8,7,9,7,10,7,11,7,12,7,13,7,14,7,15,7,16,7,17,7,18,7,19,7,20,7,21,7,22,7,23,7,24,7,25,7,26,7,27,7,28,7,29,7,30,7,31,7,32,7,33,7,34,7,35,7,36,7,37,7,38,7,39,7,
40,7,41,7,42,7,43,7,44,7,45,7,46,7,47,7,48,7,49,7,50,7,51,7,52,7,53,7,54,7,55,7,56,7,57,7,58,7,59,7,60,7,61,7,62,7,63,7,64,7,65,7,66,7,67,7,68,7,69,7,70,7,71,7,72,7,73,7,74,7,75,7,76,7,77,7};
//Snake object
struct snake
{
int _length, _foodeaten;
char _direction;
bool _deadsnake;
std::vector<_coordinates> snake_pos;
snake(int _level):_length(4*_level), _counter(0), _direction(_right), _foodeaten(0), _deadsnake(false){}
void init_snake(int _ystart)
{
snake_pos.resize(0);
for(int i = 0; i < _length; i++)
{
_coordinates _new;
_new._x = 40;
_new._y = _ystart;
snake_pos.push_back(_new);
}
snake_pos[0]._x = 40;
};
void next_pos()
{
int i;
_old = snake_pos.back();
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 >= 1; i--)
snake_pos[i] = snake_pos[i - 1];
snake_pos[0]._x -= 1;
break;
case _up:
for(i = _length - 1; i >= 1; i--)
snake_pos[i] = snake_pos[i - 1];
snake_pos[0]._y -= 1;
break;
case _down:
for(i = _length - 1; i >= 1; i--)
snake_pos[i] = snake_pos[i - 1];
snake_pos[0]._y += 1;
break;
}
}
void snake_head(int _ystart)
{
SetConsoleTextAttribute(_handle, FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
_pos.X = 40; _pos.Y = _ystart;
SetConsoleCursorPosition(_handle, _pos);
_putch('O');
}
void animate_snake()
{
if (!_deadsnake)
{
SetConsoleTextAttribute(_handle, FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
if (_counter == ((4 * _level) - 1))
{
_pos.X = _old._x; _pos.Y = _old._y;
SetConsoleCursorPosition(_handle, _pos);
_putch(' ');
}
else
_counter++;
_pos.X = snake_pos[0]._x; _pos.Y = snake_pos[0]._y;
SetConsoleCursorPosition(_handle, _pos);
_putch('O');
}
else
{
_pos.X = _old._x; _pos.Y = _old._y;
SetConsoleCursorPosition(_handle, _pos);
_putch(' ');
SetConsoleTextAttribute(_handle, FOREGROUND_RED);
for(int i = 0; i < _length; i++)
{
_pos.X = snake_pos[i]._x; _pos.Y = snake_pos[i]._y;
SetConsoleCursorPosition(_handle, _pos);
_putch('O');
}
}
}
void add_seg()
{
_coordinates _new = snake_pos.back();
snake_pos.push_back(_new);
_length++;
}
protected:
int _counter;
private:
_coordinates _old;
};
void init_lvl(snake&, char&);
void init_lvl_rand(_coordinates&, int);
void init_lvl_fixed(_coordinates&, int&);
void intro();
void update_info();
void collision(snake&, char&);
bool self_collision(snake&);
void dead_snake(snake&);
void end_game(char&);
void read_score();
void write_score();
void clear_grid();
void pause(){char _temp;while(true){Sleep(10);if(_kbhit()){_temp = _getch();if(_temp == 'p')break;}}}
int main()
{
//Set up console size and attributes
_handle = GetStdHandle(STD_OUTPUT_HANDLE);
_CONSOLE_CURSOR_INFO _cinfo = {100, false};
SetConsoleWindowInfo(_handle, true, &_size);
SetConsoleTitleA("Snake");
SetConsoleCursorInfo(_handle, &_cinfo);
srand(time(0));
char _input = '\0';
//Initialization and play again loop
do
{
_level = 1;
_score = 0;
read_score();
snake _snake1(_level);
intro();
init_lvl(_snake1, _input);
//Main game loop
while (_input != 'x')
{
if (_kbhit())
{
_input = _getch();
if (_input == _up || _input == _down || _input == _right || _input == _left)
_snake1._direction = _input;
else if (_input == 'p')
pause();
}
_snake1.next_pos();
collision(_snake1, _input);
_snake1.animate_snake();
if (_snake1._foodeaten == _numfood)
{
_level++;
_snake1._foodeaten = 0;
init_lvl(_snake1, _input);
}
Sleep(_speed);
}
end_game(_input);
clear_grid();
}
while(_input != 'n');
return 0;
}
|