上下左右键的控制也是读入之后判断属于哪一个键,再执行相应操作。输入时不能用标准输入流,要用到getch()函数,这样输入时不需要按enter,自动读入
case 72: puts(“U”);
case 80: puts(“D”);
case 75: puts(“L”);
case 77: puts(“R”);
以下是个样例,虽然写得很差,但能学习到上下左右键的基本控制
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
/*
case 72: puts(“U”);
case 80: puts(“D”);
case 75: puts(“L”);
case 77: puts(“R”);
*/
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void gotoxy(int x,int y)
{
coord.X=y;
coord.Y=x;
SetConsoleCursorPosition(hout,coord);
};
int x=10;
int y=10;
void hide() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
int direction;
void print();
void setHeroState ()
{
if(direction0&&x>2){
gotoxy(1,1);
print();
x-=1;
gotoxy(x,y);
cout <<“炮”;
}
else if(direction1&&x<12){
gotoxy(1,1);
print();
x+=1;
gotoxy(x,y);
cout <<“炮”;
}
else if(direction2&&y>2){
gotoxy(1,1);
print();
y-=1;
gotoxy(x,y);
cout <<“炮”;
}
else if(direction3&&y<39){
gotoxy(1,1);
print();
y+=1;
gotoxy(x,y);
cout <<“炮”;
}
}
void print()
{
cout << " “;
for (int i=1;i<=40;i++)
cout << “-”;
cout << endl;
for (int j=0;j<=10;j++)
{
cout << “|”;
for (int i=1;i<=40;i++) cout << " “;
cout << “|” << endl;
}
cout << " “;
for (int i=1;i<=40;i++)
cout << “-”;
}
void bang(){
if(direction0){
for(int i=x;i>1;i–){
gotoxy(1,1);
print();
gotoxy(x,y);
cout <<“炮”;
gotoxy(i,y);
cout <<"|";
}
}
if(direction1){
for(int i=x;i<13;i++){
gotoxy(1,1);
print();
gotoxy(x,y);
cout <<“炮”;
gotoxy(i,y);
cout <<”|”;
}
}
if(direction2){
for(int i=y;i>1;i–){
gotoxy(1,1);
print();
gotoxy(x,y);
cout <<“炮”;
gotoxy(x,i);
cout <<"-";
}
}
if(direction3){
for(int i=y;i<40;i++){
gotoxy(1,1);
print();
gotoxy(x,y);
cout <<“炮”;
gotoxy(x,i);
cout <<”-";
}
}
}
void gameover(){
system(“cls”);
gotoxy(6,17);
cout <<“you killed yourself, game is over!!!”;
gotoxy(7,29);
cout <<“try again”;
Sleep(3000);
}
int ch;
int main(){
cout <<"——————————"<
gotoxy(1,1);
hide();
print();
gotoxy(10,10);
cout<<“炮”;
while(ch=getch()){
if(ch72){
direction=0;
setHeroState();
}
else if(ch80){
direction=1;
setHeroState();
}
else if(ch75){
direction=2;
setHeroState();
}
else if(ch77){
direction=3;
setHeroState();
}
else if(ch==‘z’)
{
bang();
}
else if(ch==‘k’){
gameover();
return 0;
}
}
return 0;
}
下面还有一个有关上下键对于文字前箭头的控制程序(只写了主面板的几行,enter进入后,在任意按一下回到主面)
#include
#include
#include
using namespace std;
void CleanConsole(int x,int y)
{
COORD loc;
loc.Y = y; //固定行数
for (int i = 0; i < x; i++)
{
loc.X = i;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
cout << " " << endl;;
}
}
void CleanConsole( int y)
{
COORD loc;
loc.Y = y; //固定行数
for (int i = 0; i < 50; i++)
{
loc.X = i;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
cout << " " << endl;;
}
}
void WriteChar(int x, int y, char *pchar, char color)
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 1; //光标的厚度
cci.bVisible = FALSE; //光标不可见
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
COORD loc = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
cout << pchar;
}
int ShowOptions()
{
char *options[] = { “单人游戏” , “双人游戏”,“帮助”,“退出” };
system(“mode con cols=100 lines=30”);
WriteChar(16, 12, “→”, 10);
for (int i = 0; i < 4; i++)
{
WriteChar(18, 12 + i, options[i], 10);
}
char ch;
int option = 0;
while (true)
{
if (_kbhit())
{
ch = _getch(); //不进行回显
if (ch == 27) //esc
{
return -1;
}
if (ch == 72 || ch == 80 || ch == '\r')
{
if (ch == 72) //UP
{
WriteChar(16, 12 + option, " ", 0);
option--;
}
else if (ch == 80) //DOWN
{
WriteChar(16, 12 + option, " ", 0);
option++;
}
if (option < 0) //防止越界
{
option = 0;
}
else if (option >= 4) //一直让其指向租后一个选项
{
option--;
}
//处理按上下键之后的显示
WriteChar(16, 12 + option, " ", 0);
Sleep(100);
WriteChar(16, 12 + option, "→", 10);
WriteChar(18, 12 + option, options[option], 10);
if (ch == '\r')
{
return option;
}
}
}
}
}
void Page_1()
{
system(“cls”);
WriteChar(1, 1, “Just a raw page_1…\n\n”, 15);
}
void Page_2()
{
system(“cls”);
WriteChar(1, 1, “Just a raw page_2…\n\n”, 15);
}
void Page_3()
{
system(“cls”);
WriteChar(0, 0, “Just a raw page_3…\n\n”, 15);
}
int main()
{
while (true)
{
int result = ShowOptions();
if (result == 0)
{
Page_1();
system(“pause”);
}
else if (result == 1)
{
Page_2();
system(“pause”);
}
else if (result == 2)
{
Page_3();
system(“pause”);
}
else
{
cout << endl << endl;
return -1;
}
}
return 0;
}