C++ 黑框框小游戏(2)—— 贪吃蛇

简单的贪吃蛇游戏,有高分榜,有无边界两种模式,可保存游戏进度。
用到高分榜.txt,数据.txt,游戏保存.txt 三个文件保存数据。


程序代码:

//高分榜,无墙模式,障碍模式,可保存; 
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<conio.h>
#include<cstring>
#include<fstream>
#define N 25
using namespace std;
char Map[N][N]; 
struct Position{    //坐标结构体 
    int x,y;
}; 
Position snake[4000000],food,next;  //蛇身,食物,蛇头下一步   的坐标 
int head,tail,lenght,speed,grade;   //蛇头,蛇尾的下标  蛇的长度,速度,游戏级别 
char dirction,dirbef;               //方向,上一个方向 
int score=0;                        //分数 
int order,wall,order_type;          //命令,是否有墙 
int rank[5]={0};                    //高分榜 
    char first[20]={"1."};
    char second[20]={"2."};
    char thirst[20]={"3."};
    char forth[20]={"4."};
    char fifth[20]={"5."};
void SetFood(){//设置食物 

    srand((unsigned)time(0));
    while(!(Map[food.y][food.x]==' ')){//随机设置食物(不在蛇身上) 

        food.x=rand()%20 + 1;
        food.y=rand()%20 + 1;
    }
    Map[food.y][food.x]='*';
}
void ShowRank(){                        //显示界面 
    system("cls");                      //刷新 
    cout << "\n\n\t\t\t\t■■■■     ■■■   ■     ■  ■    ■\n";  
    cout << "\t\t\t\t■      ■  ■    ■  ■■   ■  ■   ■ \n";  
    cout << "\t\t\t\t■      ■  ■    ■  ■ ■  ■  ■  ■  \n";  
    cout << "\t\t\t\t■■■■    ■■■■  ■  ■ ■  ■■   \n";  
    cout << "\t\t\t\t■  ■      ■    ■  ■   ■■  ■  ■  \n";  
    cout << "\t\t\t\t■   ■     ■    ■  ■     ■  ■   ■ \n";  
    cout << "\t\t\t\t■    ■■  ■    ■  ■     ■  ■    ■\n";   
    cout<<"\n\t\t\t\t\t\t "<<first<<rank[0];
    cout<<"\n\t\t\t\t\t\t "<<second<<rank[1];
    cout<<"\n\t\t\t\t\t\t "<<thirst<<rank[2];
    cout<<"\n\t\t\t\t\t\t "<<forth<<rank[3];
    cout<<"\n\t\t\t\t\t\t "<<fifth<<rank[4];
}
void InitRank(){                        //修改高分榜("高分榜".txt) 

    int i=6,j;
    if(rank[0]<score) i=0;
    for(j=0;j<5;j++){                   //寻找插入位置 
        if(rank[j]==0&&rank[j-1]>score){//高分榜未满时 
            i=j;    
            break;
        }

    }   
    for(j=4;j>=0;j--){
        if(rank[j]>=score&&rank!=0){//高分榜满时 
            i=j+1;  
            break;
        }

    }   

    if(i<5) cout<<"\n\n\t\t\t\t恭喜进入高分榜!\n\n\n";
    for(j=4;j>i;j--){
        rank[j]=rank[j-1];
    }
    rank[i]=score;
    ofstream in;
    in.open("高分榜.txt");
    if(!in){
        cout<<"error";
    }
    //保存到文件 
    in<<rank[0]<<' ';
    in<<rank[1]<<' ';
    in<<rank[2]<<' ';
    in<<rank[3]<<' ';
    in<<rank[4]<<' ';
}
void Save(){                        //保存游戏进度("数据".txt) ("游戏保存".txt) 

    ofstream is("数据.txt");

    is<<head<<' ';
    is<<tail<<' ';
    for(int i=tail;i<=head;i++){
        is<<snake[i].x<<' ';
        is<<snake[i].y<<' ';
    }
    is<<speed<<' ';
    is<<lenght<<' ';
    is<<score<<' ';
    is<<wall<<' ';
    is<<food.x<<' ';
    is<<food.y<<' '; 
    is<<dirbef<<' ';
    ofstream isave("游戏保存.txt");
    for(int i=0;i<N;i++){           //保存地图 
        for(int j=0;j<N;j++){
            isave<<Map[i][j];
        }
    }

}

void Hello(){                       //欢迎界面 
    cout << "\n\n\t\t\t\t■■■    ■     ■  ■■■  ■    ■ ■■■■\n";  
    cout << "\t\t\t\t■    ■  ■■   ■ ■    ■ ■   ■  ■\n";  
    cout << "\t\t\t\t■        ■ ■  ■ ■    ■ ■  ■   ■\n";  
    cout << "\t\t\t\t ■■■   ■  ■ ■ ■■■■ ■■     ■■■\n";  
    cout << "\t\t\t\t      ■  ■   ■■ ■    ■ ■  ■   ■\n";  
    cout << "\t\t\t\t■    ■  ■     ■ ■    ■ ■   ■  ■\n";  
    cout << "\t\t\t\t ■■■   ■     ■ ■    ■ ■    ■ ■■■■\n";  
    cout << "\n\n\n\t\t\t\t\t\t 1.新游戏\n";
    cout << "\n\t\t\t\t\t\t 2.继续\n";
    cout << "\n\t\t\t\t\t\t 3.高分榜\n";
    cout << "\n\t\t\t\t\t\t 4.退出\n";
}

void SetMap(){                      //初始化地图 
    for(int i=0;i<N;i++){           //初始化地图 
        for(int j=0;j<N;j++){
            Map[i][j]=' '; 
        }
    }
    for(int k=0;k<N;k++){           //设置边界 
        Map[0][k]=Map[N-1][k]=Map[k][0]=Map[k][N-1]='+';
    }
    ifstream fout("高分榜.txt");     //将高分榜载入缓存 
    for(int k=0;k<5;k++) fout>>rank[k];
    SetFood();
    Hello();
    head=2;
    tail=0;
    dirction='d';                   //默认初始方向向右 
    dirbef='d';
    snake[2].x=3;                   //初始化蛇身 
    snake[2].y=1;
    snake[0].x=1;
    snake[0].y=1;
    snake[1].x=2;
    snake[1].y=1;
    Map[1][3]='O';
    Map[1][2]='O';
    Map[1][1]='O';
    lenght=3;
    speed=500;
    score=0;
    grade=0;
    cin>>order;
    if(order==1){
        system("cls"); 
        cout << "\n\n\t\t\t\t■■■    ■     ■  ■■■  ■    ■ ■■■■\n";  
        cout << "\t\t\t\t■    ■  ■■   ■ ■    ■ ■   ■  ■\n";  
        cout << "\t\t\t\t■        ■ ■  ■ ■    ■ ■  ■   ■\n";  
        cout << "\t\t\t\t ■■■   ■  ■ ■ ■■■■ ■■     ■■■\n";  
        cout << "\t\t\t\t      ■  ■   ■■ ■    ■ ■  ■   ■\n";  
        cout << "\t\t\t\t■    ■  ■     ■ ■    ■ ■   ■  ■\n";  
        cout << "\t\t\t\t ■■■   ■     ■ ■    ■ ■    ■ ■■■■\n";  
        cout<<"\n\n\n\t\t\t\t\t\t 1.普通模式\n";
        cout<<"\n\t\t\t\t\t\t 2.无墙模式\n";

        cin>>order_type;
        if(order_type==1) wall=1;
        else wall=0;
    }
    if(order==2){                   //载入保存数据 

        ifstream os("数据.txt");

        os>>head;
        os>>tail;
        for(int i=tail;i<=head;i++){
            os>>snake[i].x;
            os>>snake[i].y;
        }

        os>>speed;
        os>>lenght;
        os>>score;
        os>>wall;
        os>>food.x;
        os>>food.y; 
        os>>dirction;
        ifstream osave("游戏保存.txt");
        osave>>noskipws;
        for(int q=0;q<N;q++){
            for(int w=0;w<N;w++){
                osave>>Map[q][w];
            }
        }

    }
    if(order==4) exit(-1);
    if(order!=3){
        for(int time=3;time>0;time--) {
            system("cls");
            cout<<"\n\t\t\t\t游戏即将开始:"<<time;
            double start=(double)clock()/CLOCKS_PER_SEC;
            while((double)clock()/CLOCKS_PER_SEC-start<=1);
        }
    }


} 

void ShowMap(){                     //显示地图 
    system("cls");
    int i,j;

    for(i=0;i<N;i++){
        for(j=0;j<N;j++){
            cout<<Map[i][j]<<' ';   
        }
        if(i==2) cout<<"\t\tScore="<<score;
        if(i==4) cout<<"\t\tLenght="<<lenght;
        if(i==6) cout<<"\t\tSpeed="<<600-speed;
        if(i==8) cout<<"\t\t 按P保存";
        cout<<endl;
    }
}

bool Gameover(){                    //判断游戏结束 
    if(dirction=='p'){              //保存游戏 
        system("cls"); 
        cout<<"\n\n\t\t\t\t已保存\n\n\n";
        return 0;
    }

    if(wall){                       //有墙模式 
        if(Map[next.y][next.x] == 'O' ||Map[next.y][next.x]=='+') {
            Map[next.y][next.x] = '@' ;
            Map[snake[head].y][snake[head].x]='O';
            Map[snake[tail].y][snake[tail].x]=' ';
            ShowMap();
            system("cls");
            cout << "\t\t ■■■■    ■■■  ■        ■ ■■■■      ■■■■ ■    ■ ■■■■ ■■■■  \n";  
            cout << "\t\t■          ■    ■ ■■    ■■ ■            ■    ■ ■    ■ ■       ■     ■  \n";  
            cout << "\t\t■          ■    ■ ■ ■  ■ ■ ■            ■    ■ ■    ■ ■       ■     ■  \n";  
            cout << "\t\t■   ■■■ ■■■■ ■  ■■  ■ ■■■        ■    ■  ■  ■  ■■■   ■■■■   \n";  
            cout << "\t\t■       ■ ■    ■ ■   ■   ■ ■            ■    ■  ■  ■  ■       ■  ■     \n";  
            cout << "\t\t■       ■ ■    ■ ■   ■   ■ ■            ■    ■   ■■   ■       ■   ■    \n";  
            cout << "\t\t ■■■■   ■    ■ ■        ■ ■■■■      ■■■■    ■    ■■■■ ■    ■■  \n";  
            return 0;
        }
    }
    else{                           //无墙模式 
        if(Map[next.y][next.x] == 'O') {
            Map[next.y][next.x] = '@' ;
            Map[snake[head].y][snake[head].x]='O';
            Map[snake[tail].y][snake[tail].x]=' ';
            ShowMap();
            system("cls");
            cout << "\n\n\t\t ■■■■    ■■■  ■■      ■■ ■■■■      ■■■■ ■    ■ ■■■■ ■■■■  \n";  
            cout << "\t\t■          ■    ■ ■ ■    ■ ■ ■            ■    ■ ■    ■ ■       ■     ■  \n";  
            cout << "\t\t■          ■    ■ ■ ■    ■ ■ ■            ■    ■ ■    ■ ■       ■     ■  \n";  
            cout << "\t\t■   ■■■ ■■■■ ■  ■  ■  ■ ■■■        ■    ■  ■  ■  ■■■   ■■■■   \n";  
            cout << "\t\t■       ■ ■    ■ ■  ■  ■  ■ ■            ■    ■  ■  ■  ■       ■  ■     \n";  
            cout << "\t\t■       ■ ■    ■ ■   ■■   ■ ■            ■    ■   ■■   ■       ■   ■    \n";  
            cout << "\t\t ■■■■   ■    ■ ■    ■    ■ ■■■■      ■■■■    ■    ■■■■ ■    ■■  \n";  
            return 0;
        }
    }

    return 1;
}

int SnakeMove(){//贪吃蛇的移动 
    if(score%100==0&&score!=0) speed=500-score;      //等级(改变速度) 

    bool timeover = true;
    double start = (double)clock() / CLOCKS_PER_SEC;   //得到程序目前为止运行的时间    

    while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= speed / 1000.0)) && !_kbhit());//自动经过1秒或者等待1秒内的键盘输入
    //键盘输入
    dirbef=dirction;
    if (timeover)
    {
        dirction=_getch();    //获取方向
    }
    if(dirction!='w'&&dirction!='a'&&dirction!='s'&&dirction!='d'||((dirction=='w'&&dirbef=='s')||(dirction=='a'&&dirbef=='d')||(dirction=='s'&&dirbef=='w')||(dirction=='d'&&dirbef=='a')))
        dirction=dirbef;
        switch(dirction){
            case 'w':
                next.y = snake[head].y-1;
                next.x = snake[head].x;

                break;
            case 's':
                next.y = snake[head].y+1;
                next.x = snake[head].x;

                break;
            case 'a':
                next.y = snake[head].y;
                next.x = snake[head].x-1;

                break;
            case 'd':
                next.y = snake[head].y;
                next.x = snake[head].x+1;

                break;
            case 'p':
                Save();
                Gameover();


        }
        if(!wall) {
            if(next.y==0) next.y=N-2;
            if(next.y==N-1) next.y=1;
            if(next.x==0) next.x=N-2;
            if(next.x==N-1) next.x=1;

        }

}

void Eat(){//判断贪吃蛇有没有吃到食物
    if(next.x==food.x&&next.y==food.y){//吃到 
        score+=10;
        Map[snake[head].y][snake[head].x]='O';
        head++;
        snake[head].x=next.x;
        snake[head].y=next.y;
        Map[snake[head].y][snake[head].x]='@';
        lenght++;
        SetFood();
        ShowMap();
     }
     else{//没吃到 
        Map[snake[tail].y][snake[tail].x]=' ';
        tail++;
        Map[snake[head].y][snake[head].x]='O';
        head++;
        snake[head].x=next.x;
        snake[head].y=next.y;
        Map[snake[head].y][snake[head].x]='@';
        ShowMap();
     }
}

int main(){

    while(1){
        SetMap();
        if(order==3){
            ShowRank();
        }
        else{
            while(1){
                SnakeMove();
                if(!Gameover()) {
                    if(dirction!='p') InitRank();
                    break;
                }
                Eat();
            }
        }
        cout<<"\n....................按任意键继续\n";
        getchar();
        getchar();
        system("cls");
        order=0;
        dirction=dirbef;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值