#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#ifndef SNAKE_H
#define SNAKE_H
class Cmp
{
friend class Csnake;
int rSign; //橫坐標
int lSign; //豎坐標
public:
// friend bool isDead(const Cmp& cmp);
Cmp(int r,int l){setPoint(r,l);}
Cmp(){}
void setPoint(int r,int l){rSign=r;lSign=l;}
Cmp operator-(const Cmp &m)const
{
return Cmp(rSign-m.rSign,lSign-m.lSign);
}
Cmp operator+(const Cmp &m)const
{
return Cmp(rSign+m.rSign,lSign+m.lSign);
}
};
const int maxSize = 5; //初始蛇身長度
class Csnake
{
Cmp firstSign; //蛇頭坐標
Cmp secondSign;//蛇頸坐標
Cmp lastSign; //蛇尾坐標
Cmp nextSign; //預備蛇頭
int row; //列數
int line; //行數
int count; //蛇身長度
vector<vector<char> > snakeMap;//整個遊戲界面
queue<Cmp> snakeBody; //蛇身
public:
int GetDirections()const;
char getSymbol(const Cmp& c)const
//獲取指定坐標點上的字符
{
return snakeMap[c.lSign][c.rSign];
}
Csnake(int n)
//初始化遊戲界面大小
{
if(n<20)line=20+2;
else if(n>30)line=30+2;
else line=n+2;
row=line*3+2;
}
bool isDead(const Cmp& cmp)
{
return ( getSymbol(cmp)=='@' || cmp.rSign == row-1
|| cmp.rSign== 0 || cmp.lSign == line-1 ||
cmp.lSign == 0 );
}
void InitInstance(); //初始化遊戲界面
bool UpdataGame(); //更新遊戲界面
void ShowGame(); //顯示遊戲界面
};
#endif // SNAKE_H
using namespace std;
//測試成功
void Csnake::InitInstance()
{
snakeMap.resize(line); // snakeMap[豎坐標][橫坐標]
for(int i=0;i<line;i++)
{
snakeMap[i].resize(row);
for(int j=0;j<row;j++)
{
snakeMap[i][j]=' ';
}
}
for(int m=1;m<maxSize+1;m++)
{
//初始蛇身
snakeMap[line/2][m]='@';
//將蛇身坐標壓入隊列
snakeBody.push(Cmp(m,(line/2)));
//snakeBody[橫坐標][豎坐標]
}
//鏈表頭尾
firstSign=snakeBody.back();
secondSign.setPoint(maxSize-1,line/2);
}
//測試成功
int Csnake::GetDirections()const
{
if(GetKeyState(VK_UP)<0) return 1; //1表示按下上鍵
if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下鍵
if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左鍵
if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右鍵
return 0;
}
bool Csnake::UpdataGame()
{
//-----------------------------------------------
//初始化得分0
static int score=0;
//獲取用戶按鍵信息
int choice;
choice=GetDirections();
cout<<"Total score: "<<score<<endl;
//隨機產生食物所在坐標
int r,l;
//開始初始已經吃食,產生壹個食物
static bool eatFood=true;
//如果吃了壹個,才再出現第2個食物
if(eatFood)
{
do
{
//坐標範圍限制在(1,1)到(line-2,row-2)對點矩型之間
srand(time(0));
r=(rand()%(row-2))+1; //橫坐標
l=(rand()%(line-2))+1;//豎坐標
//如果隨機產生的坐標不是蛇身,則可行
//否則重新產生坐標
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='*';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇頭和社頸的橫坐標不相同,執行下面操作
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);
//否則,如下在原本方向上繼續移動
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='*' && !isDead(nextSign))
//如果沒有碰到食物(且沒有死亡的情況下),刪除蛇尾,壓入新的蛇頭
{
//刪除蛇尾
lastSign=snakeBody.front();
snakeMap[lastSign.lSign][lastSign.rSign]=' ';
snakeBody.pop();
//更新蛇頭
secondSign=firstSign;
//壓入蛇頭
snakeBody.push(nextSign);
firstSign=snakeBody.back();
snakeMap[firstSign.lSign][firstSign.rSign]='@';
//沒有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='*' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSign.lSign][nextSign.rSign]='@';
//只壓入蛇頭
snakeBody.push(nextSign);
firstSign=snakeBody.back();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
int main()
{
Csnake s(20);
s.InitInstance();
//s.ShowGame();
int noDead;
do
{
s.ShowGame();
noDead=s.UpdataGame();
}while (noDead);
system("pause");
return 0;
}
這個代碼可以運行的,記得給分啦