赞
踩
目录
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h> //光标设置的API
- #include <time.h> //食物的随机性
- #include <conio.h> //按键监控
-
- #define Map_Hight 23 //地图高度
- #define Map_Width 90 //地图宽度
- #define Snake_Max_len 1000 //蛇的最大长度
- #define Snake_Begin_Len 3 //蛇的初始长度
- int score=0;
- //食物
- struct
- {
- int x;
- int y;
- }food;
- //蛇
- struct
- {
- int x[Snake_Max_len];
- int y[Snake_Max_len];
- int len; //蛇的长度
- int speed=300; //蛇的移动速度
- }snake;
- char key='w';
- bool changeflag=0; //she de bian hua biao ji(布尔类型)
-
- //1.地图
- void Drawmap();
- //2.食物产生
- void Creatfood();
- //3.按键操作
- void keydown();
- //4.蛇的状态
- bool snakeStatus();
- //5.光标移动
- void gotoxy(int x,int y);
- int main(void)
- {
- //隐藏光标
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cci;
- GetConsoleCursorInfo(hOut, &cci);
- cci.bVisible = FALSE;
- SetConsoleCursorInfo(hOut, &cci);
- Drawmap();
- while(1)
- {
- Sleep(snake.speed);
- Creatfood();
- keydown();
-
- if(!snakeStatus())
- {
- break;
- }
- }
- //system("pause");定住当前屏幕
- gotoxy(Map_Width/2,Map_Hight/2);
- printf("GameOver!");
- system("CLS");
- gotoxy(Map_Width/2,Map_Hight/2+1);
- printf("你的分数是 %d\n",score);
-
-
- }
- void Drawmap()
- {
- //食物
- srand((unsigned int )time(NULL));
- do
- {
- food.x =rand() % (Map_Width-4)+2;
- food.y =rand() % (Map_Hight-2)+1;
- }while(food.x%2!=0);
- gotoxy(food.x, food.y);
- printf("◆");
- //地图
- for(int i=0;i<=Map_Hight;i++)
- {
- gotoxy(0,i);
- printf("■");
- gotoxy(Map_Width,i);
- printf("■");
- }
- for(int j=0;j<=Map_Width;j+=2)
- {
- gotoxy(j,0);
- printf("■");
- gotoxy(j,Map_Hight);
- printf("■");
- }
- //按键说明
- gotoxy(100,4);
- printf("按键说明");
- gotoxy(100,7);
- printf("向上:w W");
- gotoxy(100,10);
- printf("向下:s S");
- gotoxy(100,13);
- printf("向左:a A");
- gotoxy(100,16);
- printf("向右:d D");
- //画蛇
- snake.len=Snake_Begin_Len;
- srand((unsigned)time(NULL));
- do
- {
- snake.x[0]=rand() % (Map_Width/2)+Map_Width/4;
- snake.y[0]=rand() % (Map_Hight/2)+Map_Hight/4;
- }while (snake.x[0]%2!=0);
- gotoxy(snake.x[0],snake.y[0]);
- printf("●");
- for(int i = 1;i <snake.len ;i++)
- {
- snake.x[i]=snake.x[0];
- snake.y[i]=snake.y[i-1]+1;
- gotoxy(snake.x[i] ,snake.y[i]);
- printf("●");
- }
- //积分器
- gotoxy(100,22);
- printf("当前分数:%d",score);
- }
- void Creatfood()
- {
- if(snake.x[0] == food.x&&snake.y[0] == food.y)
- {
- //积分器
- score++;
- gotoxy(100,22);
- printf("当前分数:%d",score);
- srand((unsigned)time(NULL));
- //产生的食物不能在蛇的身上且食物的坐标要是偶数
- while(1)
- {
- bool flag=true;
- food.x=rand() % (Map_Width-4)+2;
- food.y=rand() % (Map_Hight-2)+1;
- for(int k=0;k<snake.len;k++)
- {
- if(snake.x[k]==food.x&&snake.y[k]==food.y)
- {
- flag=false;
- break;
- }
- }
- if(flag&&food.x % 2 == 0)
- break;
- }
- gotoxy(food.x,food.y);
- printf("◆");
- changeflag=true;
- }
- }
- void keydown()
- {
- //无按键处理
- if(_kbhit())
- { //有按键接收
- fflush(stdin);
- key=_getch();
- }
- //擦除最后一节
- if(!changeflag)
- {
- gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
- printf(" ");
- }
- else
- {
- snake.x[snake.len]=snake.x[snake.len-1];
- snake.y[snake.len]=snake.y[snake.len-1];
- snake.len++;
- }
- //后面的蛇身
- for(int i=snake.len-1;i>0;i--)
- {
- snake.x[i]=snake.x[i-1];
- snake.y[i]=snake.y[i-1];
- }
- switch(key)
- {
- case'w':
- case'W':
- snake.y[0]--;
- break;
- case'a':
- case'A':
- snake.x[0]-=2;
- break;
- case's':
- case'S':
- snake.y[0]++;
- break;
- case'd':
- case'D':
- snake.x[0]+=2;
- break;
- }
- gotoxy(snake.x[0],snake.y[0]);
- printf("●");
- changeflag=false;
-
- }
- bool snakeStatus()
- {
- //不能撞墙
- if(snake.x[0]==0||snake.x[0]==Map_Width||snake.y[0]==0||snake.y[0]==Map_Hight )
- return false;
- //不能撞自己
- for(int k=1;k<=snake.len;k++)
- {
- if(snake.x[k]==snake.x[0]&&snake.y[k]==snake.y[0])
- return false;
- }
- return true;
- }
- void gotoxy(int x,int y)
- {
- HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coord; //定义光标结构体
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(handle, coord);//移动光标
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。