貪喫蛇問題+註釋

這篇代碼是我按照別人的代碼進行 註釋編寫重新整理寫出來的
特別感謝那位博主的代碼 是我對貪喫蛇有了深刻的認識

#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <cstring>
#include <cstdio>
#define N 22

using namespace std;
    int gameover;

    int x1,y1;

    long start;
    int x,y;
class snake_position//貪喫蛇的座標
{
   public:
      int x,y;
      snake_position(){};
      void initialize(int &); //座標初始化
};
snake_position position[(N-2)*(N-2)+1];//座標數組 有(n-2)*(n-2)個

void snake_position::initialize(int &j)
{
    x=1;
    y=j;
}
class snake_maps  //棋盤
{
private:
    char s[N][N];  //定義一個N*N的數組
    int grade,length;  //喫掉的數目  當前的長度
    int gamespeed;  //前進時間間隔
    char direction;  //初始方向  一般爲右
    int head,tail;
    int score;
    bool gameauto;
public:
    snake_maps(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
   //初始化
   void initialize();  //初始化函數
   void show_game();   //顯示貪喫蛇信息
   int updata_game();
   void setpoint();
   void getgrade();
   void display();

};
//初始化棋盤採用“#”作爲蛇頭,”*”作爲蛇身和食物 '-''|'作爲牆壁
void snake_maps::initialize()
{
    int i,j;
    for(i=1;i<=N-2;i++)
        for(j=1;j<=N-2;j++)
        s[i][j]=' ';  //將中間空白部分初始化
    for(i=1;i<=3;i++)
        s[1][i]='*';  //製造一條蛇
    s[1][4]='#';
    for(i=0;i<=N-1;i++)
        s[0][i]=s[N-1][i]='-'; //初始化牆壁
    for(i=1;i<=N-2;i++)
        s[i][0]=s[i][N-1]='|';

}

//輸出貪喫蛇的信息
void snake_maps::show_game()
{
    system("cls"); //清屏操作  以便刷新
    int i,j;
    cout<<endl;
    for(i=0;i<N;i++)  //輸出信息
    {
        cout<<'\t';
        for(j=0;j<N;j++)
            cout<<s[i][j]<<' '; //輸出貪喫蛇棋盤
        if(i==2)
            cout<<"\t 等級"<<grade;
        if(i==6)
            cout<<"\t 速度"<<gamespeed;
        if(i==10)
            cout<<"\t 得分"<<score<<"分";
        if(i==14)
            cout<<"\t 暫停:按一下空格";
        if(i==18)
            cout<<"\t 繼續:按兩下空格";
        cout<<endl;
    }

}
void snake_maps::getgrade()//得到等級
{
    cin >>grade;
    while(grade>7||grade<1)
    {
        cout<<"輸入1-7的數字來確定等級,其他輸入的數字無效"<<endl;
        cin>>grade;
    }
    switch(grade)
    {
        case 1:gamespeed=1000;gameauto=0;break;

        case 2:gamespeed=800;gameauto=0;break;

        case 3:gamespeed=600;gameauto=0;break;

        case 4:gamespeed=400;gameauto=0;break;

        case 5:gamespeed=200;gameauto=0;break;

        case 6:gamespeed=100;gameauto=0;break;

        case 7:grade=1,gamespeed=1000;gameauto=1;break;

    }
}

//輸出等級,得分情況以及速度

void snake_maps::display()
{
    cout<<"\n\t\t\t\t等級"<<grade;

    cout<<"\n\n\n\t\t\t\t速度"<<gamespeed;

    cout<<"\n\n\n\t\t\t\t得分"<<score;

}

//產生隨機的食物

void snake_maps::setpoint()
{
    srand(time(0));
    do
    {
        x1=rand()%(N-2)+1;
        y1=rand()%(N-2)+1;

    }while(s[x1][y1]!=' ');

    s[x1][y1]='*';
}
char key;

int snake_maps::updata_game()
{
    gameover = 1;
    key = direction;
    start = clock();
    while((gameover=(clock()-start<=gamespeed))&&!kbhit());
    //如果有鍵按下或時間超過自動前進時間間隔則終止循環
        if(gameover)
        {
            getch();
            key = getch();
        }
        if(key == ' ')
        {
            while(getch()!=' '){};
//這裏實現的是按空格鍵暫停,按空格鍵繼續的功能,但不知爲何原因,
//需要按兩下空格才能繼續。這是個bug。
        }
        else
            direction = key;
        switch(direction)
        {
            case 72: x= position[head].x-1; y= position[head].y;break; // 向上
            case 80: x= position[head].x+1; y= position[head].y;break; // 向下
            case 75: x= position[head].x; y= position[head].y-1;break; // 向左
            case 77: x= position[head].x; y= position[head].y+1; // 向右
        }
        if(!(direction==72||direction==80||direction==75 ||direction==77))
 // 按鍵非方向鍵
            gameover = 0;
        else if(x==0 || x==N-1 ||y==0 || y==N-1)   // 碰到牆壁
            gameover = 0;
        else if(s[x][y]!=' '&&!(x==x1&&y==y1))    // 蛇頭碰到蛇身
            gameover = 0;
        else if(x==x1 && y==y1)
        { // 喫米,長度加1
            length ++;
            if(length>=8 && gameauto)
            {
                length -= 8;
                grade ++;
                if(gamespeed>=200)
                    gamespeed -= 200; // 改變貪喫蛇前進速度
                else
                    gamespeed = 100;
            }
            s[x][y]= '#';  //更新蛇頭
            s[position[head].x][position[head].y] = '*'; //喫米後將原先蛇頭變爲蛇身
            head = (head+1) % ( (N-2)*(N-2) );   //取蛇頭座標
            position[head].x = x;
            position[head].y = y;
            show_game();
            gameover = 1;
            score += grade*1;  //加分
            setpoint();   //產生米
        }
        else
        { // 不喫米
            s[position[tail].x][position[tail].y]=' ';//將蛇尾置空
            tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾座標
            s[position[head].x][position[head].y]='*';  //將蛇頭更爲蛇身
            head= (head+1) % ( (N-2) * (N-2) );
            position[head].x = x;
            position[head].y = y;
            s[position[head].x][position[head].y]='#'; //更新蛇頭
            gameover = 1;
        }
    return gameover;
}


int main()
{
    char ctn='y';
    int nodead;
    cout<<"\n\n\n\n\n\t\t\t 歡迎進入貪喫蛇遊戲!"<<endl;  //歡迎界面
    cout<<"\n\n\n\t\t按任意鍵開始 "<<endl;
    getch();
    while( ctn=='y' )
    {
        system("cls");
        snake_maps snake;
        snake.initialize();//初始化
        cout<<"\n\n請輸入數字選擇遊戲等級:"<<endl;
        cout<<"\n\n\n\t\t\t1.等級一:速度 1000\n\n\n\t\t\t2.等級二:速度 800";
        cout<<"\n\n\n\t\t\t3.等級三:速度 600\n\n\n\t\t\t4.等級四:速度 400";
        cout<<"\n\n\n\t\t\t5.等級五:速度 200\n\n\n\t\t\t6.等級六:速度 100";
        cout<<"\n\n\n\t\t\t7.自動升級模式:速度 100"<<endl;
        snake.getgrade();  //獲得等級
        for(int i=1;i<=4;i++)
        {
            position[i].initialize(i);//初始化座標

        }
        snake.setpoint();//產生第一個食物
        do
        {
            snake.show_game();      //進入遊戲
            nodead=snake.updata_game();
        }while(nodead);
        system("cls");
        cout<<"\n\n\n\t\t\t Gameover!\n\n"<<endl;
        snake.display();//輸出等級和得分情況

        cout<<"\n\n\n\t\t   是否選擇繼續遊戲?輸入y繼續,輸入n退出"<<endl;
        cin>>ctn;
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章