1. 程式人生 > 程式設計 >C++通過類實現控制檯貪吃蛇

C++通過類實現控制檯貪吃蛇

本文例項為大家分享了C++實現控制檯貪吃蛇的具體程式碼,供大家參考,具體內容如下

初學c++,算剛學完類吧,寫個貪吃蛇來玩玩。

現是標頭檔案

#pragma once
#ifndef SNACK_H
#define SNACK_H
 
#define Frame_X 5//邊框起點橫座標
#define Frame_Y 5//邊框起點縱座標
#define Frame_width 50//邊框長度
#define Frame_height 25//邊框高度
 
struct point//蛇個點座標
{
 int x[100]; //蛇的橫座標,其中x[0]表示蛇尾的橫座標,x[N-1]表示蛇頭的橫座標
 int y[100]; //蛇的豎座標,其中y[0]表示蛇尾的豎座標,y[N-1]表示蛇頭的豎座標
};
struct Food//食物座標
{
 int x;
 int y;
};
 
 
class snack
{
public:
 snack();
 void init_snack(point *snake);//初始化蛇
 void gotoxy(int x,int y);//游標移動
 void begin_view();//封面
 void frame();//列印邊界與遊戲說明
 void menu(point *snake,Food *food);//打實時印資訊
 void print_snack(point *snake);//列印蛇
 void move(point *snake);//移動蛇
 void get_food(point *snake,Food *food);//隨機產生食物
 void eating(point *snake,Food *food);//吃食物
 bool die(point *snake);//判斷蛇是否死亡
 
 void start_game();//主函式
 void end_game();//結束介面
 
private:
 int length;//蛇長度
 int score;//得分
 int account;//已吃的食物數量
 int difficulty;//難度 
 int stage;//關卡
 int life;//生命值
 
};
 
#endif

主要的cpp

#include"stdafx.h"
#include"snack.h"
#include<Windows.h>
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
 
using std::cout;
using std::cin;
using std::endl;
 
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
/*************獲取按鍵************/
#define key_number 11//虛擬鍵數
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) 
enum keyboard { up_,down_,left_,right_,w_,s_,a_,d_,y_,n_,esc_,other_};
int keyboard_list[] = {VK_UP,VK_DOWN,VK_LEFT,VK_RIGHT,'W','S','A','D','Y','N',VK_ESCAPE};
keyboard keyboardarray[] = { up_,other_ };
keyboard get_the_key()
{
 for (int i = 0; i < key_number; i++)
 {
 if (KEYDOWN(keyboard_list[i]))
 return keyboardarray[i];
 }
 return keyboardarray[key_number];
}
 
 
int a[2];//記錄蛇尾座標
 
snack::snack()
{
 length = 3;
 score = 0;
 account = 0;
 difficulty = 1;
 stage = 1;
 life = 3;
}
 
void snack::init_snack(point *snake)
{
 snake->x[0] = Frame_X + 2;  //初始化蛇的橫座標
 snake->y[0] = Frame_Y + Frame_height / 2; //初始化蛇的豎座標
 for (int i = 1; i < length; i++)/* 依次得到蛇身、蛇頭的座標 */
 {
 snake->x[i] = snake->x[i - 1] + 2;
 snake->y[i] = snake->y[i - 1];
 }
}
 
void snack::gotoxy(int x,int y)
{ 
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(hOut,pos);
}
 
void snack::begin_view()
{
 SetConsoleTextAttribute(hOut,FOREGROUND_GREEN | FOREGROUND_INTENSITY);
 cout << "\n\n\n\n";
 cout << "   ****  *  *  *   ****  *  *\n";
 cout << "  *   * **  *  *  *  * * *\n";
 cout << "  *   * * *  ***  *   * *\n";
 cout << "   ***  * * *  * *  *   **\n";
 cout << "      *  * * *  * *  *   * *\n";
 cout << "       * * * *  *****  *   * *\n";
 cout << "  *    * *  ** *  *  *   * *  *\n";
 cout << "   ****  *  * **  **   ****  *  *\n";
 SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
 cout << "\n\n\n\n";
 cout << "    /^\\/^\\ "<<endl;
 cout << "    _|__| O|"<<endl;
 cout << " \\/  /~  \_/ \\ "<<endl;
 cout << "  \\____|__________/ \\ "<<endl;
 cout << "   \\_______  \\ "<<endl;
 cout << "     `\\  \\     \\ "<<endl;
 cout << "      |  |     \\ "<<endl;
 cout << "      /  /      \\ "<<endl;
 cout << "     /  /      \\\\ "<<endl;
 cout << "     /  /       \\ \\ "<<endl;
 cout << "     /  /       \\ \\ "<<endl;
 cout << "    /  /    _----_   \\ \\ "<<endl;
 cout << "    /  /   _-~  ~-_   | |"<<endl;
 cout << "    (  (  _-~ _--_ ~-_  _/ |"<<endl;
 cout << "    \\  ~-____-~ _-~ ~-_ ~-_-~ /"<<endl;
 cout << "    ~-_   _-~   ~-_  _-~"<<endl;
 cout << "     ~--______-~    ~-___-~"<<endl;
 
}
 
void snack::frame()
{
 SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | FOREGROUND_INTENSITY);
 //列印上邊界
 gotoxy(Frame_X,Frame_Y);
 cout << "┏";
 for (int i = 0; i < Frame_width/2-2; i++)
 cout << "━";
 cout << "┓";
 //列印下邊界
 gotoxy(Frame_X,Frame_Y + Frame_height);
 cout << "┗";
 for (int i = 0; i < Frame_width/2-2; i++)
 cout << "━";
 cout << "┛"; 
 //列印左右邊界
 for (int i = 0; i < Frame_height-1; i++)
 {
 gotoxy(Frame_X,Frame_Y + 1 + i);
 cout << "┃";
 gotoxy(Frame_X+Frame_width-2,Frame_Y + 1 + i);
 cout << "┃";
 }
 SetConsoleTextAttribute(hOut,FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
 gotoxy(Frame_X + Frame_width + 3,Frame_Y + 1);
 cout << "按鍵說明:方向鍵或WSAD鍵控制";
 gotoxy(Frame_X + Frame_width + 3,Frame_Y + 3);
 cout << " 長按加速";
 gotoxy(Frame_X + Frame_width + 3,Frame_Y + 5);
 cout << " 按ESC退出遊戲";
 
}
 
 
void snack::menu(point *snake,Food *food)
{
 gotoxy(Frame_X + 3,Frame_Y - 2);
 cout << "分數:" << score;
 gotoxy(Frame_X + Frame_width -11,Frame_Y - 2);
 cout << "生命值:" << life;
 gotoxy(Frame_X + Frame_width - 30,Frame_Y - 2);
 cout << "等級:" << stage;
 /*gotoxy(Frame_X + Frame_width + 3,Frame_Y + 5);
 cout << "橫座標:" << food->x;
 gotoxy(Frame_X + Frame_width + 3,Frame_Y + 7);
 cout << "縱座標:" << food->y;
 gotoxy(Frame_X + Frame_width + 3,Frame_Y + 9);
 cout << "X: " << snake->x[length - 1];
 gotoxy(Frame_X + Frame_width + 3,Frame_Y + 11);
 cout << "Y: " << snake->y[length - 1];*/
 
 
}
 
 
void snack::print_snack(point *snake)
{
 for (int i = 0; i<length; i++)
 {
 gotoxy(snake->x[i],snake->y[i]);
 if (i == 0)
 cout << "○";//列印蛇尾
 else if (i == length - 1)
 cout << "¤";//列印蛇頭
 else
 cout << "◎";//列印蛇身
 }
}
 
 
void snack::move(point *snake)
{
 gotoxy(snake->x[0],snake->y[0]);
 cout << " ";//清除蛇尾
 for (int i = 1; i < length ; i++)/* 後一節的座標依次取代前一節的座標 */
 {
 snake->x[i - 1] = snake->x[i];
 snake->y[i - 1] = snake->y[i];
 }
}
 
 
void snack::get_food(point *snake,Food *food)
{
 int i,j; 
 srand(static_cast<unsigned int>(time(0)));
 SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_INTENSITY);
 
 /* 產生食物的條件:1.在遊戲視窗內 2.不在蛇的身上 */
 while (1)
 {
 j = 0;
 food->x = Frame_X + 2 * (rand() % (Frame_width / 2 - 2)) + 2;//得到食物的橫座標
 food->y = Frame_Y + rand() % (Frame_height - 2) + 1;//得到食物的縱座標 
 for (i = 0; i<length; i++)
 {/* 判斷食物是否在蛇的身上,如果在蛇身上,則重新產生;否則,列印蛇身 */
 if (food->x == snake->x[i] && food->y == snake->y[i])
 {
 j = 1;
 break;
 }
 }
 if (i == length)
 {
 gotoxy(food->x,food->y);
 cout << "⊙";
 break;
 }
 else
 continue;
 }
 SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);//設定蛇顏色
}
 
 
void snack::eating(point *snake,Food *food)
{
 if (snake->x[length - 1] == food->x&&snake->y[length - 1] == food->y)
 {
 length++;
 for (int i = length - 1; i > 0; i--)/* 蛇後節座標依次賦值給蛇前一節的座標,依次得到蛇身及蛇頭的座標 */
 {
 snake->x[i] = snake->x[i - 1];
 snake->y[i] = snake->y[i - 1];
 }
 snake->x[0] = a[0];//得到蛇尾移動前的橫座標 
 snake->y[0] = a[1];//得到蛇尾移動前的縱座標 
 get_food(snake,food);
 account++; 
 score+=difficulty*10;//分數增加
 if (account % 5 == 0)
 {
 difficulty++;
 stage++;
 }
 }
}
 
bool snack::die(point *snake)
{
 for (int i = 0; i < length-1; i++)
 {
 if (snake->x[length - 1] == snake->x[i] && snake->y[length - 1] == snake->y[i])
 return true;
 }
 if (snake->x[length - 1] == Frame_X || snake->y[length - 1] == Frame_Y || snake->x[length - 1] == Frame_X + Frame_width - 2 || snake->y[length - 1] == Frame_Y + Frame_height - 1)
 return true;
 return false;
}
 
 
void snack::start_game()
{
 //游標隱藏
 CONSOLE_CURSOR_INFO Cursorlnfo;
 GetConsoleCursorInfo(hOut,&Cursorlnfo);//獲取控制檯游標資訊
 Cursorlnfo.bVisible = false;//隱藏控制檯游標資訊
 SetConsoleCursorInfo(hOut,&Cursorlnfo);//設定控制檯游標資訊
 system("mode con cols=100 lines=40");//設定視窗大小
 
 point s,*snake = &s; //定義蛇的結構體指標並指向蛇的結構體
 Food f,*food = &f; //定義食物的結構體指標並指向食物的結構體
 keyboard key_state,Key;
 keyboard temp = right_;
 int j = 0;
 char ch=77;
 
 begin_view();
 gotoxy(12,34);
 cout << "按任意鍵繼續......";
 while(!_kbhit())
 { }
 system("cls");
 frame();
 init_snack(snake);
 get_food(snake,food);
 
 while (1)
 {
 j = 0;
 menu(snake,food);
 a[0] = snake->x[0];
 a[1] = snake->y[0];
 key_state = get_the_key();
 if (key_state == temp)//長按加速
 {
 j = 1;
 Sleep(20);
 }
 if(key_state!=other_)
 temp = key_state;
 switch (temp)
 {
 case up_:
 case w_:
 move(snake);
 snake->y[length - 1] -= 1;
 break;
 case down_:
 case s_:
 move(snake);
 snake->y[length - 1] += 1;
 break;
 case left_:
 case a_:
 move(snake);
 snake->x[length - 1] -= 2;
 break;
 case right_:
 case d_:
 move(snake);
 snake->x[length - 1] += 2;
 break;
 case esc_:
 end_game();
 break;
 }
 
 
 eating(snake,food);
 print_snack(snake);
 if (die(snake))
 {
 life--;
 if (life == 0)
 end_game();
 else
 {
 length = 3;
 start_game();
 }
 
 }
 if (j == 0)
 {
 if (difficulty < 10)
 Sleep(200 - difficulty * 20);
 else
 Sleep(10);
 }
 else
 Sleep(10);
 }
}
 
void snack::end_game()
{
 system("CLS");
 system("color 0D");
 cout << "\n   ***  *   **  **  *********";
 cout << "\n  *  *  *   **  **  *  ";
 cout << "\n *     ***   * * * *  *  ";
 cout << "\n *     * *  * * * *  ******** ";
 cout << "\n *  ***  * *  *  *  * *  ";
 cout << "\n *   *  *****  *  *  * *  ";
 cout << "\n  *  * *  *  *  *  * *  ";
 cout << "\n   **  **  ** *  *  * *********";
 cout << "\n\n\n\n\n\n";
 cout << "\n  ***  **  ** ********** ******  ";
 cout << "\n *   *   *   *   *      *  * ";
 cout << "\n *  *  *  *  *    *  * ";
 cout << "\n *  *  * *  ********  ******  ";
 cout << "\n *  *  * *  *    * *  ";
 cout << "\n *  *  * *  *    *  *  ";
 cout << "\n *  *  ***  *    *  ** ";
 cout << "\n  ***   *  ********** *  ** ";
 
 SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
 gotoxy(Frame_X + Frame_width / 2,27);
 cout << "總得分:" << score;
 Sleep(200);
 exit(0);
 
}

然後是main函式

#include "stdafx.h"
#include"snack.h"
 
int main()
{ 
 snack a;
 a.start_game();
 return 0;
}

關於C++小遊戲的更多精彩內容請點選專題: 《C++經典小遊戲》 學習瞭解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。