#include<iostream>
#include<conio.h>
#include<thread>
using namespace std;
using namespace std::literals;
int map[20][20], snake[1000][3], pts, length, fx;
auto speed=1.0s;
bool quit;
inline void sleep(auto time) {
this_thread::sleep_for(time);
}
struct Game {
static void gameover()
{
quit = true;
sleep(0.5s);
system(cls);
cout << 你死了,最终得到了 << pts << 分,按F重新开始游戏;
char key = 0;
while (1)
{
if (_kbhit())
{
key = _getch();
if (key == 'f' or key == 'F')
{
break;
}
}
}
initialize();
}
static void move()
{
while (1)
{
if (quit)return;
sleep(speed);
int lastx = snake[length][1];
int lasty = snake[length][2];
if (length >= 2)
{
map[lastx][lasty] = 0;
map[snake[1][1]][snake[1][2]] = 1;
}
else
{
map[snake[1][1]][snake[1][2]] = 0;
}
for (int i = length; i >= 2; i--)
{
snake[i][1] = snake[i - 1][1];
snake[i][2] = snake[i - 1][2];
}
bool crack = false;
if (fx == 1)
{
snake[1][1]--;
}
if (fx == 2)
{
snake[1][1]++;
}
if (fx == 3)
{
snake[1][2]--;
}
if (fx == 4)
{
snake[1][2]++;
}
int x = snake[1][1];
int y = snake[1][2];
if (x < 1 or map[x][y] == 1 or y < 1 or x > 15 or y > 15)
{
gameover();
}
else
{
if (map[x][y] == 3)
{
int bs = 1;
if (speed == 0.5s)bs = 2;
if (speed == 0.3s)bs = 3;
if (speed == 0.1s)bs = 5;
pts=pts+bs;
length++;
snake[length][1] = lastx;
snake[length][2] = lasty;
map[lastx][lasty] = 1;
fruit();
}
map[x][y] = 2;
}
}
}
static void display()
{
while (1)
{
if (quit)return;
cout << 分数: << pts << endl;
for (int i = 1; i <= 16; i++)cout << --;
cout << endl;
for (int i = 1; i <= 15; i++)
{
for (int j = 1; j <= 15; j++)
{
if (map[i][j] == 0)cout << ;
if (map[i][j] == 1)cout << ██;
if (map[i][j] == 2)
{
if (fx == 1)cout << ↑ ;
if (fx == 2)cout << ↓ ;
if (fx == 3)cout << ← ;
if (fx == 4)cout << → ;
}
if (map[i][j] == 3)cout << #;
}
cout << | << endl;
}
for (int i = 1; i <= 16; i++)cout << --;
cout << endl<<移动:WASD/↑←↓→;
cout << endl << 重新开始:一头创死;
string temp = ;
if (speed == 1s)temp = 1;
if (speed == 0.5s)temp = 0.5;
if (speed == 0.3s)temp = 0.3;
if (speed == 0.1s)temp = 0.1;
cout << endl << 当前速度: << temp << s/格,调整速度请按T键 << endl;
sleep(0.1s);
system(cls);
}
}
static void monitor()
{
char key = 0;
while (1)
{
if (quit)return;
key = 0;
if (_kbhit())
{
key = _getch();
int ff = 0;
if (snake[2][1] == snake[1][1] - 1)ff = 1;
if (snake[2][1] == snake[1][1] + 1)ff = 2;
if (snake[2][2] == snake[1][2] - 1)ff = 3;
if (snake[2][2] == snake[1][2] + 1)ff = 4;
int temp = fx;
if (key == 72 or key == 'w' or key == 'W')fx = 1;
else if (key == 75 or key == 'a' or key == 'A')fx = 3;
else if (key == 77 or key == 'd' or key == 'D')fx = 4;
else if (key == 80 or key == 's' or key == 'S')fx = 2;
else if (key == 't' or key == 'T')
{
if (speed == 1s)speed = 0.5s;
else if (speed == 0.5s)speed = 0.3s;
else if (speed == 0.3s)speed = 0.1s;
else speed = 1s;
}
if (fx == ff)fx = temp;
}
}
}
static void fruit()
{
int x = 0;
int y = 0;
while (map[x][y] == 0)
{
x = rand() % 15 + 1;
y = rand() % 15 + 1;
if (map[x][y] == 0)
{
map[x][y] = 3;
break;
}
x = 0;
y = 0;
}
}
static void initialize()
{
srand(time(0));
speed = 1s;
pts = 0;
quit = false;
int xx = 0;
int yy = 0;
for (int i = 1; i <= 15; i++)
for (int j = 1; j <= 15; j++)map[i][j] = 0;
length = 1;
xx = rand() % 12 + 2;
yy = rand() % 12 + 2;
snake[1][1] = xx;
snake[1][2] = yy;
fx = rand() % 4 + 1;
map[xx][yy] = 2;
fruit();
thread t1(display);
thread t2(monitor);
thread t3(move);
t1.join();
t2.join();
t3.join();
}
};
int main()
{
Game::initialize();
}
你刚刚浪费了人生中宝贵的几分钟。