这是游戏内菜单的一些代码。用户按ESC键调出菜单,按F8退出游戏
#include <iostream>
#include <conio.h>
using namespace std;
#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66
void gamemenu()
{
cout<<" \n";
cout<<"[1] Resume Game \n";
cout<<"[2] Save Game \n";
cout<<"[3] Exit Game \n";
}
int main()
{
int KB_code=0;
cout<<"Game menu 0.01 beta\n\n";
cout<<"press F8 to Exit\n";
cout<<"press ESC for Game menu \n";
while(KB_code != KB_F8 )
{
if (kbhit())
{
KB_code = getch();
//cout<<"KB_code = "<<KB_code<<"\n";
switch (KB_code)
{
case KB_ESCAPE:
gamemenu();
break;
case KB_LEFT:
//Do something
break;
case KB_RIGHT:
//Do something
break;
case KB_UP:
//Do something
break;
case KB_DOWN:
//Do something
break;
}
}
}
return 0;
}
输出:
Game menu 0.01 beta
press F8 to Exit
press ESC for Game menu
[1] Resume Game
[2] Save Game
[3] Exit Game