C++#include<bits/stdc++.h>
#include <Windows.h>
#include <time.h>
#include <conio.h>
#ifndef __cplusplus
typedef char bool;
#endif
void got(int x, int y){
COORD coord;
coord.X=x;coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
struct ac{int x,y;}food;
struct bc{int sp,l,x[100],y[100];}sn;
void gotoxy(int x,int y);
int key=72,ch,score,i,o;
void dr(){
for(i=0; i<=78; i+=2){
got(i,0);printf("■");
got(i,24);printf("■");
}
for(i=1; i<24; i++){
got(0,i);printf("■");
got(78,i);printf("■");
}
while(1){
srand((unsigned int)time(NULL));
food.x=rand()%(74)+2;
food.y=rand()%(22)+1;
if(food.x%2==0)break;
}
got(food.x,food.y);
printf("#");
sn.l=3,sn.sp=200,sn.x[0]=40,sn.y[0]=12;
got(sn.x[0],sn.y[0]);
std::cout<<o;
for(i=1; i<sn.l; i++){
sn.x[i]=sn.x[i-1]+2,sn.y[i]=sn.y[i-1];
got(sn.x[i],sn.y[i]);
std::cout<<o;
}got(76,0);
}
void ke(){
int pre_key=key;
if(_kbhit()){fflush(stdin);key=_getch();key=_getch();}
if(ch==0){got(sn.x[sn.l-1],sn.y[sn.l-1]);printf(" ");}
for(i=sn.l-1; i>0; i--) sn.x[i]=sn.x[i-1],sn.y[i]=sn.y[i-1];
if(pre_key==72&&key==80) key=72;
if(pre_key==80&&key==72) key=80;
if(pre_key==75&&key==77) key=75;
if(pre_key==77&&key==75) key=77;
switch(key){
case 75:sn.x[0]-=2;break;
case 77:sn.x[0]+=2;break;
case 72:sn.y[0]--;break;
case 80:sn.y[0]++;break;
}
got(sn.x[0],sn.y[0]);
std::cout<<o;got(76,0);ch=0;
}
void cre(){
if(sn.x[0]==food.x&&sn.y[0]==food.y){
while(1){
int flag=1;
srand((unsigned int)time(NULL));
food.x=rand()%(74)+2;food.y=rand()%(22)+1;
for(i=0; i<sn.l; i++)
if(sn.x[i]==food.x&&sn.y[i]==food.y){flag=0;break;}
if(flag&&food.x%2==0) break;
}
got(food.x,food.y);
printf("#"),sn.l++,score+=10,sn.sp+=5,ch=1,o++;
}
}
bool tus(){
if(sn.y[0]==0||sn.y[0]==24)return 0;
if(sn.x[0]==0||sn.x[0]==78)return 0;
for(i=1; i<sn.l; i++) if(sn.x[i]==sn.x[0]&&sn.y[i]==sn.y[0]) return 0;
return 1;
}
int main(){
dr();
while(1){
ke();if(!tus()) break;
cre();Sleep(sn.sp);
}
got(39,12);std::cout<<"Game Over!\n";
got(39,13);printf("本次游戏得分为:%d\n",score*1000000);
Sleep(5000);
return 0;
}