#include <iostream>
#include <conio.h>
#include <List>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
struct Pos
{
int x;
int y;
};
list <Pos>shenti;
int a[20][20] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
//画面输出
void shuchu(int a[20][20]);
//改变蛇身链表
bool gaibianshenti(char fangxiang,Pos &shiwu,bool &siwang);
//判断食物是否与蛇身重合
bool ischonghe(Pos shiwu);
int main()
{
//蛇头
Pos tou = {2,2};
shenti.push_back(tou);
//初始化方向
char fangxiang='d';
//初始化食物点
Pos shiwu = {0,0};
//是否死亡
bool siwang=false;
int a1[20][20];
//创建计时器1秒刷新一次
clock_t start = clock();
clock_t end;
char g = ' ';
while (true)
{
//Sleep(2000);
end = clock();
//输入方向接收
if (_kbhit())
{
g = _getch();
}
if (1 == ((end - start) / 1000))
{
if (g == 'a' || g == 'w' || g == 's' || g == 'd')
{
//cout << g << endl;
//反方向无法通行
if (fangxiang == 'a' && g == 'w')
{
fangxiang = g;
}
else if (fangxiang == 'a' && g == 's')
{
fangxiang = g;
}
else if (fangxiang == 'w' && g == 'd')
{
fangxiang = g;
}
else if (fangxiang == 'w' && g == 'a')
{
fangxiang = g;
}
else if (fangxiang == 's' && g == 'd')
{
fangxiang = g;
}
else if (fangxiang == 's' && g == 'a')
{
fangxiang = g;
}
else if (fangxiang == 'd' && g == 's')
{
fangxiang = g;
}
else if (fangxiang == 'd' && g == 'w')
{
fangxiang = g;
}
}
//初始化地图
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
a1[i][j] = a[i][j];
}
}
//每次产生食物一个
srand(time(0));
if (shiwu.x==0&&shiwu.y==0)
{
do
{
shiwu.x = rand() % 18 + 1;
shiwu.y = rand() % 18 + 1;
}while (ischonghe(shiwu));//判断食物是否与蛇身重合
//cout << shiwu.x << "," << shiwu.y << endl;
}
bool chidao=gaibianshenti(fangxiang,shiwu,siwang);//判断是否吃到食物
//渲染地图
if (chidao)
{
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
a1[it->y][it->x] = 2;
}
}
else
{
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
a1[it->y][it->x] = 2;
}
a1[shiwu.y][shiwu.x] = 3;
}
system("cls");
//打印
shuchu(a1);
if (siwang == true)
{
break;
}
//system("cls");
start = end;
}
}
system("cls");
cout << "游戏结束" << endl;
return 0;
}
void shuchu(int a[20][20])
{
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
if (a[i][j] == 0)
{
cout << " ";
}
else if (a[i][j] == 1)
{
cout << "#";
}
else if (a[i][j] == 2)
{
cout << "@";
}
else if (a[i][j]==3)
{
cout << "*";
}
else
{
cout << a[i][j];
}
}
cout << endl;
}
}
bool gaibianshenti(char fangxiang, Pos &shiwu, bool &siwang)
{
//shiwu.x = 0;
//shiwu.y = 0;
if (fangxiang == 'a')
{
//cout << fangxiang << endl;
Pos p;
p.x=shenti.begin()->x - 1;
p.y = shenti.begin()->y;
//判断是否碰撞身体
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
if (p.x == it->x && p.y == it->y)
{
siwang = true;
}
}
//是否碰撞边框
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
if (a[i][j] == 1)
{
if (p.x == j && p.y == i)
{
siwang = true;
}
}
}
}
shenti.push_front(p);
if (shiwu.x == p.x && shiwu.y == p.y)
{
shiwu.x = 0;
shiwu.y = 0;
return true;
}
else
{
shenti.pop_back();
}
}
else if (fangxiang=='s')
{
//cout << fangxiang << endl;
Pos p;
p.x = shenti.begin()->x;
p.y = shenti.begin()->y+1;
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
if (p.x == it->x && p.y == it->y)
{
siwang = true;
}
}
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
if (a[i][j] == 1)
{
if (p.x == j && p.y == i)
{
siwang = true;
}
}
}
}
shenti.push_front(p);
if (shiwu.x == p.x && shiwu.y == p.y)
{
shiwu.x = 0;
shiwu.y = 0;
return true;
}
else
{
shenti.pop_back();
}
}
else if(fangxiang=='d')
{
//cout << fangxiang << endl;
Pos p;
p.x = shenti.begin()->x + 1;
p.y = shenti.begin()->y;
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
if (p.x == it->x && p.y == it->y)
{
siwang = true;
}
}
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
if (a[i][j] == 1)
{
if (p.x == j && p.y == i)
{
siwang = true;
}
}
}
}
shenti.push_front(p);
if (shiwu.x == p.x && shiwu.y == p.y)
{
shiwu.x = 0;
shiwu.y = 0;
return true;
}
else
{
shenti.pop_back();
}
}
else if(fangxiang=='w')
{
//cout << fangxiang << endl;
Pos p;
p.x = shenti.begin()->x;
p.y = shenti.begin()->y-1;
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
if (p.x == it->x && p.y == it->y)
{
siwang = true;
}
}
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
if (a[i][j] == 1)
{
if (p.x == j && p.y == i)
{
siwang = true;
}
}
}
}
shenti.push_front(p);
if (shiwu.x == p.x && shiwu.y == p.y)
{
shiwu.x = 0;
shiwu.y = 0;
return true;
}
else
{
shenti.pop_back();
}
}
return false;
}
bool ischonghe(Pos shiwu)
{
list<Pos>::iterator it = shenti.begin();
for (it = shenti.begin(); it != shenti.end(); it++)
{
if (it->x==shiwu.x&&it->y==shiwu.y)
{
return true;
}
}
return false;
}
控制台贪吃蛇
于 2022-06-23 09:37:04 首次发布