1. 程式人生 > >c++實現貪吃蛇

c++實現貪吃蛇

c++面向物件實現控制檯貪吃蛇

學習了c++面向物件方面的知識,閒著無聊寫了貪吃蛇,主要由下面幾個類組成:

  • MAP類實現地圖的渲染
  • 蛇類實現蛇的初始化,渲染,行走,吃食物等行為
  • *食物類實現食物的初始化及生成
  • point類實現座標類
  • 工具函式包括游標移動,控制檯設定,隨機數生成等

程式碼:

  • MAP類
class Map
{
public:
    void initialization();
private:

};
inline void Map::initialization()
{
    gotoxy(15, 0);
    cout << "貪吃蛇"
<< endl; for (int i = 0; i < 50; i++) { gotoxy(i, 1); cout << "="; } for (int j = 1; j < 21; j++) { gotoxy(0, j); cout << "#"; } for (int n = 1; n < 21; n++) { gotoxy(50, n); cout << "#"; } for
(int v = 1; v < 51; v++) { gotoxy(v, 20); cout << "="; } }
  • Snack類
class Snack
{
public:
    void initialization();
    void move();
    void turn(int key);
    void draw();
    void collision(Food& food);
    void drawscore();
    friend class Food;
private
: vector<Point> SnackNode; int score; }; inline void Snack::draw() { for (int i = 0; i < SnackNode.size(); i++) { gotoxy(SnackNode[i].get_x(), SnackNode[i].get_y()); cout << "■"; } Sleep(0.5 * 1000); system("cls"); } void Snack::collision(Food& food) { cout << SnackNode[0].get_x(); if (SnackNode[0].get_x()>=51 || SnackNode[0].get_y()>=51) { //exit(0); } if (SnackNode[0].get_x() == food.point.get_x()&&SnackNode[0].get_y()==food.point.get_y()) { SnackNode.push_back(food.point); if (food.get_flag()==1) { food.set_flag(0); score++; } } } inline void Snack::drawscore() { gotoxy(80, 5); cout << "大人,你的分數是:" << score; } inline void Snack::initialization() { Point tail(1, 8,RIGHT); Point body(3, 8, RIGHT); Point head(5, 8,RIGHT); SnackNode.push_back(head); SnackNode.push_back(body); SnackNode.push_back(tail); } inline void Snack::move() { for (int i = SnackNode.size()-1; i >0; i--) { SnackNode[i].set_x(SnackNode[i - 1].get_x()); SnackNode[i].set_y(SnackNode[i - 1].get_y()); } switch (SnackNode[0].get_direct()) { case UP: SnackNode[0].setup(); break; case RIGHT: SnackNode[0].setright(); break; case LEFT: SnackNode[0].setleft(); break; case DOWN: SnackNode[0].setdown(); break; default: break; } } inline void Snack::turn(int key) { switch (key) { case 72: { if (SnackNode[0].get_direct() != DOWN) { SnackNode[0].set_direct(UP); } break; } case 77: {if (SnackNode[0].get_direct() != LEFT) { SnackNode[0].set_direct(RIGHT); } break; } case 75: {if (SnackNode[0].get_direct() != RIGHT) { SnackNode[0].set_direct(LEFT); } break; } case 80: {if (SnackNode[0].get_direct() != UP) { SnackNode[0].set_direct(DOWN); } break; } default: break; } }
  • Food類
 class Food
{
public:
    Food(int flag1 = 0) :flag(flag1) {}
    void food_coor();
    void food_draw();
    int get_flag() const { return flag; }
    void set_flag(int fg) { flag = fg; }
    friend class Snack;
private:
    Point point;
    int flag;
};

inline void Food::food_coor()
{
    point.set_x(Random(4,47));
    point.set_y(Random(2,18));
    flag = 1;

}
inline void Food::food_draw() {
    gotoxy(point.get_x(), point.get_y());
    cout << "@";
}
  • 工具函式
 void gotoxy(int x, int y)
{
    COORD loc = { x,y };
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput, loc);
}
void setwindows() {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO CursorInfo;
    GetConsoleCursorInfo(handle, &CursorInfo);//獲取控制檯游標資訊  
    CursorInfo.bVisible = false; //隱藏控制檯游標  
    SetConsoleCursorInfo(handle, &CursorInfo);
}
  • main函式
int main()
{
    setwindows();
    Map map;
    Snack snack;
    Food food(0);
    snack.initialization();
    while (true)
    {

        while (!_kbhit())
        {
            if (food.get_flag() == 0)
            {
                food.food_coor();
            }
            map.initialization();
            snack.draw();
            snack.move();

            food.food_draw();
            snack.collision(food);
            snack.drawscore();
        }   
        snack.turn(_getch());
    }
    getchar();
    return 0;
}

總結:

c++真好玩 QAQ!