好玩的话记得点赞哦!!!
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define X 23
#define Y 75
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int WAIT_TIME=150;
int map_0[X][Y];
int Snake[X*Y][2];
int Slength;
int direction;
int score=0;
bool pdEatFood=false;
void csh();
void huaMap();
void huaSnake();
void gotoxy(int x,int y);
void move();
void intokey();
bool check(int x,int y);
void putfood();
bool gameover();
void dy_fs();
void Game();
int main(){
while(true)
Game();
return 0;
}
void csh(){
system("color 1B");
srand(time(NULL));
Slength=4;
gotoxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
int x,y;
Snake[0][0]=X/2;
Snake[0][1]=Y/2;
for(x=1;x<X-1;x++)
for(y=1;y<Y-1;y++)
map_0[x][y]=0;
for(x=0;x<X;x++){
map_0[x][0]=1;
map_0[x][Y-1]=1;
}
for(y=0;y<Y-1;y++){
map_0[0][y]=1;
map_0[X-1][y]=1;
}
for(x=1; x<4; x++){
Snake[x][0]=Snake[0][0]+x;
Snake[x][1]=Snake[0][1];
}
direction=UP;
}
void huaMap(){
int x,y;
for(x=0; x<X; x++){
for(y=0; y<Y; y++){
if(map_0[x][y]==1)printf("#");
if(map_0[x][y]==0)printf(" ");
}
printf("\n");
}
}
void huaSnake(){
int x;
gotoxy(Snake[0][0],Snake[0][1]);
printf("O");
gotoxy(Snake[1][0],Snake[1][1]);
printf("#");
}
void gotoxy(int i,int j){
COORD position={j,i};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}
void move(){
int i;
if(!gameover()){
gotoxy(Snake[Slength-1][0],Snake[Slength-1][1]);
printf(" ");
}
for(i=Slength-1;i>0;i--){
Snake[i][0]=Snake[i-1][0];
Snake[i][1]=Snake[i-1][1];
}
switch(direction){
case UP:
Snake[0][0]--;
break;
case DOWN:
Snake[0][0]++;
break;
case LEFT:
Snake[0][1]--;
break;
case RIGHT:
Snake[0][1]++;
break;
}
if(pdEatFood){
Slength++;
pdEatFood=false;
}
gotoxy(0,0);
printf("#");
}
void intokey(){
if(kbhit()!=0){
char in;
while(!kbhit()==0)
in=getch();
switch(in){
case 'w':
case 'W':
if(direction!=DOWN)
direction=UP;
break;
case 's':
case 'S':
if(direction!=UP)
direction=DOWN;
break;
case 'a':
case 'A':
if(direction!=RIGHT)
direction=LEFT;
break;
case 'd':
case 'D':
if(direction!=LEFT)
direction=RIGHT;
break;
case 'p':
case 'P':
in=getch();
break;
case '+':case '=':if(WAIT_TIME>0)WAIT_TIME-=10;break;
case '-':case '_':if(WAIT_TIME<1000)WAIT_TIME+=10;break;
}
}
}
bool check(int ii,int jj){
int i;
for(i=0;i<Slength;i++){
if(ii==Snake[i][0]&&jj==Snake[i][1])
return false;
}
return true;
}
void putfood(){
int i,j;
do{
i=rand()%(X-2)+1;
j=rand()%(Y-2)+1;
}while(check(i,j)==false);
map_0[i][j]=-1;
gotoxy(i,j);
printf("$");
return;
}
bool gameover(){
bool isgameover=false;
int sX,sY;
sX=Snake[0][0],sY=Snake[0][1];
if(sX==0||sX==X-1||sY==0||sY==Y-1)
isgameover=true;
int i;
for(i=1;i<Slength;i++){
if(sX==Snake[i][0]&&sY==Snake[i][1])
isgameover=true;
}
return isgameover;
}
void dy_fs(){
gotoxy(X,0);
printf("最终得分:%d ",score);
gotoxy(X+1,0);
printf("速度:%d ",1000-WAIT_TIME);
return;
}
void Game(){
csh();
huaMap();
putfood();
MessageBox(NULL,"欢迎来到贪吃蛇游戏!","温馨提示",MB_OK);
Sleep(1000);
while(true){
if(gameover()){
system("cls");
printf("Game Over!\n");
system("pause");
pdEatFood=false;
WAIT_TIME=150;
score=0;
break;
}
huaSnake();
Sleep(WAIT_TIME);
intokey();
if(gameover()){
system("cls");
printf("Game Over!\n");
system("pause");
pdEatFood=false;
WAIT_TIME=150;
score=0;
break;
}
move();
dy_fs();
if(map_0[Snake[0][0]][Snake[0][1]]==-1){
map_0[Snake[0][0]][Snake[0][1]]=0;
pdEatFood=true;
putfood();
score+=1;
}
}
return;
}