原创贪吃蛇(仅在windows系统下可用 Dev-C++可用 其他IDE未测试 请使用英文输入法游玩):
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
string c[110][110];
void print(int x,int y){
system("cls");
for(int i=x-20;i<=x+20;i++){
for(int j=y-20;j<=y+20;j++)
if(i<0||i>100||j<0||j>100) cout<<" ";
else cout<<c[i][j];
cout<<endl;
}
}
int x=50,y=50,len=5,fx;
struct node{
int x,y;
};
queue<node>q;
const int dx[]{-1,1,0,0},dy[]{0,0,-1,1};
signed main(){
srand(time(NULL));
q.push(node{50,50});
q.push(node{51,50});
q.push(node{52,50});
q.push(node{53,50});
q.push(node{54,50});
for(int i=0;i<=100;i++)
for(int j=0;j<=100;j++)
if(0);
else if(i==0&&j==0) c[i][j]="┌ ";
else if(i==0&&j==100) c[i][j]="┐ ";
else if(i==100&&j==0) c[i][j]="└ ";
else if(i==100&&j==100) c[i][j]=="┘ ";
else if(i==0) c[i][j]="┬ ";
else if(i==100) c[i][j]="┴ ";
else if(j==0) c[i][j]="├ ";
else if(j==100) c[i][j]="┤ ";
else if(i==x&&j==y) c[i][j]="■";
else c[i][j]=" ";
while(1){
if(x<1||x>99||y<1||y>99){
cout<<"lose,length:"<<len;
Sleep(5000);
return 0;
}
print(x,y);
if(_kbhit()){
char cur=_getch();
switch(cur){
case 'w':fx=0;break;
case 's':fx=1;break;
case 'a':fx=2;break;
case 'd':fx=3;break;
}
}
if(rand()%20==0) c[rand()%99+1][rand()%99+1]="★";
x=x+dx[fx];y=y+dy[fx];
if(c[x][y]=="★") len++;
if(c[x][y]=="■"){
cout<<"lose,length:"<<len;
Sleep(5000);
return 0;
}
c[x][y]="■";
q.push(node{x,y});
if(q.size()>len){
int nx=q.front().x,ny=q.front().y;
c[nx][ny]=" ";
q.pop();
}
Sleep(100);
}
return 0;
}