赞
踩
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<windows.h>
- #include<conio.h>
- #define WIDE 60
- #define HIGH 20
-
- //1 设置地图范围(边界)宽 高
- //2 初始化蛇 初始化食物
- //3将蛇显示在屏幕上
- //4 蛇的移动(通过wasd按键控制蛇的移动方向)
- /*
- a>蛇碰到墙
- b>蛇碰到身体
- c>蛇碰到障碍物
- d>蛇碰到食物(蛇身体增长一节,原食物消失,生成新的食物,增加分数)
-
- */
- //5 蛇的死亡 积分
- typedef struct boby{
- int x;
- int y;
- }BODY;
- typedef struct snake{
- BODY list[WIDE*HIGH];//身体
- int size;//蛇的身体大小
- BODY food;
- COORD coord;
- int dx;//蛇x轴移动的方向
- int dy;//蛇y轴移动的方向
- int score;
- BODY tail;
- }SNAKE;
- void bianjie()
- {
- int i,j;
- for(i=0;i<HIGH;i++)
- {
- for(j=0;j<WIDE;j++)
- {
- printf("#");
- }
- printf("\n");
- }
- }
- void init_snake(SNAKE *snake){
- snake->list[0].x=WIDE/2;
- snake->list[0].y=HIGH/2;
- snake->list[1].x=(WIDE/2)-1;
- snake->list[1].y=HIGH/2;
- snake->size=2;
- init_food(snake);
- snake->dx=1;
- snake->dy=0;
- snake->score=0;
- }
- void init_food(SNAKE *snake){
- srand(time(NULL));//设置随机数种子
- snake->food.x=rand() % WIDE;
- snake->food.y=rand() % HIGH;
- }
- void show_ui(SNAKE *snake){
- //显示蛇
- int i;
- for(i=0;i<snake->size;i++){
- snake->coord.X=snake->list[i].x;
- snake->coord.Y=snake->list[i].y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),snake->coord);
- if(0==i){
- printf("@");
- }
- else{
- printf("*");
- }
- }
- //显示食物
- snake->coord.X=snake->food.x;
- snake->coord.Y=snake->food.y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),snake->coord);
- printf("#");
- //将原来尾巴位置显示空格
- snake->coord.X=snake->tail.x;
- snake->coord.Y=snake->tail.y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),snake->coord);
- printf(" ");
- }
- void move_snake(SNAKE *snake){
- int i;
- //记录尾巴坐标;
- snake->tail.x=snake->list[snake->size-1].x;
- snake->tail.y=snake->list[snake->size-1].y;
- //更新蛇的身体坐标,除蛇头
- for(i=snake->size-1;i>0;i--){
- snake->list[i]=snake->list[i-1];
- }
- //更新蛇头
- snake->list[0].x+=snake->dx;
- snake->list[0].y+=snake->dy;
- }
- void control_snake(SNAKE *snake){
- char key=0;
- while(_kbhit())//判断是否按下按键,按下不等于0
- {
- key = _getch();
- }
- switch(key){
- case 'a':
- snake->dx=-1;
- snake->dy=0;break;
- case 's':
- snake->dx=0;
- snake->dy=1;break;
- case 'w':
- snake->dx=0;
- snake->dy=-1;break;
- case 'd':
- snake->dx=1;
- snake->dy=0;break;
- }
- }
- void game_end(SNAKE *snake){
- snake->coord.X=25;
- snake->coord.Y=25;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),snake->coord);
- printf("游戏结束 得分为%d\n",snake->score);
- Sleep(3);
- exit(0);
- }
- void snake_eat_body(SNAKE *snake){
- int i;
- for(i=1;i<snake->size;i++){
- if(snake->list[0].x==snake->list[i].x&&snake->list[0].y==snake->list[i].y){
- game_end(snake);
-
- }
- }
- }
- void snake__eat_food(SNAKE *snake){
- if(snake->list[0].x==snake->food.x&&snake->list[0].y==snake->food.y){
- init_food(snake);
- snake->size++;
- snake->score+=10;
- }
- }
- void start_game(SNAKE *snake){
- while(snake->list[0].x<60&&snake->list[0].x>=0&&snake->list[0].y<20&&snake->list[0].y>=0){
- //控制蛇的更新方向
- control_snake(snake);
- //更新蛇的坐标
- move_snake(snake);
- //蛇的移动
- //system("cls");
- //init_wall();
- show_ui(snake);
- //蛇是否碰到墙
- //蛇是否碰到自己
- snake_eat_body(snake);
- //蛇是否碰到食物-(碰到食物,原食物消失,产生新食物)
- snake__eat_food(snake);
- Sleep(150);//延迟0.5s
- }
- game_end(snake);
- }
- void init_wall(){
- int i,j;
- for(i=0;i<=HIGH;i++){
- for(j=0;j<=WIDE;j++){
- if(i==HIGH||j==WIDE){
- printf("+");
- }
- else{
- printf(" ");
- }
- }
- printf("\n");
- }
- }
- void hide_guangbiao(){
- CONSOLE_CURSOR_INFO cci;
- cci.dwSize=sizeof(cci);
- cci.bVisible=FALSE;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cci);
- }
- int main()
- {
- hide_guangbiao();
- //bianjie();
- init_wall();
- SNAKE *snake=(SNAKE *)malloc(sizeof(SNAKE));
- init_snake(snake); //初始化蛇和食物
- show_ui(snake);//显示信息页面
- start_game(snake);//开始游戏
-
-
- system("pause");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。