正文
增加 1 行删除 138 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138```cpp#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 200using 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;}``````1https://cdn.luogu.com.cn/upload/image_hosting/bn0ocyh7.png