【LB】C语言实现贪吃蛇

时间:2022-09-02 11:11:22
懒得写注释~~~
【LB】C语言实现贪吃蛇
#include<stdio.h> #include<windows.h> #include<conio.h> #include<time.h> #include<stdlib.h>
typedef struct{ int x; int y; }foods;
typedef struct{ int x; int y; }point;
typedef struct{ int top;
point at[100]; }points;
points a; foods food;

int map[20][20]={  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}    };
void print(){ int mx,my; for(my=0;my<20;my++){ for(mx=0;mx<20;mx++){ if(map[mx][my]==1) printf("田"); if(map[mx][my]==2) printf("■"); if(map[mx][my]==0) printf("  ");} printf("\n");} }
void push(int x,int y) {if(a.top>99) printf("错误"); else{ a.top++; a.at[a.top].x=x; a.at[a.top].y=y; } }
void start(){ a.top=-1; push(3,10); push(4,10); }
void draw(){ int temp; for(temp=a.top;temp>=0;temp--) map[a.at[temp].x][a.at[temp].y]=2; }
int check_food(){ int temp; for(temp=a.top;temp>=0;temp--) if(food.x==a.at[temp].x&&food.y==a.at[temp].y) return 1; return 0; }
void set_food(){ do{ srand((int)time(0)); food.x=rand()%18+1; food.y=rand()%18+1; map[food.x][food.y]=2;} while(check_food()); }
int check(int x) {int temp; switch(x){ case 1: for(temp=a.top-3;temp>0;temp--) if(a.at[a.top].x+1==a.at[temp].x&&a.at[a.top].y==a.at[temp].y) return 1;break; case 2: for(temp=a.top-3;temp>0;temp--) if(a.at[a.top].y+1==a.at[temp].y&&a.at[a.top].x==a.at[temp].x) return 1;break; case 3: for(temp=a.top-3;temp>0;temp--) if(a.at[a.top].x-1==a.at[temp].x&&a.at[a.top].y==a.at[temp].y) return 1;break; case 4: for(temp=a.top-3;temp>0;temp--) if(a.at[a.top].y-1==a.at[temp].y&&a.at[a.top].x==a.at[temp].x) return 1;} return 0;
}
void goright(){ int temp; if(a.at[a.top].x+1==food.x&&a.at[a.top].y==food.y) {push(food.x,food.y); set_food(); } else if(a.at[a.top].x+1==19) {print();printf("撞墙啦!");system("pause");exit(1);} else if(check(1)) {print();printf("咬到自己啦!");system("pause");exit(1);} else {map[a.at[0].x][a.at[0].y]=0; for(temp=0;temp<a.top;temp++){ a.at[temp].x=a.at[temp+1].x; a.at[temp].y=a.at[temp+1].y;} a.at[a.top].x++;} }
void godown(){ int temp; if(a.at[a.top].y+1==food.y&&a.at[a.top].x==food.x) {push(food.x,food.y); set_food(); } else if(a.at[a.top].y+1==19) {print();printf("撞墙啦!");system("pause");exit(1);} else if(check(2)) {print();printf("咬到自己啦!");system("pause");exit(1);} else{ map[a.at[0].x][a.at[0].y]=0; for(temp=0;temp<a.top;temp++){ a.at[temp].x=a.at[temp+1].x; a.at[temp].y=a.at[temp+1].y;} a.at[a.top].y++;} }
void goleft(){ int temp; if(a.at[a.top].x-1==food.x&&a.at[a.top].y==food.y) {push(food.x,food.y); set_food(); } else if(a.at[a.top].x-1==0) {print();printf("撞墙啦!");system("pause");exit(1);} else if(check(3)) {print();printf("咬到自己啦!");system("pause");exit(1);} else{ map[a.at[0].x][a.at[0].y]=0; for(temp=0;temp<a.top;temp++){ a.at[temp].x=a.at[temp+1].x; a.at[temp].y=a.at[temp+1].y;} a.at[a.top].x--;} }
void goup(){ int temp; if(a.at[a.top].y-1==food.y&&a.at[a.top].x==food.x) {push(food.x,food.y); set_food(); } else if(a.at[a.top].y-1==0) {print();printf("撞墙啦!");system("pause");exit(1);} else if(check(4)) {print();printf("咬到自己啦!");system("pause");exit(1);} else{ map[a.at[0].x][a.at[0].y]=0; for(temp=0;temp<a.top;temp++){ a.at[temp].x=a.at[temp+1].x; a.at[temp].y=a.at[temp+1].y;} a.at[a.top].y--;} }

void go(int way){ switch(way){ case 1:goright();break; case 2:godown();break; case 3:goleft();break; case 4:goup(); } }
void main(){ int way=1; char keyboard; start(); set_food(); draw(); print(); while(1){ if(!_kbhit()){ system("cls");      go(way); draw(); print(); Sleep(200);} else { keyboard=_getch(); if(way!=3&&keyboard=='d') way=1; else if(way!=4&&keyboard=='s') way=2; else if(way!=1&&keyboard=='a') way=3; else if(way!=2&&keyboard=='w') way=4; else if(keyboard=='p') system("pause");} }; }