#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
#define WIDTH 30
#define HEIGHT 20
enum Dir { STOP = 0, LEFT, RIGHT, UP, DOWN };
Dir dir;
int snakeX[200], snakeY[200];
int len;
int foodX, foodY;
int score;
int delay = 120;
DWORD startTime; // 游戏启动时间戳
void GotoXY(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO ci;
ci.dwSize = 1;
ci.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);
}
void SpawnFood()
{
bool overlap;
do
{
overlap = false;
foodX = rand() % (WIDTH - 2) + 1;
foodY = rand() % (HEIGHT - 2) + 1;
for (int i = 0; i < len; i++)
{
if (snakeX[i] == foodX && snakeY[i] == foodY)
{
overlap = true;
break;
}
}
} while (overlap);
}
void Init()
{
dir = STOP;
len = 4;
snakeX[0] = WIDTH / 2;
snakeY[0] = HEIGHT / 2;
snakeX[1] = snakeX[0] - 1;
snakeY[1] = snakeY[0];
snakeX[2] = snakeX[0] - 2;
snakeY[2] = snakeY[0];
snakeX[3] = snakeX[0] - 3;
snakeY[3] = snakeY[0];
}
void Draw()
{
GotoXY(0, 0);
for (int i = 0; i < WIDTH; i++)
cout << "#";
cout << endl;
}
void Input()
{
if (_kbhit())
{
int key = _getch();
if (key == 224)
{
key = _getch();
switch (key)
{
case 72: if(dir != DOWN) dir = UP; break;
case 80: if(dir != UP) dir = DOWN; break;
case 75: if(dir != RIGHT) dir = LEFT; break;
case 77: if(dir != LEFT) dir = RIGHT; break;
}
}
else if (key == 'q' || key == 'Q')
{
exit(0);
}
}
}
void Logic()
{
if(dir == STOP)
return;
}
int main()
{
HideCursor();
srand((unsigned)time(NULL));
Init();
}