当前位置:主页 > 软件编程 > C代码 >

C++通过类实现控制台贪吃蛇

时间:2021-05-07 10:51:13 | 栏目: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_,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 << "分数:" << 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++经典小游戏》 学习了解

您可能感兴趣的文章:

相关文章