From 2425d6615102b80cae1a9bf313a1fd2c6302efc9 Mon Sep 17 00:00:00 2001 From: shine56 <732337565@qq.com> Date: Tue, 8 Jan 2019 16:00:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 贪吃蛇.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 332 insertions(+) create mode 100644 贪吃蛇.c diff --git a/贪吃蛇.c b/贪吃蛇.c new file mode 100644 index 0000000..3daad67 --- /dev/null +++ b/贪吃蛇.c @@ -0,0 +1,332 @@ +#include +#include +#include +#include +#include +#include + +#define H 30 +#define K 59 +int V=300; //Ĭٶ +int score=0, ko=-6, foodsum=0; // , koscorenλnո foodsum¼Եʳ +int a[H][K]; + +void gamestart(int (*a)[K]); //Ϸʼ +void show(int (*a)[K]); // ʾ +void gameplay(int (*a)[K]); // Ϸ +void Smove(char move, int (*a)[K]); //ߵƶ +void gotoxy(int x, int y); //꺯 +void food(int (*a)[K]); //ʳ +void changevelocity(); //ıٶ +void gameover(); //Ϸ +void show2(int m); //ٶѡ + + +int main() +{ + char flag='s'; + while(flag=='s') + { + gamestart(a); + changevelocity(); + gameplay(a); + gameover(); + flag = getch(); + } + + return 0; +} +void gotoxy(int x, int y)//λú +{ + HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); + COORD pos; + pos.X = x; + pos.Y = y; + SetConsoleCursorPosition(handle, pos); +} + +void gamestart(int (*a)[K]) //ʼʼ +{ + score = 0; + ko=-6; foodsum=0; V=300; + + int i, j, k; + for(i=0; i0; i--)// + a[15][25-i]=i; + + int fx, fy; //ʳ + srand(time(NULL)); + do{ + fx=rand()%25 + 2; + fy=rand()%45 + 14; + }while(a[fx][fy]!=0); + a[fx][fy]=-2; + + //ʼֺͷ, -5ո -61ո , 02ո + a[9][2]=-11; + for(k=3; k<7; k++) + a[9][k]=-5; + a[9][7]=-6; + + a[11][2]=-9; + a[12][2]=-8; + for(k=3; k<7; k++) + { + a[11][k]=-5; a[12][k]=-5; + } + a[11][7]=-6; a[12][7]=-6; + + a[24][2]=-10; a[24][3]=-5; a[24][4]=-5; a[24][5]=-6; + + show(a); +} + +void show(int (*a)[K])//Ϸʾ +{ + gotoxy(0, 0); + int i, j; + system("color b0"); + for(i=0; i1) + printf(""); + if(a[i][j]==1) + printf(""); + if(a[i][j]==-2) + printf(""); + if(a[i][j]==-20) + printf(""); + if(a[i][j]==-9) + printf("[w] [s]"); + if(a[i][j]==-8) + printf("[a] [d]"); + if(a[i][j]==-10) + printf("÷֣%d", score); + if(a[i][j]==-11) + printf("[j]ѡٶ"); + } + printf("\n"); + } + //getchar(); +} + +void show2(int m) +{ + int i; + gotoxy(55, 7); + printf(""); + for(i=0; i0) + { + a[i][j]++; + if(a[i][j]>max) + { + max=a[i][j]; + wx=i; wy=j; + } + if(a[i][j]==2) + { + hx=i; hy=j; + } + } + } + } + int hx1, hy1; + if(move=='d')// + { + hx1=hx; hy1=hy+1; + } + if(move=='w')// + { + hx1=hx-1; hy1=hy; + } + if(move=='s')// + { + hx1=hx+1; hy1=hy; + } + if(move=='a')// + { + hx1=hx; hy1=hy-1; + } + + if(a[hx1][hy1]==-2 || a[hx1][hy1]==-20) //Եʳ + { + if(a[hx1][hy1]==-2) + { + foodsum++; + score+=10; + } + else + score+=100; + + if(score>9 && score<100) a[24][5]=-5; + if(score>99) a[24][6]=-6; + if(score>999) a[24][6]=-5; + a[hx1][hy1]=0; + food(a); + } + else{ + a[wx][wy]=0; + } + if(a[hx1][hy1]>1 || a[hx1][hy1]==-1) //Լײǽ + { + a[0][0]=-3; + } + else + { + a[hx1][hy1]=1; + } +} + +void food(int (*a)[K])//µʳ +{ + int fx, fy; + srand(time(NULL)); + do{ + fx=rand()%25 + 2; + fy=rand()%45 + 14; + }while(a[fx][fy]!=0); + //printf("%d %d\n", fx, fy); + if(foodsum==5) + { + foodsum=0; + a[fx][fy]=-20; + } + else + a[fx][fy]=-2; +} + +void changevelocity() //ıٶ +{ + int m=2; + char ve, ch; + while(1) + { + ch=getch(); + if(ch=='j' || ch=='J') + break; + else + { + gotoxy(40, 4); + printf("ΪӢ״̬ѡٶȣ"); + } + } + + if(ch=='j' || ch=='J') + { + show2(m); + while(1) + { + int flag=1; + ve=getch(); + if(ve=='=') + { + if(V<=0) + flag=0; + else + V-=50; + m++; + if(V<=350 && flag==1) + show2(m); + } + if(ve=='-') + { + if(V>=350) + flag=0; + else + V+=50; + m--; + if(V<=350 && flag==1) + show2(m); + } + if(ve==13) + { + system("cls"); + show(a); + break; + } + } + } +} + +void gameover() //Ϸ +{ + //system("cls"); + gotoxy(50, 12); + printf("GAME OVER!"); + gotoxy(50, 13); + printf("ĵ÷֣%d", score); + gotoxy(49, 15); + printf("sһ"); + gotoxy(49, 17); + printf("Ϸ"); +}