我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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
#include
#include
#include
#include
 
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 << "    /^\\/^\\ "<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; ix[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(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; ix == 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++通过类如何实现控制台贪吃蛇
路径分享:http://shouzuofang.com/article/iphccd.html

其他资讯