介绍
这篇文章主要为大家展示了c++通过类如何实现控制台贪吃蛇,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
初学c++,算刚学完类吧,写个贪吃蛇来玩玩。
现是头文件
# pragma> # include" stdafx.h" # include" snack.h" # include# include # include # include # include 使用std:: cout; 使用std:: cin; 使用std:: endl; 处理胡特=GetStdHandle (STD_OUTPUT_HANDLE);/* * * * * * * * * * * * *获取按键* * * * * * * * * * * */#定义key_number 11//虚拟键数 #定义KEYDOWN (vk_code) ((GetAsyncKeyState (vk_code),0 x8000), # 63;1:0) enum键盘{up_, down_、left_ right_, w_, s_,现代,d_, y_, n_, esc_, other_}; int keyboard_list []={VK_UP、VK_DOWN VK_LEFT, VK_RIGHT, & # 39; w # 39;, & # 39; & # 39;, & # 39;一个# 39;,& # 39;d # 39;, & # 39; y # 39;, & # 39; n # 39;, VK_ESCAPE}; 键盘keyboardarray []={up_、down_ left_, right_, w_, s_,现代,d_, y_, n_, esc_, other_}; 键盘get_the_key () { for (int i=0;我& lt;key_number;我+ +) { 如果(KEYDOWN (keyboard_list[我])) 返回keyboardarray[我]; } 返回keyboardarray [key_number]; } int[2];//记录蛇尾坐标 零食:零食() { 长度=3; 分数=0; 账户=0; 困难=1; 阶段=1; 生活=3; } 无效的零食:init_snack(点*蛇) { 蛇→x [0]=Frame_X + 2;//初始化蛇的横坐标 蛇→y [0]=Frame_Y + Frame_height/2;//初始化蛇的竖坐标 for (int i=1;我& lt;长度;我+ +)/*依次得到蛇身,蛇头的坐标*/{ 蛇蛇→x[我]=鷛 (i - 1) + 2; 蛇蛇→y[我]=鷜 (i - 1); } } 无效的零食::gotoxy (int x, int y) { COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(胡特、pos); } 无效的零食:begin_view () { SetConsoleTextAttribute(胡特,FOREGROUND_GREEN | FOREGROUND_INTENSITY); cout & lt; & lt;“\ n \ n \ n \ n"; cout & lt; & lt;“;* * * * * * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * * * \ n"; cout & lt; & lt;“;* * * * * * * * * * * * * * * * \ n"; SetConsoleTextAttribute(胡特,FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); cout & lt; & lt;“\ n \ n \ n \ n"; cout & lt; & lt;“;/^ \ \/^ \ \“& lt; & lt; endl; cout & lt; & lt;“;阿_ | _ | |“& lt; & lt; endl; cout & lt; & lt;“;\ \//~ \ _/\ \“& lt; & lt; endl; cout & lt; & lt;“;__ | __________ \ \/\ \“& lt; & lt; endl; cout & lt; & lt;“;____ \ \ \ \“& lt; & lt; endl; cout & lt; & lt;“;' \ \ \ \ \ \“& lt; & lt; endl; cout & lt; & lt;“;| | \ \“& lt; & lt; endl; cout & lt; & lt;“;//\ \“& lt; & lt; endl; cout & lt; & lt;“;//\ \ \ \“& lt; & lt; endl; cout & lt; & lt;“;//\ \ \ \“& lt; & lt; endl; cout & lt; & lt;“;//\ \ \ \“& lt; & lt; endl; cout & lt; & lt;“;//_,_ \ \ \ \“& lt; & lt; endl; cout & lt; & lt;“;//_ - ~ ~ _ | |“& lt; & lt; endl; cout & lt; & lt;“;(~ _ ~ _ - _ _ _/|“& lt; & lt; endl; cout & lt; & lt;“;\ \ ~ __ - ~ _ ~ ~ _ ~ _ ~/? lt; & lt; endl; cout & lt; & lt;“;~ ~ ~ _ _ _ _,~“& lt; & lt; endl; cout & lt; & lt;“;~——______ - ~ ~ ___ ~“& lt; & lt; endl; } 空白零食:框架() { SetConsoleTextAttribute(胡特,FOREGROUND_BLUE | FOREGROUND_INTENSITY);//打印上边界 gotoxy (Frame_X Frame_Y); cout & lt; & lt;┏“;; for (int i=0;我& lt;Frame_width/2 - 2;我+ +) cout & lt; & lt;“━“; cout & lt; & lt;“┓“;//打印下边界 gotoxy (Frame_X Frame_Y + Frame_height); cout & lt; & lt;┗“;; for (int i=0;我& lt;Frame_width/2 - 2;我+ +) cout & lt; & lt;“━“; cout & lt; & lt;“┛“;//打印左右边界 for (int i=0;我& lt;Frame_height-1;我+ +) { gotoxy (Frame_X Frame_Y + 1 + i); cout & lt; & lt;┃“;; gotoxy (Frame_X + Frame_width-2 Frame_Y + 1 + i); cout & lt; & lt;┃“;; } SetConsoleTextAttribute(胡特,FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); gotoxy (Frame_X + Frame_width + 3, Frame_Y + 1); cout & lt; & lt;“按键说明:方向键或WSAD键控制“; gotoxy (Frame_X + Frame_width + 3, Frame_Y + 3); cout & lt; & lt;“;长按加速“; gotoxy (Frame_Y Frame_X + Frame_width + 3, + 5); cout & lt; & lt;“;按ESC退出游戏“; } 空白零食::菜单(*蛇,食物*) { gotoxy (Frame_X + 3, Frame_Y - 2); cout & lt; & lt;“分数:“;& lt; & lt;分数; -11年gotoxy (Frame_X + Frame_width Frame_Y - 2); cout & lt; & lt;“生命值:“;& lt; & lt;生活; gotoxy (Frame_Y Frame_X + Frame_width - 30日- 2); cout & lt; & lt;“等级:“;& lt; & lt;阶段;/* gotoxy (Frame_Y Frame_X + Frame_width + 3, + 5); cout & lt; & lt;“横坐标:“;& lt; & lt;食物→x; gotoxy (Frame_Y Frame_X + Frame_width + 3, + 7); cout & lt; & lt;“纵坐标:“;& lt; & lt;食物→y; gotoxy (Frame_Y Frame_X + Frame_width + 3, + 9); cout & lt; & lt;“谢:“& lt; & lt;蛇→x(长度- 1); gotoxy (Frame_X + Frame_width + 3, Frame_Y + 11); cout & lt; & lt;“杨:“& lt; & lt;蛇→y(长度- 1);*/} 无效的零食:print_snack(点*蛇) { for (int i=0;i c++通过类如何实现控制台贪吃蛇