上下左右键控制 简单

上下左右键的控制也是读入之后判断属于哪一个键,再执行相应操作。输入时不能用标准输入流,要用到getch()函数,这样输入时不需要按enter,自动读入
case 72: puts(“U”);
case 80: puts(“D”);
case 75: puts(“L”);
case 77: puts(“R”);

以下是个样例,虽然写得很差,但能学习到上下左右键的基本控制
#include<bits/stdc++.h>
#include<windows.h>
#include
#include
#include
#include <conio.h>
#include
#include <stdio.h>
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(direction
1&&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(direction
3&&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(direction
1){
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(direction
3){
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 <<"——————————"<<endl;
cout <<“炮打小游戏”<<endl;
cout <<“制作人:陆麒宇”<<endl;
cout <<"——————————"<<endl;
cout <<“炮弹打出后要冷却一会”<<endl;
cout <<"——————————"<<endl;
Sleep(3000);
gotoxy(1,1);
hide();
print();
gotoxy(10,10);
cout<<“炮”;
while(ch=getch()){
if(ch72){
direction=0;
setHeroState();
}
else if(ch
80){
direction=1;
setHeroState();
}
else if(ch75){
direction=2;
setHeroState();
}
else if(ch
77){
direction=3;
setHeroState();
}
else if(ch==‘z’)
{
bang();
}
else if(ch==‘k’){
gameover();
return 0;
}
}
return 0;
}
下面还有一个有关上下键对于文字前箭头的控制程序(只写了主面板的几行,enter进入后,在任意按一下回到主面)
#include
#include <windows.h>
#include <conio.h>
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;

}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值