C++コンソールの下のヘビ食い


C++コンソールの下のヘビ食い
これは私の大学1年生の最初の学期の退屈な1つの小さいゲームで、私はその时これを书いた时やはりとても感动したことを覚えていて、今大学2年生になって、振り返ってこれらのコードを见て、とても不器用で、しかし结局时间は有限で、その上これは私が书いた最初のゲームで、変えたくなくて、このようにしましょう.記念にコードを貼っておきましょう.上級者は噴き出さないでください!!
//2014.12.12 Mr.Xiong   
//      ,    ,    
#include <iostream>
#include <cstdio>
#include <string>
#include <iomanip>
#include <conio.h>
#include <ctime>
#include <windows.h>
#define ESC 27    /*ESC  ASCII */
using namespace std;
const int G_MAPX = 23;//  
const int G_MAPY = 24;//  
int G_MAP[G_MAPX][G_MAPY] = { 0 };
const int Blank = 0;
const int Obstacle = 1;
const int Snake = 2;
const int Food = 3;
int Lenght = 4;
double Settime = 9;
double Speed = 3000 / Settime;//    0.3
int i, j; //    
bool FoodExist = false;//          
enum G_Direction { UP, DOWN, LEFT, RIGHT };
G_Direction NowDirec = DOWN;//         
G_Direction PrevDirec;//        
void SetBackground();//    
void ShowBackground();//    
void gotoxy(int x, int y);  //                 
void Hidden();//    
void GameOver();
void ShowTime();//      
class SnakeNode {
private:

    SnakeNode *front, *next;
public:
    int x, y;
    void AddSnakeHead(int xx, int yy);
    void DelSnakeTail();
    int GetSnakeX() {
        return x;
    }
    int GetSnakeY() {
        return y;
    }
    void Release();
}*Head = NULL, *Tail = NULL;
void SnakeNode::AddSnakeHead(int xx, int yy) {
    SnakeNode *prt = new SnakeNode;
    prt->x = xx;
    prt->y = yy;
    if(Head != NULL) {
        prt->next = Head;
        Head->front = prt;
        Head = prt;
    } else {
        Head = Tail = prt;
        Tail->next = NULL;
    }
    gotoxy(prt->x, prt->y);
    cout << setw(2) << "●";
}
void SnakeNode::DelSnakeTail() {
    SnakeNode *Temp = Tail;
    Tail = Temp->front;
    Tail->next = NULL;
    gotoxy(Temp->x, Temp->y);
    cout << setw(2) << " ";
    delete Temp;
}
void SnakeNode::Release() { //      
    SnakeNode *p = Head;
    while(p->next == NULL) {
        p = p->next;
        delete p->front;
    }
    delete Head;
    Head = NULL;
    Tail == NULL;
}
class FOOD {
public:
    int FoodX, FoodY;
    void CreateFood();
    int GetFoodX() {
        return FoodX;
    }
    int GetFoodY() {
        return FoodY;
    }
} FD;
void FOOD::CreateFood() {
    srand((unsigned)time(NULL));
    FoodX = rand() % (G_MAPX - 1) + 1;
    FoodY = rand() % (G_MAPY - 3) + 1; //》》》》》》》》》》??????      ??????????
    gotoxy(FoodX, FoodY);
    G_MAP[FoodX][FoodY] = Food;//    
    FoodExist = true;
    cout << setw(2) << "★";
}
class Move {
private:
    char Key;

public:
    int x1, y1, FFoodX, FFoodY;
    G_Direction GetGirection();
    void Moving();
    bool Hitwall();
    bool OppositeDirec();//   ,                    
} SK;
bool Move::OppositeDirec() {
    if((PrevDirec == UP && NowDirec == DOWN) || (PrevDirec == DOWN && NowDirec == UP) || (PrevDirec == LEFT && NowDirec == RIGHT) || (PrevDirec == RIGHT && NowDirec == LEFT))
        return true;
    else
        return false;
}
bool Move::Hitwall() { //      
    if (x1 == 0 || x1 == G_MAPY - 1 || y1 == 0 || y1 == G_MAPX  - 1)
        return true;
    else
        return false;
}
G_Direction Move::GetGirection() {
    if(_kbhit()) {
        PrevDirec = NowDirec;
        Key = _getch();
        switch (Key) {
        case 0x48:
            NowDirec = UP;
            break;
        case 0x50:
            NowDirec = DOWN;
            break;
        case 0x4b:
            NowDirec = LEFT;
            break;
        case 0x4d:
            NowDirec = RIGHT;
            break;
        default:
            break;
        }
        fflush(stdin);
        if(OppositeDirec())//   。          ,    
            NowDirec = PrevDirec;
    } else
        Moving();
}
void Move::Moving() {
    x1 = Head->GetSnakeX();
    y1 = Head->GetSnakeY();
    Sleep(Speed);
    switch (NowDirec) {
    case UP:
        y1--;
        break;
    case DOWN:
        y1++;
        break;
    case LEFT:
        x1--;
        break;
    case RIGHT:
        x1++;
        break;
    default:
        break;
    }
    Head->AddSnakeHead(x1, y1);
    Tail->DelSnakeTail();
    ShowTime();
    if(!FoodExist) {
        FD.CreateFood();
    }
    FFoodX = FD.GetFoodX();
    FFoodY = FD.GetFoodY();
    if(x1 == FFoodX && y1 == FFoodY) {
        Lenght++;
        Speed -= 50;
        FoodExist = false;
        Head->AddSnakeHead(x1, y1); //     
    }
    if (Hitwall() == true)
        GameOver();
    GetGirection();
}
int main() {
    Hidden();
    SetBackground();
    ShowBackground();//     
    Head->AddSnakeHead(2, 3); //           
    Head->AddSnakeHead(2, 4);
    Head->AddSnakeHead(2, 5);
    Head->AddSnakeHead(2, 6);
    while(1) {
        SK.GetGirection();
        SK.Moving();
    }
    return 0;
}


/*****************************     *******************************/
void SetBackground() {
    for(i = 0; i < G_MAPX; i++) {
        for(j = 0; j < G_MAPY; j++) {
            if(i == 0 || i == G_MAPX - 1 || j == 0 || j == G_MAPY - 1)
                G_MAP[i][j] = Obstacle;
        }
    }
}
void ShowBackground() {
    system("color 2e");  //      
    cout << "





\t\t\t**** ****"; cout << ", "; Sleep(700); cout << " "; Sleep(700); cout << " "; Sleep(700); cout << "yi"; Sleep(700); cout << " ~~~"; cout << "





\t\t\t\t\t\t\tTip:↑↓←→ "; cout << "








\t\t\t\t\t\t\t\tLoading"; cout << "."; Sleep(300); cout << "."; Sleep(300); cout << "."; Sleep(900); system("cls"); for (i = 0; i < G_MAPX; i++) { for (j = 0; j < G_MAPY; j++) { if (G_MAP[i][j] == Obstacle) cout << setw(2) << "▉"; else cout << setw(2) << " "; } if (i == 2) cout << " >>> <<<"; else if (i == G_MAPX - 5) cout << "\t :"; else if (i == G_MAPX - 3)cout << "\t :"; cout << endl; } } void gotoxy(int x, int y) { // COORD loc = { 2 * x, y }; HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput, loc); } void Hidden() { // HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO hidden; GetConsoleCursorInfo(hOut, &hidden); hidden.bVisible = 0; SetConsoleCursorInfo(hOut, &hidden); } void GameOver() { Sleep(3000); Head->Release(); system("cls"); cout << "


\t\t******************************
"; cout << "\t\t\tGAME OVER!
"; cout << "\t\t******************************
"; Sleep(2000); exit(0); } void ShowTime() { gotoxy(G_MAPX + 2, G_MAPY - 3); // SYSTEMTIME m_time; GetLocalTime(&m_time); int a = m_time.wHour; int b = m_time.wMinute; int c = m_time.wSecond; cout << "\t\t\t" << setw(2) << a << ":" << b << ":" << c << endl; }

运行効果はそのままにしましょう.丑いだけです.~~