溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++通過類如何實現(xiàn)控制臺貪吃蛇

發(fā)布時間:2020-07-29 14:45:44 來源:億速云 閱讀:173 作者:小豬 欄目:編程語言

這篇文章主要為大家展示了C++通過類如何實現(xiàn)控制臺貪吃蛇,內(nèi)容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

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

現(xiàn)是頭文件

#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);//打?qū)崟r印信息
 void print_snack(point *snake);//打印蛇
 void move(point *snake);//移動蛇
 void get_food(point *snake,Food *food);//隨機產(chǎn)生食物
 void eating(point *snake,Food *food);//吃食物
 bool die(point *snake);//判斷蛇是否死亡
 
 void start_game();//主函數(shù)
 void end_game();//結(jié)束界面
 
private:
 int length;//蛇長度
 int score;//得分
 int account;//已吃的食物數(shù)量
 int difficulty;//難度 
 int stage;//關(guān)卡
 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//虛擬鍵數(shù)
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) &#63; 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_,down_,left_,right_,w_,s_,a_,d_,y_,n_,esc_,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 << "分數(shù):" << 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++)/* 后一節(jié)的坐標依次取代前一節(jié)的坐標 */
 {
 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);
 
 /* 產(chǎn)生食物的條件:1.在游戲窗口內(nèi) 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++)
 {/* 判斷食物是否在蛇的身上,如果在蛇身上,則重新產(chǎn)生;否則,打印蛇身 */
 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);//設(shè)置蛇顏色
}
 
 
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--)/* 蛇后節(jié)坐標依次賦值給蛇前一節(jié)的坐標,依次得到蛇身及蛇頭的坐標 */
 {
 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;//分數(shù)增加
 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);//設(shè)置控制臺光標信息
 system("mode con cols=100 lines=40");//設(shè)置窗口大小
 
 point s, *snake = &s; //定義蛇的結(jié)構(gòu)體指針并指向蛇的結(jié)構(gòu)體
 Food f, *food = &f; //定義食物的結(jié)構(gòu)體指針并指向食物的結(jié)構(gòu)體
 keyboard key_state, Key;
 keyboard temp = right_;
 int j = 0;
 char ch=77;
 
 begin_view();
 gotoxy(12, 34);
 cout << "按任意鍵繼續(xù)......";
 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函數(shù)

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

以上就是關(guān)于C++通過類如何實現(xiàn)控制臺貪吃蛇的內(nèi)容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI