#include<iostream>
#include<Windows.h>
#include<ctime>
#include<vector>
#include<cstdio>
#include<conio.h>
#include<random>
#include<cstdlib>
#define random(x) 2+rand()%(x)
using namespace std;
enum direction { up = 72, down = 80, leftw = 75, rightw = 77 };
struct node
{
int x = 0, y = 0;
direction way = up;
};
vector<node> snake;
void printblock(int, int);
bool die();
void gotoxy(int, int);
void hide();
void update();
void initialization();
bool getway(void);
void snakemove();
struct food
{
int x, y;
void delivery();
private:
bool ok();
void build()
{
srand(time(0));
x = random(115) + 2;
y = random(27) + 2;
}
};
bool food::ok()
{
bool flag = true;
if (x % 2 != 1)return false;
for (auto n : snake)
{
if (n.x == x && n.y == y)
{
flag = false;
break;
}
}
return flag;
}
void food::delivery()
{
do
{
build();
} while (!ok());
}
food one;
int score = 0;
int main()
{
initialization();
one.delivery();
while (1)
{
if (1000 - score > 50)
Sleep(1000 - score);
else
Sleep(50);
if (getway())break;
fflush(stdin);
snakemove();
update();
if (die())
{
gotoxy(59, 14);
cout << "得分" << score;
break;
}
if (snake[0].x == one.x && snake[0].y == one.y)
{
one.delivery();
node tmp = snake[snake.size() - 1]; tmp.y++;
snake.push_back(tmp);
getway();
update();
score += 10;
}
}
return 0;
}
void printblock(int x, int y)
{
hide();
gotoxy(x, y);
putchar('\05');
gotoxy(0, 0);
fflush(stdout);
}
void gotoxy(int x, int y)
{
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void hide(void)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void update()
{
system("cls");
for (auto n : snake)
{
printblock(n.x, n.y);
}
printblock(one.x, one.y);
}
void initialization()
{
hide();
for (int i = 1; i <= 4; i++)
{
node block; block.x = 59; block.y = i + 13;
snake.push_back(block);
}
one.delivery();
update();
}
bool getway()
{
if (kbhit())
{
int throw_away = getch();
if (throw_away == 224)
{
int way = getch();
switch (way)
{
case up:
{
if (snake[0].way != down)
{
snake[0].way = up;
}
else
{
return 0;
}
break;
}
case down:
{
if (snake[0].way != up)
{
snake[0].way = down;
}
else
{
return 0;
}
break;
}
case leftw:
{
if (snake[0].way != rightw)
{
snake[0].way = leftw;
}
else
{
return 0;
}
break;
}
case rightw:
{
if (snake[0].way != leftw)
{
snake[0].way = rightw;
}
else
{
return 0;
}
break;
}
}
}
if (throw_away == 98)return 1;
else return 0;
}
return 0;
}
void snakemove()
{
vector<node> tmp = snake;
for (int i = 0; i < snake.size() - 1; i++)
{
snake[i + 1] = tmp[i];
}
switch (snake[0].way)
{
case down:
{
snake[0].y++;
break;
}
case up:
{
snake[0].y--;
break;
}
case leftw:
{
snake[0].x -= 2;
break;
}
case rightw:
{
snake[0].x += 2;
break;
}
}
}
bool die()
{
node tmp = snake[0];
if (tmp.x < 1 || tmp.y < 1 || tmp.x>117 || tmp.y>28)return true;
for (int i = 1; i < snake.size(); i++)
{
if (snake[i].x == tmp.x && snake[i].y == tmp.y)
{
return true;
}
}
return false;
}