c++贪吃蛇改进版

上次的贪吃蛇的基础上改变了食物的颜色,增加了读取上次记录,本来想写一个读取最高纪录的,没写出来,太菜,,,下面是代码
/**红色豆,可以读写上次记录,最大记录还是写不进去*/
#include <iostream>
#include<windows.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include <cstdlib>
#include <cstdio>
#include<fstream>
using namespace std;
/*
#define w 1//上
#define a 3//左
#define s 2//下
#define d 4//右*/

/**定位光标*/
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void locate(int x,int y){
    coord.X=y;
    coord.Y=x;
    SetConsoleCursorPosition(hout,coord);
}

/**隐藏光标*/
void hide(){
    CONSOLE_CURSOR_INFO cursor_info = {1,0};
    SetConsoleCursorInfo(hout,&cursor_info);
}

/**生成随机数*/
double random(double start,double end){
    return start+(end-start)*rand()/(RAND_MAX+1.0);

}

/**地图的长宽,蛇的坐标,长度,方向,食物的位置*/
int m,n;
struct node{
    int x;
    int y;
}snake[1000];
int snake_length,dir;
node food;
int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

/**输出墙*/
void print_wall(int n,int m){
    cout<<" ";
    for(int i=0;i<n;i++){
        cout<<"-";
    }
    cout<<" "<<endl;
    for(int j=0;j<m;j++){
        cout<<"|";
        for(int k=0;k<n;k++){
            cout<<" ";
        }
        cout<<"|"<<endl;
    }
    cout<<" ";
    for(int i=0;i<n;i++){
        cout<<"-";
    }
}

/**首次输出蛇*/
void print_snake(){
     SetConsoleTextAttribute(h,
                            FOREGROUND_RED |
                            FOREGROUND_GREEN |
                            FOREGROUND_BLUE);
    locate(snake[0].x,snake[0].y);
    cout<<"@";
    for(int i=1;i<=snake_length-1;i++){
            locate(snake[i].x,snake[i].y);
        cout<<"*";
    }
}

/**判断自撞以及撞墙*/
bool is_correct(){
    if(snake[0].x==0||snake[0].y==0||snake[0].x==21||snake[0].y==61){
        return false;
    }
    for(int i=1;i<=snake_length-1;i++){
        if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y){
            return false;
        }
    }
    return true;
}

/**随机生成并输出食物*/
bool print_food(){
    //WORD wOldColorAttrs;//Word是16位短整数,无符号短整型,0到65535之间的整数
    //CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

    // Save the current color
    //GetConsoleScreenBufferInfo(h, &csbiInfo);//用于检索指定的控制台屏幕缓冲区的信息,调用成功返回值非0,调用不成功返回0;
    //wOldColorAttrs = csbiInfo.wAttributes;
    srand((unsigned)time(0));
    bool e;
    while(1){
            e=true;
        int i=(int)random(0,20)+1,j=(int)random(0,60)+1;
        food.x=i;food.y=j;
        for(int k=0;k<=snake_length-1;k++){
            if(food.x==snake[k].x&&food.y==snake[k].y){
                e= false;
                break;
            }
        }
        if(e){
            break;
        }
    }
    locate(food.x,food.y);
    //SetConsoleTextAttribute(h, wOldColorAttrs);
    //cout<<"$";*/
        SetConsoleTextAttribute(h,
                            FOREGROUND_RED |FOREGROUND_INTENSITY); //设置控制台背景颜色和字体颜色的函数,
   cout<<"$";
    return true;
}

void print_smileface(){
  SetConsoleTextAttribute(h,
                            FOREGROUND_RED |
                            FOREGROUND_GREEN |
                            FOREGROUND_BLUE);
    locate(8,64);
     cout<<"  ********  "<<endl;
     locate(9,64);
    cout<<"*          *"<<endl;
    locate(10,64);
    cout<<"*  ^    ^  *"<<endl;
    locate(11,64);
    cout<<"*          *"<<endl;
    locate(12,64);
    cout<<"*    ˇ    *"<<endl;
    locate(13,64);
    cout<<"  ********  "<<endl;
    locate(14,66);
    cout<<"吃到了~"<<endl;
}

void print_face(){
     SetConsoleTextAttribute(h,
                            FOREGROUND_RED |
                            FOREGROUND_GREEN |
                            FOREGROUND_BLUE);
  locate(8,64);
     cout<<"  ********  "<<endl;
     locate(9,64);
    cout<<"*          *"<<endl;
    locate(10,64);
    cout<<"*  *    *  *"<<endl;
    locate(11,64);
    cout<<"*          *"<<endl;
    locate(12,64);
    cout<<"*    *     *"<<endl;
    locate(13,64);
    cout<<"  ********  "<<endl;
    locate(14,66);

    cout<<"小心哦~"<<endl;
}

int readmaxlength(){
    ifstream inFile;
    inFile.open("c:\\snake.txt");
    if(inFile.is_open()){
        int maxlength;
        inFile>>maxlength;
        return maxlength;
    }
}

/**写上次记录*///http://blog.csdn.net/kingstar158/article/details/6859379/
void writeFile(){
    ofstream outFile;
    outFile.open("c:\\snake.txt");
    int lastlength;
    lastlength=snake_length;
    int maxlength;
    outFile << lastlength <<endl;
    int length=readmaxlength();
    if(length<snake_length){
            maxlength=snake_length;
        outFile<<maxlength;
    }
   else{
    maxlength=length;
    outFile<<maxlength;
   }
    outFile.close();
}

/**写最大记录*/
/*void writemaxFile(){
    ifstream inFile;
    inFile.open("c:\\snake1.txt");
    if(inFile.is_open()){
        int maxlength;
        inFile>>maxlength;
        int length=maxlength;
        if(snake_length>length){
            ofstream outFile;
            outFile.open("c:\\snake1.txt");
              outFile << length << endl;
            locate(17,64);
            cout<<"最高记录:"<<maxlength<<endl;
        }
    }
}*/

/**读取纪录*/
void readFile(){
    ifstream inFile;
    inFile.open("c:\\snake.txt");
    if(inFile.is_open()){
        int lastlength;
        inFile >> lastlength;
        int maxlength;
        inFile>>maxlength;
        locate(16,64);
        cout << "上次纪录:" << lastlength << endl;
        locate(17,64);
        cout<<"最高纪录:"<<maxlength<<endl;
    }
    else{
        locate(16,64);
        cout << "上次纪录:" << 0 << endl;
        locate(17,64);
        cout<<"最高纪录:"<<snake_length<<endl;
    }
}

/**前进*/
bool go_ahead(){
     SetConsoleTextAttribute(h,
                            FOREGROUND_RED |
                            FOREGROUND_GREEN |
                            FOREGROUND_BLUE);
    node temp;
    temp = snake[snake_length-1];
    bool e = false;
    for(int i=snake_length-1;i>=1;i--){
        snake[i]=snake[i-1];
    }
    snake[0].x=snake[0].x+direct[dir][0];
    snake[0].y=snake[0].y+direct[dir][1];
    locate(snake[1].x,snake[1].y);
    cout<<"*";
    /*如果吃到食物了*/
    if(snake[0].x==food.x&&snake[0].y==food.y){
        snake_length++;
        e = true;
        snake[snake_length-1]=temp;
    }
    /*输出此时蛇的状态*/
    if(!e){
        locate(temp.x,temp.y);
        cout<<" ";
        print_face();
        readFile();
    }
    else{
        print_food();
        print_smileface();
        readFile();
    }
    locate(snake[0].x,snake[0].y);
    cout<<"@";
    /*如果自撞或者撞墙了*/
    if(!is_correct()){
        system("cls");
        cout<<"你输了"<<endl;
        cout<<"最终长度是:"<<snake_length;
        return false;
    }
    return true;
}


void print_logo(){
   // SetConsoleTextAttribute(h,FOREGROUND_RED|FOREGROUND_INTENSITY);
    cout<<"**********   **       **        *        **           **********"<<endl;
    cout<<"**********   ***      **       ***       **      **   **********"<<endl;
    cout<<"**           ****     **      ** **      **    **     **        "<<endl;
    cout<<"**           ** **    **     **   **     **  **       **        "<<endl;
    cout<<"**********   **  **   **    **     **    ****         **********"<<endl;
    cout<<"**********   **   **  **    *********    ***          **********"<<endl;
    cout<<"        **   **    ** **    **     **    ** **        **        "<<endl;
    cout<<"        **   **     ****    **     **    **   **      **        "<<endl;
    cout<<"**********   **      ***   **       **   **     **    **********"<<endl;
    cout<<"**********   **       **  **         **  **       **  **********"<<endl;
    for(int i=1;i<65;i++){
        cout<<"-";
    }
cout<<endl;
//SetConsoleTextAttribute(h,FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    cout<<" * ** * "<<endl;
    cout<<"* .  . *"<<endl;
    cout<<"*      *"<<endl;
    cout<<"*   -  *"<<endl;
    cout<<" *    *     * *"<<endl;
    cout<<"   * *    * * * *        *"<<endl;
    cout<<"    * * * *    * * * * ** "<<endl;
    cout<<"     * * *       * * * *"<<endl;
    cout<<endl;

    locate(12,30);
    cout<<"游戏规则:按上下左右键控制蛇的移动"<<endl;
    locate(13,40);
    cout<<"每吃掉一个食物身体长长一节"<<endl;
    locate(14,40);
    cout<<"撞到墙或者自己的身体则游戏失败"<<endl;
    locate(15,40);
    cout<<"我们的目标是变大变长!"<<endl;
    locate(16,40);
    cout<<"请输入难度(1~100)开始游戏吧!"<<endl;
    cout<<endl;
}

/**主函数**/
int main(){

    print_logo();
    int hard;
    locate(17,40);
    cin>>hard;
    if(hard<1||hard>100){
        cout<<"游戏难度不得小于1不得大于100,请重新开始游戏"<<endl;
        system("pause");
        return 0;
    }
    snake_length=5;
    for(int i=0;i<5;i++){
        snake[i].x=1;
        snake[i].y=5-i;
    }
    dir=3;
    system("cls");
    hide();
    print_wall(60,20);
    print_food();
    print_snake();
    locate(3,62);
    cout<<"当前长度:";

    //char a,s,w,d;

    clock_t q,t;
    char ch;
    double hard_len;

    /**开始游戏*/
    while(1){
        hard_len=(double)snake_length/(double)(60*20);
        q=clock();
        while(1){
            t=clock();
            if(t-q>=(int)(400-30*hard)*(1-sqrt(hard_len))){
                break;
            }
        }
        /**键入上下左右*/
        if(kbhit()){
            ch=getch();
            if(ch==-32){
                ch=getch();
                switch(ch){
                case 72://上
                    if(dir==2||dir==3){
                        dir=0;
                    }
                    break;
                case 80:
                    if(dir==2||dir==3){
                        dir=1;
                    }
                    break;
                case 75:
                    if(dir==0||dir==1){
                        dir=2;
                    }
                    break;
                case 77:
                    if(dir==0||dir==1){
                        dir=3;
                    }
                    break;

              /*  case 3://左a
                    if(dir==0||dir==1){
                        dir=2;
                    }
                    break;
                case 1://上w
                        if(dir==2||dir==3){
                        dir=0;
                        }
                    break;

                case 2://下s
                     if(dir==2||dir==3){
                        dir=1;
                    }
                    break;
                case 4://右d
                    if(dir==0||dir==1){
                        dir=3;
                    }
                    break;*/
                }
            }
        }
        if(!go_ahead()){
            writeFile();
            // writemaxFile();
            break;
        }

        locate (3,73);
        cout<<snake_length<<endl;

    }
    system("pause");
    return 0;
}

下面是运行结果


  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值