|
- #include <stdio.h>
- #include <windows.h>
- #include <time.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- int snake_len=1;//蛇的長度
- int snake_loc[50][2]={31,12};//整條蛇的位置,最長為50
- int snake_head[2]={31,12};//蛇頭位置,初始值為11,12;
- int food[2];//食物位置
- char snake_direction='s';
- int delay=200; //蛇每delay個時間走一步
- int eat_flag=0;//1表示吃了食物,0表示未吃
- int liv_stat=0;//1表示死了,游戲該結束了;0表示還活著
- void gotoxy(int x, int y)//定位光標,x為行坐標,y為列坐標
- {
- COORD pos = {x,y};
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hOut, pos);
- }
- void hidden()//隱藏光標
- {
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cci;
- GetConsoleCursorInfo(hOut,&cci);
- cci.bVisible=0;//賦1為顯示,賦0為隱藏
- SetConsoleCursorInfo(hOut,&cci);
- }
- void init()//初始化
- {
- int i;
- snake_len=1;//蛇的長度
- snake_loc[0][0]=31;//整條蛇的位置
- snake_loc[0][1]=12;
- snake_head[0]=31;//蛇頭位置,初始值為11,12;
- snake_head[1]=12;
- snake_direction='s';
- delay=200;
- eat_flag=0;
- liv_stat=0;
- for(i=1;i<50;i++)
- {
- snake_loc[i][0]=0;//整條蛇的位置
- snake_loc[i][1]=0;
- }
-
- }
- void create_window()//創建窗口
- {
- gotoxy(0,0);
- printf("********************************************************************************");
- printf("* * *");
- printf("* * *");
- printf("* * 分數:1 *");
- printf("* * 按鍵說明: *");
- printf("* * 上:w *");
- printf("* * 下:s *");
- printf("* * 左:a *");
- printf("* * 右:d *");
- printf("* * 暫停:空格 *");
- printf("* * 退出:Esc鍵 *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("* * *");
- printf("********************************************************************************");
- }
- void update_score()//更新分數
- {
- gotoxy(73,3);
- printf("%2d",snake_len);
- }
- void create_food()//產生食物的位置
- {
- time_t t;
- srand(time(&t));
- while(1)
- {
- food[0]=rand()%62+1;//生成1~62之間的隨機數,其中random函數生成0~77之間的隨機數
- food[1]=rand()%22+1;//生成1~22之間的隨機數,其中random函數生成0~17之間的隨機數
- if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;
- }
- gotoxy(food[0],food[1]);
- printf("*");
- }
- void direction()
- {
- char keyhit=0,i;
- while(kbhit()!=0)keyhit=getch();
- if( ((keyhit=='a') || (keyhit=='d') || (keyhit=='w') || (keyhit=='s')) && (abs(snake_direction/16-keyhit/16)==1) )snake_direction=keyhit;
- else if(keyhit==' ')
- {
- gotoxy(30,24);
- system("pause");
- gotoxy(30,24);
- for(i=0;i<19;i++)printf(" ");
- }
- else if(keyhit==27)exit(0);
- }
- void state()//判定蛇死沒死
- {
- if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)liv_stat=1;
- }
- void eat()//判定蛇吃沒吃上,并對根據方向對蛇頭位置進行更新
- {
- switch(snake_direction)
- {
- case 'w': snake_head[1]--;break;
- case 's': snake_head[1]++;break;
- case 'a': snake_head[0]--;break;
- case 'd': snake_head[0]++;break;
- }
- if((food[0]==snake_head[0]) && (food[1]==snake_head[1]) )
- {
- eat_flag=1;
- switch(snake_direction)
- {
- case 'w': snake_head[1]--;break;
- case 's': snake_head[1]++;break;
- case 'a': snake_head[0]--;break;
- case 'd': snake_head[0]++;break;
- }
- }
-
- }
- void show_snake()//更新蛇在屏幕中的位置
- {
- gotoxy(snake_head[0],snake_head[1]);
- printf("*");
- gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);
- printf(" ");
- }
- void update_maxtrix()//更新存儲蛇位置的數組
- {
- int i;
- if(eat_flag!=1)
- {
- for(i=snake_len-1;i>0;i--)
- {
- snake_loc[i][0]=snake_loc[i-1][0];
- snake_loc[i][1]=snake_loc[i-1][1];
- }
- }
- else
- {
- snake_len++;
- if(snake_len>3 && delay>100)delay-=30;
- for(i=snake_len-1;i>1;i--)
- {
- snake_loc[i][0]=snake_loc[i-2][0];
- snake_loc[i][1]=snake_loc[i-2][1];
- }
- snake_loc[1][0]=food[0];
- snake_loc[1][1]=food[1];
- eat_flag=0;
- create_food();
- update_score();
- }
- snake_loc[0][0]=snake_head[0];
- snake_loc[0][1]=snake_head[1];
- }
- void main()
- {
- LOOP:
- system("cls");
- init();
- create_window();
- hidden();
- create_food();
- while(1)
- {
- int i;
- Sleep(delay);
- direction();
- eat();
- show_snake();
- update_maxtrix();
- state();
- if(liv_stat==1)
- {
- for(i=1;i<snake_len;i++)
- {
- gotoxy(snake_loc[i][0],snake_loc[i][1]);
- printf(" ");
- }
- gotoxy(food[0],food[1]);
- printf(" ");
- gotoxy(30,12);
- printf("Game over!\n");
- gotoxy(25,13);
- printf("繼續請按y,退出請按n");
- while(1)
- {
- i=getch();
- if(i=='y')goto LOOP;
- else if(i=='n')break;
- }
- break;
- }
- }
- }
復制代碼 |
|