首页 > 编程 > C++ > 正文

C++贪吃蛇200行以内实现完整功能

2019-11-10 22:55:07
字体:
来源:转载
供稿:网友
#include <iostream>#include<cstdio>#include<cstdlib>#include<ctime>#include<conio.h>using namespace std;#define Size 22#define Max_snake_length 400typedef struct{    int x,y;}Point;char map[Size][Size];Point snake[Max_snake_length],food,Next;     //定义蛇,食物,下一步蛇头的位置int head,tail;int grade,length,autotime;   //游戏等级,蛇长度,自动前进所需时间char direction;inline void Update(char map[][Size],int grade,int length,int autotime){   //inline定义内联函数,节省程序运行时   //的内存调度开销   //刷新地图    system("cls");    int i,j;    PRintf("/n");    for(i=0;i<Size;i++){        printf("/n");        for(j=0;j<Size;j++)            printf("%c ",map[i][j]);        if(i==0)            printf("/t等级为: %d",grade);        if(i==2)            printf("/t长度为: %d",length);        if(i==6)            printf("/t自动前进实践");        if(i==8)            printf("/t间隔为: %d ms",autotime);        printf("/n");    }}//Welcome界面inline void hello(){    puts("/n/n/n/t/t/t贪吃蛇即将开始!");    double start;    for(int i=3;i>=0;i--){        start=(double)clock()/CLOCKS_PER_SEC;        while((double)clock()/CLOCKS_PER_SEC-start<=1);  //经过1秒以后        if(i>0){            system("cls");            printf("/n/n/n/t/t/t进入倒计时: %d/n",i);   //倒计时        }        else{            Update(map,grade,length,autotime);        }    }}//随机生成食物位置inline void create_food(){    srand(int(time(0)));     //开始掉用种子函数,为后面生成随机数准备    do{        food.x=rand()%20+1;        food.y=rand()%20+1;    }while(map[food.x][food.y]!=' ');    map[food.x][food.y]='!';}//初始化inline void init(){    int i,j;    for(i=1;i<=20;i++){        for(j=1;j<=20;j++){            map[i][j]=' ';        }    }    for(i=0;i<=21;i++){        map[0][i]=map[21][i]=map[i][0]=map[i][21]='*';    //边界用星号表示    }    map[1][1]=map[1][2]='O';     //蛇出生点处,蛇身的位置    map[1][3]='@';    head=2;         //头部在snake[]蛇数组中的下标    tail=0;           //数组下标    snake[head].x=1;   //出生点蛇的头和尾的X,Y坐标    snake[head].y=3;    snake[tail].x=1;    snake[tail].y=1;    snake[1].x=1;      //出生点蛇身的X,Y坐标    snake[1].y=2;    create_food();    grade=1;length=3;autotime=500;    direction=77;        //初始时方向}//预前进inline int GO(){    bool timeover=true;    double start=(double)clock()/CLOCKS_PER_SEC;      //程序到目前为止运行的时间    //自动经过1秒,或者等待1秒内的额键盘输入    while((timeover=((double)clock()/CLOCKS_PER_SEC-start<=autotime/1000.0))&&!_kbhit());    if(timeover){   //键盘输入        _getch();        direction=_getch();   //获取方向    }    switch(direction){        case 72:   //上方向键,ASCii 码为72            Next.x=snake[head].x-1;            Next.y=snake[head].y;            break;        case 80:   //下方向键,ASCii 码为80            Next.x=snake[head].x+1;            Next.y=snake[head].y;            break;        case 75:   //左方向键,ASCii 码为75            Next.x=snake[head].x;            Next.y=snake[head].y-1;            break;        case 77:   //左方向键,ASCii 码为77            Next.x=snake[head].x;            Next.y=snake[head].y+1;            break;        default:            puts("/tGame over!");            return 0;    }    //触碰边界    if(Next.x==0||Next.x==21||Next.y==0||Next.y==21){        puts("/tGame over!");        return 0;    }    //吃到自己    if(map[Next.x][Next.y]!=' '&&!(Next.x==food.x&&Next.y==food.y)){        puts("/tGame over!");        return 0;    }    if(length==400){        puts("/tGood game!");        return 0;    }    return 1;}//吃到食物inline void eating_food(){    length++;    //长度加1    int _grade=length/10+1;    if(_grade!=grade){        grade=_grade;        if(autotime>=100){            autotime=550-grade*50;     //难度加1,降落速度加快50ms        }    }    map[Next.x][Next.y]='@';       //蛇头位置移动到下一格    map[snake[head].x][snake[head].y]='O';   //原蛇头处变蛇身    head=(head+1)%400;             //蛇头下标加1    snake[head].x=Next.x;          //蛇头下标变化    snake[head].y=Next.y;    create_food();    Update(map,grade,length,autotime);   //刷新地图}inline void eating_fail(){    map[snake[tail].x][snake[tail].y]=' ';   //蛇尾原来的位置变成空格    tail=(tail+1)%400;             //蛇尾下标加1    map[Next.x][Next.y]='@';       //蛇头位置变化    map[snake[head].x][snake[head].y]='O';    //原蛇头位置变化为蛇身    head=(head+1)%400;             //蛇头下标加1    snake[head].x=Next.x;          //蛇头下标变化    snake[head].y=Next.y;    Update(map,grade,length,autotime);   //刷新地图}int main(){    init();    hello();    while(1){        if(GO()){            if(Next.x==food.x&&Next.y==food.y){                eating_food();            }else{                eating_fail();            }        }else{            return 0;        }    }    return 0;

}

运行效果:

       


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选