C++【小游戏系列】冒险者之旅

<<冒险者之旅>>

只是实现基础功能,后面会优化代码以及建立框架,当然有更好实现方法请告知,言谢(GBK编码)

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <iomanip>
using namespace std;

void OutputWindows( );			//输出界面外框
void OutputDownWindows( );		//输出底部界面外框
void WhileString( string, int );//循环输出字符串
void WordMain( );				//输出主界面文字
void WordRoleSlect( );			//输出角色选择文字
void WordRoleContent( int );	//输出角色介绍文字
void WordAltar( int );			//输出祭坛界面文字
void MoveMainOption( );			//移动主界面方框
void MoveRoleOption( );			//移动角色选择界面方框
void MoveRole( );				//移动角色

HANDLE hand = GetStdHandle( STD_OUTPUT_HANDLE );//创建屏幕输入输出的句柄
COORD coord;									//创建坐标的结构体
string global_role;								//游戏角色

//对本函数传值可以设置光标的位置
void SetPosition( int x, int y ) {
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition( hand, coord );	//传入输入输出句柄和坐标设置光标位置
}

//建立一个有xy的坐标结构体
struct StructXY {
	int x;
	int y;
}struct_xy, struct_xy2;

//抹除初始坐标内容,相当于更新后将之前的位置设为空格
void Erase( StructXY erase_xy, int select ) {
	SetPosition( erase_xy.x, erase_xy.y );
	cout << setw( 1 ) << "\0";
	switch( select ) {
		case 0:erase_xy.x += 11; break;
		case 1:erase_xy.x += 9; break;
	}
	SetPosition( erase_xy.x, erase_xy.y );
	cout << setw( 1 )<< "\0";
}

//循环输出文字,传入xy结构体,输出文字,输出文字个数
void WordOutput( StructXY word_xy, string *st, int number ) {
	for( int i = 0; i < number; i++ ) {
		SetPosition( word_xy.x, word_xy.y );
		cout << st[ i ];
		word_xy.y++;
	}
}

//移动角色2,传入xy结构体,方向id
void MoveRole2( StructXY role_xy, int select ) {
	SetPosition( role_xy.x, role_xy.y );
	cout << setw( 2 ) << "\0";
	switch( select ) {
		case 0:role_xy.y--; break;
		case 1:role_xy.y++; break;
		case 2:role_xy.x--; break;
		case 3:role_xy.x++; break;
	}
	SetPosition( role_xy.x, role_xy.y );
	cout << global_role;	//输出游戏角色
}

//移动方框,传入xy结构体,界面id
void MoveOption( StructXY move_xy, int select ) {
	SetPosition( move_xy.x, move_xy.y );
	cout << '[';
	switch( select ) {
		case 0:move_xy.x += 11; break;
		case 1:move_xy.x += 9; break;
	}
	SetPosition( move_xy.x, move_xy.y );
	cout << ']';
}

//输出界面外框
void OutputWindows( ) {
	cout << "╔";
	WhileString( "═", 44 );
	cout << "╗";
	SetPosition( 0,15 );
	cout << "╚";
	WhileString( "═", 44 );
	cout << "╝";
}

//输出底部界面外框
void OutputDownWindos( ) {
	SetPosition( 2, 10 );
	cout << "╔" << setw( 42 ) << "╗";
	SetPosition(2, 14);
	cout << "╚" << setw( 42 ) << "╝";
}

//循环输出字符串,传入字符串,数量
void WhileString( string st, int number ) {
	for( int i = 0; i < number; i++ ) {
		cout << st;
	}
}

//输出主界面文字
void WordMain( ) {
	OutputWindows( );
	string words_main[ ] = { "╔═════════════════╗","   冒 险 者 之 旅","╚═════════════════╝"," ",
						   "      新的冒险","      继续冒险","        设置",
					 	   "      退出游戏" };
	struct_xy = { 14,1 };
	WordOutput( struct_xy, words_main, 8 );
	MoveMainOption( );
}

//输出角色选择文字
void WordRoleSelect( ) {
	system( "cls" );
	OutputWindows( );
	string words_main[ ] = { "\t\t    选择角色"," "," "," ",
						   "\t   ●\t       ★\t   ◆"," ",
						   "\t 战  士      法  师      刺  客" };
	struct_xy = { 0, 2 };
	WordOutput( struct_xy, words_main, 7 );
	MoveRoleOption( );
}

//输出角色介绍文字
void WordRoleContent( int select ) {
	string warrior[ ] = { "生命值:100\t\t攻击:10","魔法值:50 \t\t护甲:6","技能:盾击" };
	string master[ ] = { "生命值:60 \t\t攻击:7 ","魔法值:120\t\t护甲:3","技能:炎爆" };
	string assassin[ ] = { "生命值:90 \t\t攻击:11","魔法值:80 \t\t护甲:5","技能:绞杀" };
	struct_xy2 = { 6, 11 };
	switch( select ) {
		case 7:WordOutput( struct_xy2, warrior, 3 ); break;
		case 19:WordOutput( struct_xy2, master, 3 ); break;
		case 31:WordOutput( struct_xy2, assassin, 3 ); break;
	}
}

//输出祭坛界面文字
void WordAltar( int select ) {
	system( "cls" );
	OutputWindows( );
	struct_xy = { 2,2 };
	string words_altar[ ] = { "\t\t    古老祭坛"," "," ","◤︿◥","〔〇〕","◣﹀◢",
							" "," "," ","   古老的祭坛:传闻当世界陷入灾难时就会有",
							" ","\t\t 勇士降生在此" };
	WordOutput( struct_xy, words_altar, 12 );
	OutputDownWindos( );
	SetPosition( 2, 3 );
	WhileString( "━", 42 );
	SetPosition( 2, 9 );
	WhileString( "━", 42 );
	SetPosition( 12, 6 );
	switch( select ) {
		case 7:cout << "●"; global_role = "●"; break;
		case 19:cout << "★"; global_role = "★"; break;
		case 31:cout << "◆"; global_role = "◆"; break;
	}
	MoveRole( );
}

//移动主界面方框
void MoveMainOption( ) {
	struct_xy = { 18, 5 };
	MoveOption( struct_xy, 0 );
	bool bool_0;
	while( !bool_0 ) {
		switch( getch( ) ) {
			case 'w':if( struct_xy.y > 5 ) { Erase( struct_xy, 0 );
					 struct_xy.y--; MoveOption( struct_xy, 0 ); } break;
			case 's':if( struct_xy.y < 8 ) { Erase( struct_xy, 0 );
					 struct_xy.y++; MoveOption( struct_xy, 0 ); } break;
			case '\x0d':bool_0 = true; if( struct_xy.y == 5 ) WordRoleSelect( ); break;
			default :break;
		}
	}
}

//移动角色选择界面方框
void MoveRoleOption( ) {
	OutputDownWindos( );
	WordRoleContent( 19 );
	struct_xy = { 19, 8 };
	MoveOption( struct_xy, 1 );
	bool bool_0;
	while( !bool_0 ) {
		switch( getch( ) ) {
			case 'a':if( struct_xy.x > 7 ) { Erase( struct_xy, 1 ); struct_xy.x -= 12;
					 MoveOption( struct_xy, 1 ); WordRoleContent( struct_xy.x ); } break;
			case 'd':if( struct_xy.x < 31 ) { Erase( struct_xy, 1 ); struct_xy.x += 12;
					 MoveOption( struct_xy, 1 ); WordRoleContent( struct_xy.x ); } break;
			case '\x0d':bool_0 = true; WordAltar( struct_xy.x ); break;
			default :break;
		}
	}
}

//移动角色
void MoveRole( ) {
	struct_xy = { 12, 6 };
	bool bool_0;
	while( !bool_0 ) {
		switch( getch( ) ) {
			case 'w':if( struct_xy.y > 4 ) { MoveRole2( struct_xy, 0 ); struct_xy.y--; } break;
			case 's':if( struct_xy.y < 8 ) { MoveRole2( struct_xy, 1 ); struct_xy.y++; } break;
			case 'a':if( struct_xy.x > 8 ) { MoveRole2( struct_xy, 2 ); struct_xy.x--; } break;
			case 'd':if( struct_xy.x < 44 ) { MoveRole2( struct_xy, 3 ); struct_xy.x++; } break;
			default :break;
		}
	}
}

//主函数
int main( ) {
	CONSOLE_CURSOR_INFO info = { 1, 0 };	//建立一个光标属性的结构体并赋值,1代表透明度,0代表不显示
	SetConsoleCursorInfo( hand, &info );	//设置光标的属性,调入光标的句柄和引用光标属性结构体
	SetConsoleTitle ( TEXT( "冒险者之旅" ) );	//设置cmd标题
	system ( "mode con cols=47 lines=16" );	//设置cmd窗口大小
	system ( "color 0f" );					//设置cmd背景颜色
	WordMain( );							//运行游戏主界面函数
	SetPosition( 0, 16 );
	return 0;
}

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值