自己的貪喫蛇C++終結版

在這裏感謝賀利堅老師對我這學期的幫助,嘿嘿。

/* 
* 程序的版權和版本聲明部分 
* Copyright (c)2013, 在校學生 
* All rightsreserved. 
* 文件名稱: 貪喫蛇.cpp 
* 作    者:劉旺 
* 完成日期:2014年7月4日 
* 版本號: v1.0 
* 
* 輸入描述: 
* 問題描述:實現貪喫蛇
* 程序輸出: 
* 問題分析: 
*/  

#include <iostream>
#include <vector>
#include <windows.h>
#include <conio.h>
#include <cstdio>
#include <fstream>
#include <ctime>

using namespace std ;

//定義蛇類
class Snack{
      public:
             Snack *next ;  //指向下一個蛇身的指針
             Snack(){}
             Snack(int x1, int y1):x(x1),y(y1){}
             int getX() ;  //獲取x座標
             int getY() ;   //獲取y座標
             int getN() ;   //獲取蛇的長度
             void setN(int n1) ;  //設置蛇長度
             void setX(int x1) ;  //設置y座標
             void setY(int y1) ;  //設置y座標
      private:
             static int n ;    //蛇長度
             int x ;          //x座標
             int y ;           //y座標
};
int Snack::n = 3 ;//初始化捨身

//定義獲取x座標函數
int Snack::getX()
{
    return x ;
}
//定義獲取y座標函數
int Snack::getY()
{
    return y ;
}
//定義獲取蛇身函數
int Snack::getN()
{
    return n ;
}
//定義設置x座標函數
void Snack::setX(int x1)
{
      x = x1 ;
}
//定義設置y座標函數
void Snack::setY(int y1)
{
     y = y1 ;
}
//定義設置蛇身長度函數
void Snack::setN(int n1)
{
    n = n1 ;
}

//定義操控類
class Console{
  public:
        Console():head(NULL),speed(100),style(1),lis(true){a[0]=3;a[1]=3;a[2]=3;}
        void snack_Add() ; //控制蛇的生長
        void snack_body() ;  //控制蛇身
        void gotoxy(Snack *p) ; //控制蛇身的座標
        void gotoxy(int x1, int y1) ;
        void console() ;          //控制輸入的按鍵
        void snack_Run(int m) ;   //控制蛇身移動
        void snack_food() ;       //控制蛇的食物出現
        void save2() ;
        void read2() ;
        void del() ;
        void setLis(bool b){lis=b;}
        void setSpeed(int s){speed=s;}
        int getSpeed(){return speed;}
        void setstyle(int l){style=l;} ;
        ~Console() ;       //釋放動態內存
        int a[3] ;
        void setHead(Snack *he){head=he;}
  private:
       Snack *head ;//蛇頭
       Snack *food ;//蛇的食物
       int speed ;  //速度
       int style ; //樣式
       bool lis ;

};
//定義食物座標函數
void Console::snack_food()
{
     int x ,y ;
     srand(time(NULL)) ;
     x = rand()%45+2 ;
     y = rand()%15+1 ;
     food = new Snack(x,y) ;

}
//定義控制蛇增長函數
void Console::snack_Add()
{
    if(head==NULL)
   {
    Snack *p = new Snack(4,1) ;
    Snack *p1 = new Snack(3,1) ;
    Snack *p2 = new Snack(2,1) ;
    head = p ;
    p->next = p1 ;
    p1->next = p2 ;
    p2->next = NULL ;
   }
   else
  {
   int x = food->getX() ;
   int y = food->getY() ;
   Snack *head1 = new Snack(x,y) ;
   head1->next = head ;
   int n1 = head->getN()+1 ;
   head = head1 ;
   head->setN(n1) ;
   snack_food() ;
 }
}
//定義蛇的運動函數
void Console::snack_Run(int m)
{

       Snack *p=head->next ;
       Snack *p1 ;
       int x1 = head->getX() ;
       int y1 = head->getY() ;
        int n = p->getN() ;
        int j = 2 ;
       while(j<n)
       {
           if((x1==p->getX())&&y1==p->getY())
           {
               exit(0) ;
           }
           p = p->next ;
           j++;
       }
       if((head->getX()==food->getX())&&(head->getY()==food->getY()))
    {
         if(lis){cout << '\a';}
         snack_Add() ;
    }
       p = head ;
       Snack *head1;
       head1 = new Snack(0,0) ;
       *head1 = *head ;
       int x,y ;
         x = p->getX() ;
         y = p->getY() ;
       j = 1 ;
    while(j<=n)
    {
        p = p->next ;
         j++ ;
    }
    switch(m)
    {
       case 1: head1->setY(y-1) ;break ;
       case 2: head1->setY(y+1) ;break ;
       case 3: head1->setX(x-1) ;break ;
       case 4: head1->setX(x+1) ;break ;
    }

      head1->next = head ;
      head = head1 ;
}
//定義蛇的身體函數
void Console::snack_body()
{
    Snack *p = head ;
    int n = p->getN() ;
    int j = 1 ;
    gotoxy(73,5);
    cout << n-3 ;
    while(j<n)
    {
         if(p==head)
         {
             gotoxy(p) ;
             if(style==1){
             cout << '@' ;}
             else{cout << '#';}
         }
         else
        {

            gotoxy(p) ;
            if(style==1){
            cout << '#' ;}
            else{cout << '*';}
        }
        p = p->next ;
        j++;
    }
            gotoxy(p) ;
            cout << ' ' ;
            gotoxy(food) ;
            cout << '$' ;
}
//定義蛇身的座標函數
void Console::gotoxy(Snack *p)
{
     int x,y ;
     x = p->getX() ;
     y = p->getY() ;
     HANDLE hout ;
     COORD coord ;
     if(x>64){cout<<"遊戲結束!     請按Esc退出";save2();exit(0);}
     if(y>24){cout<<"遊戲結束!     請按Esc退出";save2();exit(0);}
     if(x<1){cout<<"遊戲結束!     請按Esc退出";save2();exit(0);}
     if(y<1){cout<<"遊戲結束!     請按Esc退出";save2();exit(0);}
     coord.X= x ;
     coord.Y= y ;
     hout=GetStdHandle(STD_OUTPUT_HANDLE) ;
     SetConsoleCursorPosition(hout,coord) ;
}
void Console::save2(){
     int i = head->getN() ;
      ofstream fout("fen.dat",ios_base::out) ;
     if(i>a[0]){
        a[0]=i ;
     }else if(i>a[1]){
          a[1] = i ;
     }
     else if(i>a[2]){
        a[2] = i ;
     }
    fout.write((char*)&a[0],sizeof(a[0])) ;
    fout.write((char*)&a[1],sizeof(a[1])) ;
    fout.write((char*)&a[2],sizeof(a[2])) ;
}
void Console::read2()
{
      ifstream fin("fen.dat",ios_base::in) ;
      fin.read((char*)&a[0],sizeof(a[0])) ;
      fin.read((char*)&a[1],sizeof(a[1])) ;
      fin.read((char*)&a[2],sizeof(a[2])) ;
      fin.close() ;
}

void Console::gotoxy(int x1, int y1)
{
     HANDLE hout ;
     COORD coord ;
     coord.X= x1 ;
     coord.Y= y1 ;
     hout=GetStdHandle(STD_OUTPUT_HANDLE) ;
     SetConsoleCursorPosition(hout,coord) ;
}
//定義判斷輸入鍵盤的函數
void Console::console()
{
           char i ;
           char j ;
             while(1)
             {
                     i = getch()  ;
                     if((i=='s'&&j=='w')||(i=='w'&&j=='s')||(i=='a'&&j=='d')||(i=='d'&&j=='a'))
                     {
                         i = j ;
                     }
                     switch(i)
                    {
                       case 'w': while(!kbhit()){Sleep(speed);snack_Run(1);snack_body();} break ;
                       case 's': while(!kbhit()){Sleep(speed);snack_Run(2);snack_body();} break ;
                       case 'a': while(!kbhit()){Sleep(speed);snack_Run(3);snack_body();} break ;
                       case 'd': while(!kbhit()){Sleep(speed);snack_Run(4);snack_body();}  break ;
                       case 27 : break ;
                    }
                       j = i ;
                       if(i==27){break;}
              }

}
//定義釋放動態內存的函數
Console::~Console()
{
     int i=1 ;
     int n1 = head->getN() ;
     Snack *p =head,*p2 ;
     for(i=1; i<=n1; i++)
     {
         p2 = p ;
         p = p->next ;
         delete p2 ;
     }
}

void Console::del(){
      int i=1 ;
     int n1 = head->getN() ;
     Snack *p =head,*p2 ;
     for(i=1; i<=n1; i++)
     {
         p2 = p ;
         p = p->next ;
         delete p2 ;
     }
     head = NULL ;
}


class Game{
  public:
      Game():x(0),y(0),s(){s.read2();}
      Game(int x1, int y1):x(x1),y(y1){}
          void cheek() ; //邊框
          void gotoxy(int x1, int y1) ; //座標
          void welcome() ;
          void console() ;
          void console2() ;
          void console3() ;
          void console4() ;
          void console5() ;
          void console6() ;
          void console7() ;
          void col() ;
          void nandu() ;
          void seting() ;
          void listener() ;
          void paihang() ;
          void style() ;
          void welcome1() ;
          void print() ;
  private:
          //Console con ;
          int x ; //x座標
          int y ; //y座標
          Console s ;
};

void Game::print()
{
   cout << "                          ╔═══════════╗                            " ;
   cout << "                          ║操作說明:            ║                            " ;
   cout << "                          ║   1.w,s上下移動      ║                            " ;
   cout << "                          ║   2.回車代表確定     ║                            " ;
   cout << "                          ║   3.ESC代表退出      ║                            " ;
   cout << "                          ║   4.蛇的移動是w,s,a,d║                            " ;
   cout << "                          ╚═══════════╝                            " ;
}

void Game::paihang()
{
   cout << "                          ╔═══════════╗                            " ;
   cout << "                          ║排行榜:              ║                            " ;
   cout << "                          ║       1.             ║                            " ;
   cout << "                          ║       2.             ║                            " ;
   cout << "                          ║       3.             ║                            " ;
   cout << "                          ╚═══════════╝                            " ;
   gotoxy(37,12);
   cout << s.a[0]-3 << "分";
   gotoxy(37,13);
   cout << s.a[1]-3 << "分";
   gotoxy(37,14);
   cout << s.a[2]-3 << "分";
}

void Game::welcome(){
   cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
   cout << "☉    * * * * * *                                               *       *     ☉"  ;
   cout << "☉    *                                                         *     *       ☉"  ;
   cout << "☉    *                                                         *   *         ☉"  ;
   cout << "☉    *              * * * * *          *         * * * * *     * *           ☉"  ;
   cout << "☉    * * * * * *    *       *         * *        *             *             ☉"  ;
   cout << "☉              *    *       *        *   *       *             * *           ☉"  ;
   cout << "☉              *    *       *       * * * *      *             *   *         ☉"  ;
   cout << "☉   	        *    *       *      *       *     *             *     *       ☉" ;
   cout << "☉    * * * * * *    *       *     *         *    * * * * *     *       *     ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                       ╔═════════════╗                       ☉"  ;
   cout << "☉                       ║                          ║                       ☉"  ;
   cout << "☉                       ║       1.開始遊戲         ║                       ☉"  ;
   cout << "☉                       ║       2.排行榜           ║                       ☉"  ;
   cout << "☉                       ║       3.設置             ║                       ☉"  ;
   cout << "☉                       ║                          ║                       ☉"  ;
   cout << "☉                       ╚═════════════╝                       ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
}

void Game::welcome1(){
   cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
   cout << "☉    * * * * * *                                               *       *     ☉"  ;
   Sleep(1000) ;
   cout << "☉    *                                                         *     *       ☉"  ;
   Sleep(1000) ;
   cout << "☉    *                                                         *   *         ☉"  ;
   Sleep(1000) ;
   cout << "☉    *              * * * * *          *         * * * * *     * *           ☉"  ;
   Sleep(1000) ;
   cout << "☉    * * * * * *    *       *         * *        *             *             ☉"  ;
   Sleep(1000) ;
   cout << "☉              *    *       *        *   *       *             * *           ☉"  ;
   Sleep(1000) ;
   cout << "☉              *    *       *       * * * *      *             *   *         ☉"  ;
   Sleep(1000) ;
   cout << "☉   	        *    *       *      *       *     *             *     *       ☉" ;
   Sleep(1000) ;
   cout << "☉    * * * * * *    *       *     *         *    * * * * *     *       *     ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉                       ╔═════════════╗                       ☉"  ;
   cout << "☉                       ║                          ║                       ☉"  ;
   cout << "☉                       ║       1.開始遊戲         ║                       ☉"  ;
   cout << "☉                       ║       2.排行榜           ║                       ☉"  ;
   cout << "☉                       ║       3.設置             ║                       ☉"  ;
   cout << "☉                       ║                          ║                       ☉"  ;
   cout << "☉                       ╚═════════════╝                       ☉"  ;
   cout << "☉                                                                            ☉"  ;
   cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
}

void Game::col() {
     cout << "                          ╔═══════════╗                            " ;
     cout << "                          ║遊戲界面顏色:        ║                            " ;
     cout << "                          ║             1.藍色   ║                            " ;
     cout << "                          ║             2.綠色   ║                            " ;
     cout << "                          ║             3.藍綠   ║                            " ;
     cout << "                          ║             4.紅色   ║                            " ;
     cout << "                          ║             5.紫色   ║                            " ;
     cout << "                          ╚═══════════╝                            " ;
}
void Game::seting(){
   cout << "                          ╔════════════════╗                  " ;
   cout << "                          ║   1.遊戲界面顏色               ║                  " ;
   cout << "                          ║   2.遊戲難度                   ║                  " ;
   cout << "                          ║   3.遊戲聲音                   ║                  " ;
   cout << "                          ║   4.蛇的樣式                   ║                  " ;
   cout << "                          ╚════════════════╝                  " ;
}

void Game::nandu(){
   cout << "                          ╔═══════════╗                            " ;
   cout << "                          ║遊戲難度:            ║                            " ;
   cout << "                          ║         1.簡單       ║                            " ;
   cout << "                          ║         2.一般       ║                            " ;
   cout << "                          ║         3.困難       ║                            " ;
   cout << "                          ╚═══════════╝                            " ;
}

void Game::listener(){
   cout << "                          ╔═══════════╗                            " ;
   cout << "                          ║遊戲聲音:            ║                            " ;
   cout << "                          ║         1.開         ║                            " ;
   cout << "                          ║         2.關         ║                            " ;
   cout << "                          ╚═══════════╝                            " ;
}

void Game::style(){
   cout << "                          ╔═══════════╗                            " ;
   cout << "                          ║蛇的樣式:            ║                            " ;
   cout << "                          ║         1.@#         ║                            " ;
   cout << "                          ║         2.#*         ║                            " ;
   cout << "                          ╚═══════════╝                            " ;
}

void Game::console(){
     system("cls") ;
     Snack *p = NULL ;
     Game wel(32,17) ;
     wel.welcome() ;
     wel.gotoxy(32,17) ;
     cout << "→" ;
     int a =17 ;
     char c ;
     while(1)
     {
       c = getch() ;
       switch(c)
       {
        case 'w':a-- ;if(a<17){a=17;}wel.gotoxy(32,a) ;cout << "→" ;wel.gotoxy(32,a+1) ;cout<<" ";break ;
        case 's':a++ ;if(a>19){a=19;}wel.gotoxy(32,a) ;cout << "→" ;wel.gotoxy(32,a-1) ;cout<<" ";break ;
        case 27 :exit(0) ;
        case 13 :if(a==17){system("cls");cheek();s.snack_Add();s.snack_food();s.snack_body();s.console();system("cls") ;wel.welcome();wel.gotoxy(32,17) ;
     cout << "→" ;break;}
                 else if(a==19){system("cls");wel.gotoxy(0,10);seting();console2();}
                 else{system("cls");wel.gotoxy(0,10);paihang();console7();}
       }
     }
}

void Game::console2()
{
    system("cls") ;
     Game g ;
     g.gotoxy(0,10) ;
     g.seting() ;
     g.gotoxy(29,11) ;
     cout << "→" ;
     int a =11 ;
     char c ;
     while(1)
     {
       c = getch() ;
       switch(c)
       {
        case 'w':a-- ;if(a<11){a=11;}g.gotoxy(29,a) ;cout << "→" ;g.gotoxy(29,a+1) ;cout<<" ";break ;
        case 's':a++ ;if(a>14){a=14;}g.gotoxy(29,a) ;cout << "→" ;g.gotoxy(29,a-1) ;cout<<" ";break ;
        case 27 :console() ;
        case 13 :if(a==11){system("cls");g.gotoxy(0,7) ;col();console3();}
                 else if(a==12){system("cls");g.gotoxy(0,7);nandu();console4();}
                 else if(a==13){system("cls");g.gotoxy(0,7);listener();console5();}
                 else{system("cls");g.gotoxy(0,7);style();console6();}
       }

     }
}

void Game::console3(){
     Game g ;
     g.gotoxy(38,9) ;
     cout << "→" ;
     int a =9 ;
     char c ;
     while(1)
     {
       c = getch() ;
       switch(c)
       {
        case 'w':a-- ;if(a<9){a=9;}g.gotoxy(38,a) ;cout << "→" ;g.gotoxy(38,a+1) ;cout<<" ";break ;
        case 's':a++ ;if(a>13){a=13;}g.gotoxy(38,a) ;cout << "→" ;g.gotoxy(38,a-1) ;cout<<" ";break ;
        case 27 :console2() ;
        case 13 :if(a==9){system("color 1");}
                 else if(a==10){system("color 2");}
                 else if(a==11){system("color 3");}
                 else if(a==12){system("color 4");}
                 else{system("color 5");}
       }
     }
}

void Game::console4(){
     Game g ;
     g.gotoxy(35,9) ;
     cout << "→" ;
     int a =9 ;
     char c ;
     while(1)
     {
       c = getch() ;
       switch(c)
       {
        case 'w':a-- ;if(a<9){a=9;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a+1) ;cout<<" ";break ;
        case 's':a++ ;if(a>11){a=11;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a-1) ;cout<<" ";break ;
        case 27 :console2() ;
        case 13 :if(a==9){s.setSpeed(150);g.gotoxy(35,13);cout << "難度設爲:  簡單   按ESC退出";}
                 else if(a==10){s.setSpeed(100);g.gotoxy(35,13);cout << "難度設爲:  一般   按ESC退出";}
                 else{s.setSpeed(50);g.gotoxy(35,13);cout << "難度設爲:  困難   按ESC退出";}
       }
     }

}

void Game::console5()
{
     Game g ;
     g.gotoxy(35,9) ;
     cout << "→" ;
     int a =9 ;
     char c ;
     while(1)
     {
       c = getch() ;
       switch(c)
       {
        case 'w':a-- ;if(a<9){a=9;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a+1) ;cout<<" ";break ;
        case 's':a++ ;if(a>10){a=10;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a-1) ;cout<<" ";break ;
        case 27 :console2() ;
        case 13 :if(a==9){s.setLis(true);g.gotoxy(35,15);cout<<"聲音已開啓       按ESC退出" ;}
                 else{s.setLis(false);g.gotoxy(35,15);cout<<"聲音已關閉       按ESC退出" ;}
       }
     }
}

void Game::console6()
{
     Game g ;
     g.gotoxy(35,9) ;
     cout << "→" ;
     int a =9 ;
     char c ;
     while(1)
     {
       c = getch() ;
       switch(c)
       {
        case 'w':a-- ;if(a<9){a=9;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a+1) ;cout<<" ";break ;
        case 's':a++ ;if(a>10){a=10;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a-1) ;cout<<" ";break ;
        case 27 :console2() ;
        case 13 :if(a==9){s.setstyle(1);g.gotoxy(35,13);cout <<"蛇的樣式:  樣式一      按ESC退出";}
                 else{s.setstyle(2);g.gotoxy(35,13);cout <<"蛇的樣式:  樣式二      按ESC退出";}
       }
     }
}

void Game::console7()
{
    char c ;
    while(1)
    {
       c = getch() ;
       if(c==27){console();break;}
    }
}
//定義邊框
void Game::cheek()
{
    cout << "╔" ;
    cout << "═══════════════════════════════╦══════" ;
    cout << "╗" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║ 難度:     ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║ 分數:     ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║  w鍵:上   ║" ;
    cout << "║                                                              ║  s鍵:下   ║" ;
    cout << "║                                                              ║  a鍵:左   ║" ;
    cout << "║                                                              ║  f鍵:右   ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║  排行榜    ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║ 1.         ║" ;
    cout << "║                                                              ║ 2.         ║" ;
    cout << "║                                                              ║ 3.         ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║            ║" ;
    cout << "║                                                              ║ESC退出遊戲 ║" ;
    cout << "╚" ;
    cout << "═══════════════════════════════╩══════" ;
    cout << "╝" ;
    gotoxy(69,17);
    cout << s.a[0]-3<<"分" ;
    gotoxy(69,18);
    cout << s.a[1]-3<<"分" ;
    gotoxy(69,19);
    cout << s.a[2]-3<<"分" ;
    gotoxy(73,3);
    if(s.getSpeed()==100){cout << "一般";}
    else if(s.getSpeed()==50){cout << "困難";}
    else {cout << "簡單" ;}
}
//定義邊框內顯示內容的座標
void Game::gotoxy(int x1, int y1)
{
     HANDLE hout ;
     COORD coord ;
     coord.X= x1 ;
     coord.Y= y1 ;
     hout=GetStdHandle(STD_OUTPUT_HANDLE) ;
     SetConsoleCursorPosition(hout,coord) ;
}



//主函數
int main()
{
    //system("color fc");
    //system("mode con cols=80 lines=25") ;
    system("title 貪喫蛇") ;
    Game g ;
    g.print() ;
    Sleep(5000) ;
    system("cls") ;
    g.welcome1() ;
    g.console() ;
    return 0 ;
}
首先進入操作說明界面:


過幾秒自動跳轉到開始界面:



遊戲界面:



排行榜:


剩下的都是設置界面:










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