#include<easyx.h>
#include<windows.h>
#include<conio.h>
#include<cstdlib>
#include<ctime>
#include<string>
#include<thread>
#include<queue>
#define LARGE 15
#define X 25
#define Y 19
#define SPEED 200
using namespace std;
const long long fx[4]={0,1,0,-1},fy[4]={-1,0,1,0};
long long mp[X+10][Y+10];
char c=' ';
bool running=1;
void input(){
while(running){
c=getch();
}
return;
}
void output(long long x,long long y){
if(mp[x][y]==0){
setlinecolor(WHITE);
setfillcolor(WHITE);
}
if(mp[x][y]==1){
setlinecolor(RED);
setfillcolor(RED);
}
if(mp[x][y]==2){
setlinecolor(BLACK);
setfillcolor(WHITE);
}
if(mp[x][y]==3){
setlinecolor(BLACK);
setfillcolor(BLACK);
}
fillrectangle((x-1)*LARGE,(y-1)*LARGE,x*LARGE,y*LARGE);
}
void new_apple(){
srand(time(0));
long long t1,t2;
do{
t1=rand()%X+1,t2=rand()%Y+1;
}while(mp[t1][t2]!=0);
mp[t1][t2]=1;
output(t1,t2);
}
queue<long long>qx,qy;
int main(){
initgraph(X*LARGE,Y*LARGE);
thread LBWNB(input);
setbkcolor(WHITE);
long long x=X/2,y=(Y+1)/2,f=3,l=3;
mp[x][y]=3,mp[x+1][y]=mp[x+2][y]=2;
qx.push(x+2);qy.push(y);
qx.push(x+1);qy.push(y);
qx.push(x);qy.push(y);
BeginBatchDraw();
new_apple();
while(1){
mp[x][y]=2;
if(c=='w'&&f!=2){
f=0;
c=' ';
}
else if(c=='d'&&f!=3){
f=1;
c=' ';
}
else if(c=='s'&&f!=0){
f=2;
c=' ';
}
else if(c=='a'&&f!=1){
f=3;
c=' ';
}
x=x+fx[f],y+=fy[f];
if(x==0){
x=X;
}
if(y==0){
y=Y;
}
if(x==X+1){
x=1;
}
if(y==Y+1){
y=1;
}
if(mp[x][y]==2){
FlushBatchDraw();
break;
}
if(mp[x][y]==1){
new_apple();
}
else{
mp[qx.front()][qy.front()]=0;
qx.pop();
qy.pop();
}
mp[x][y]=3;
qx.push(x);
qy.push(y);
for(int i=1;i<=X;++i){
for(int j=1;j<=Y;++j){
output(i,j);
}
}
qx.push(-1);
while(qx.front()!=-1){
output(qx.front(),qy.front());
qx.push(qx.front());
qy.push(qy.front());
qx.pop();
qy.pop();
}
qx.pop();
FlushBatchDraw();
Sleep(SPEED);
}
running=0;
string s="game over";
settextcolor(BLACK);
outtextxy(X*LARGE/2-LARGE*2,Y*LARGE/2,&s[0]);
FlushBatchDraw();
system("pause");
EndBatchDraw();
closegraph();
return 0;
}look here
这是 2025年10月29日 存档的第 1 版,不是当前版本。查看当前版本