#include <stdio.h>
#include <tchar.h>
#include<string>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<iostream>
#include<conio.h>
using namespace std;
class world
{
public:
world();
~world();
void outworld();
void creatworld();
void clearworld();
void setfood();
void creatsnake();
void move();
private:
char ok[30][30];
int jlx[900];
int jly[900];
int sx;
int sy;
char where;
int speed;
int level;
int eat;
};
world::world()
{
}
world::~world()
{
}
void world::creatworld()
{
int i, j;
for(i=0;i<900;i++)
{
jlx[i]=0;
jly[i]=0;
}
sx=0;sy=0;
i = 0;
for (j = 0; j < 30; j++)
{
ok[i][j] = '*';
}
i = 29;
for (j = 0; j < 30; j++)
{
ok[i][j] = '*';
}
j = 0;
for (i = 0; i < 30; i++)
{
ok[i][j] = '#';
}
j = 29;
for (i = 0; i < 30; i++)
{
ok[i][j] = '#';
}
for (i = 1; i < 29; i++)
{
for (j = 1; j < 29; j++)
{
ok[i][j] = ' ';
}
}
}
void world::setfood()
{
int i, j;
while (1)
{
i = rand() % 30 + 1;
j = rand() % 30 + 1;
if (ok[i][j] == ' ')
{
ok[i][j] = '@'; return;
}
else
{
continue;
}
}
}
void world::clearworld()
{
system("cls");
}
void world::outworld()
{
int i, j;
cout<<" "<<endl;
for (i = 0; i < 30; i++)
{
for (j = 0; j < 30; j++)
{
cout << ok[i][j];
}
cout << endl;
}
cout<<"得分(吃的食物):"<<eat<<endl;
level=eat/5;
cout<<"等级(随着得分提升):"<<level<<endl;
}
void world::creatsnake()
{
int i=16,j=16;
jlx[0]=i;sy++;
jly[0]=j;sx++;
level = 1; speed = 500;
eat=0;
return;
}
void world::move()
{
creatworld();
creatsnake();
setfood();
char direct = 'd',direct2='d';where = direct;
time_t a;
int pd=0;
a = clock();
while (1)
{
if(!_kbhit())
{
}
else
{
direct2=_getch();
if (direct2 == 'p')
{
system("pause");
}
else if (direct2 == 'w')
{
direct = 'w';
}
else if (direct2 == 's')
{
direct = 's';
}
else if (direct2 == 'a')
{
direct = 'a';
}
else if (direct2 == 'd')
{
direct = 'd';
}
else
{
;
}
}
if(direct=='w')
{
if(jlx[0]-1==0||ok[jlx[0]-1][jly[0]]=='$')
{
cout<<"GAMEOVER"<<endl;
system("pause");break;
}
else if(ok[jlx[0]-1][jly[0]]=='@')
{
eat++;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
sx++;sy++;
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jlx[0]-=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
setfood();
}
else if(ok[jlx[0]-1][jly[0]]==' ')
{
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jlx[0]-=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
}
}
else if(direct=='s')
{
if(jlx[0]+1==29||ok[jlx[0]+1][jly[0]]=='$')
{
cout<<"GAMEOVER"<<endl;
system("pause");break;
}
else if(ok[jlx[0]+1][jly[0]]=='@')
{
eat++;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
sx++;sy++;
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jlx[0]+=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
setfood();
}
else if(ok[jlx[0]+1][jly[0]]==' ')
{
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jlx[0]+=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
}
}
else if(direct=='a')
{
if(jly[0]-1==0||ok[jlx[0]][jly[0]-1]=='$')
{
cout<<"GAMEOVER"<<endl;
system("pause");break;
}
else if(ok[jlx[0]][jly[0]-1]=='@')
{
eat++;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
sx++;sy++;
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jly[0]-=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
setfood();
}
else if(ok[jlx[0]][jly[0]-1]==' ')
{
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jly[0]-=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
}
}
else if(direct=='d')
{
if(jly[0]+1==29||ok[jlx[0]][jly[0]+1]=='$')
{
cout<<"GAMEOVER"<<endl;
system("pause");break;
}
else if(ok[jlx[0]][jly[0]+1]=='@')
{
eat++;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
sx++;sy++;
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jly[0]+=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
setfood();
}
else if(ok[jlx[0] ][jly[0]+1]==' ')
{
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]=' ';
}
for(int i=sx-1;i>=1;i--)
{
jlx[i]=jlx[i-1];
jly[i]=jly[i-1];
}
jly[0]+=1;
for(int i=0;i<sx;i++)
{
ok[jlx[i]][jly[i]]='$';
}
}
}
a = clock();
outworld();
if(eat/5>=0&&eat/5<=2)
{
Sleep(150);
}
else if(eat/5>2&&eat/5<=4)
{
Sleep(100);
}
else if(eat/5>4&&eat/5<=6)
{
Sleep(66);
}
else
{
Sleep(60);
}
clearworld();
}
}
int main()
{
srand((int)time(NULL));
cout<<"是否开始游戏?(yes or no)"<<endl;
string b;
while(1)
{
cin>>b;
if(b=="yes")
{
break;
}
else if(b=="no")
{
cout<<"听你哒"<<endl;return 0;
}
else
{
continue;
}
}
cout<<"wsad控制方向,p暂停,如果撞墙或撞到自己或者操作不合法则死亡"<<endl;
system("pause");
world a;
a.move();
return 0;
}
贪吃蛇小demo
最新推荐文章于 2021-08-24 10:41:05 发布