#include<iostream>
#include<vector>
#include<random>
#include<ctime>
#include <Windows.h>
#include"conio.h"
const int height = 20;
const int width = 20;
using namespace std;
class Snake {
private:
int len;
int x ,y, foodx, foody,score;
enum eDirection{STOP=0,UP,RIGHT,DOWN,LEFT};
eDirection dir;
int nTail;
int tail_x[100], tail_y[100];
HANDLE hOutput, hOutBuf;
COORD coord = { 0,0 };
DWORD bytes = 0;
char data[height + 2][width + 2];
bool switchOver;
public:
bool gameOver;
Snake() {
dir = STOP;
nTail = 0;
score = 0;
gameOver = false;
switchOver = false;
creatFood();
x = 9,y = 9;
double_buff_init();
}
void double_buff_init() {
hOutBuf = CreateConsoleScreenBuffer(
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
hOutput = CreateConsoleScreenBuffer(
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
CONSOLE_CURSOR_INFO cci;
cci.bVisible = 0;
cci.dwSize = 1;
SetConsoleCursorInfo(hOutput, &cci);
SetConsoleCursorInfo(hOutBuf, &cci);
}
void creatFood() {
std::default_random_engine e(time(0));
std::uniform_int_distribution<int> distx(1, height-1);
std::uniform_int_distribution<int> disty(1, width-1);
while (1) {
foodx = distx(e);
foody = disty(e);
if (foodx == x && foody == y)
continue;
break;
}
}
void Input() {
if (kbhit()) {
switch (getch()) {
case 'w':
dir = UP;
break;
case 'd':
dir = RIGHT;
break;
case 's':
dir = DOWN;
break;
case 'a':
dir = LEFT;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Move() {
int tempx,tempy;
int prex = tail_x[0];
int prey = tail_y[0];
tail_x[0] = x;
tail_y[0] = y;
for (int i = 1; i < nTail;i++) {
tempx = tail_x[i];
tempy = tail_y[i];
tail_x[i] = prex;
tail_y[i] = prey;
prex = tempx;
prey = tempy;
}
switch (dir)
{
case UP:
y--;
break;
case RIGHT:
x++;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
default:
break;
}
for (int i = 0; i < nTail; i++) {
if (tail_x[i] == x && tail_y[i] == y)
gameOver = true;
}
if (x == foodx && y == foody) {
nTail++;
score += 10;
creatFood();
}
/*if (x<0 || x>width || y<0 || y>height)
gameOver = true;*/
if (x >= height) x = 1; else if (x < 0) x = height - 1;
if (y >= width) y = 1; else if (y < 0) y = width - 1;
}
void draw() {
for (int i = 0; i < width ; i++)
data[0][i] = '#';
cout << '\n';
char a = ' ';
for (int i = 1; i < height - -1; i++) {
for (int j = 0; j < width; j++) {
if (j == 0 || j == width - 1)
a = '#';
else {
if (x == j && y == i)
a = 'O';
else if (foodx == j && foody == i) {
a = '*';
}
else{
bool print = false;
for (int k = 0; k < nTail; k++) {
if (tail_x[k] == j && tail_y[k] == i)
{
a = 'o';
print = true;
}
}
if (!print)
a = ' ';
}
}
data[i][j] = a;
}
cout << '\n';
}
for (int i = 0; i < width; i++)
data[height - 1][i] = '#';
cout << '\n';
if (!switchOver) {
for (int i = 0; i < height; i++) {
coord.Y = i;
WriteConsoleOutputCharacterA(hOutBuf, data[i], width, coord, &bytes);
}
SetConsoleActiveScreenBuffer(hOutBuf);
}
else {
for (int i = 0; i < height; i++) {
coord.Y = i;
WriteConsoleOutputCharacterA(hOutput, data[i], width, coord, &bytes);
}
SetConsoleActiveScreenBuffer(hOutput);
}
switchOver = !switchOver;
Sleep(200);
}
};
int main() {
Snake snake = Snake();
while (!snake.gameOver) {
snake.draw();
snake.Input();
snake.Move();
}
return 0;
}