教你如何用c++写2D版我的世界(1)——续

发布于:2022-07-26 ⋅ 阅读:(8989) ⋅ 点赞:(7)

目录

改变颜色

system("color")

SetConsoleTextAttribute(参考于匿名用户__和yyf525的博客)

hello!我又来啦!

上一篇我们写了我的世界2D版的初始界面,但小编觉得还不够,还要给代码再升华一下,比如:

改变颜色

在c++里,有2种主要的改变窗口颜色的办法,我们来看第一种:

system("color")

不由得说,system永远是神,哪里都能碰到他,我们可以使用system改变窗口前景色和背景色,如:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	system("color F0");
	cout<<"hello";
	return 0;
}


效果:

 

哎,可能有读者注意到了,color后面加了个“F0”,这是什么意思呢?下面给大家看一张图片:

这下大家应该懂了吧,前面的F指背景色亮白色,后面的0指前景色黑色。

 当然,这时候又有人说了,如果要改变单一文字颜色该怎么办?别急,还有这个:

SetConsoleTextAttribute(参考于匿名用户__和yyf525的博客)

在使用system("color")的时候,你会发现,一旦使用该函数,整个窗口都会被改变颜色。那么如何设置单个字的颜色呢?这就要用到SetConsoleTextAttribute函数了,他需要头文件<windows.h>

SetConsoleTextAttribute函数:

void SetColorAndBackground(int ForgC, int BackC) {
	WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

其中ForgC指前景色,BackC指背景色,和system一样,数值仍然是1~15。

看个例子:

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;

void SetColorAndBackground(int ForgC, int BackC) {
	WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
	
int main() {
	SetColorAndBackground(15,0);
	cout<<"hello"<<endl;
	SetColorAndBackground(7,4);
	cout<<"hello"<<endl;
	return 0;
}

效果:

 当然,如果你觉得太复杂了,可以试试这个简单版:

void color(int n)//颜色
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n);
	return;
}

这是上面函数的颜色表(使用c++输出):

 代码:

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void color(int n)//颜色
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n);
	return;
}
int main()
{
	cout<<"     ";
	for(int i=1;i<=255;i++){
		color(i);
		cout<<" ";
		if(i<100)cout<<" ";
		if(i<10)cout<<" ";
		cout<<i;
		color(7);
		cout<<" ";
		if(i%16==15)cout<<"\n";
	}
	return 0;
}

既然学了颜色的设置,我们就能把我的世界2D版的界面改成这样:

 总代码:

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h> 
void rgb_init() {																// 初始化
	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);		//输入句柄
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);		//输出句柄
	DWORD dwInMode, dwOutMode;
	GetConsoleMode(hIn, &dwInMode);						//获取控制台输入模式
	GetConsoleMode(hOut, &dwOutMode);					//获取控制台输出模式
	dwInMode |= 0x0200;									//更改
	dwOutMode |= 0x0004;
	SetConsoleMode(hIn, dwInMode);						//设置控制台输入模式
	SetConsoleMode(hOut, dwOutMode);					//设置控制台输出模式
}
void rgb_set(int wr,int wg,int wb,int br,int bg,int bb) {						//设置RGB
	printf("\033[38;2;%d;%d;%dm\033[48;2;%d;%d;%dm",wr,wg,wb,br,bg,bb);	//\033[38表示前景,\033[48表示背景,三个%d表示混合的数
}
using namespace std;
int main()
{
	rgb_init();
	rgb_set(255,255,255,0,168,243);
	system("title Minecraft");//标题 
	//初始菜单 
	system("mode con cols=60 lines=37");//设置窗口大小 
	string s="|                       MINCRAFT 2D                        |";
	string s1="|                              by Tony                     |";
	string s2="|             Pleas type 'p' start the game...             |";
	string s3="|                                   Type 'o' exit the game.|";
	cout<<"@----------------------------------------------------------@"<<endl;
	cout<<"|                                                          |"<<endl;
	cout<<"|                                                          |"<<endl;
	for(int i=0;i<s.size();i++){
		cout<<s[i];
		Sleep(10);//Sleep函数延长时间 
	}
	cout<<endl;
	for(int i=0;i<s1.size();i++){
		cout<<s1[i];
		Sleep(10);
	}
	cout<<endl;
	for(int i=0;i<s2.size();i++){
		cout<<s2[i];
		Sleep(10);
	}
	cout<<endl;
	cout<<"|                                                          |"<<endl;
	cout<<"|                                                          |"<<endl;
	cout<<"|                                                          |"<<endl;
	cout<<"|                                                          |"<<endl;
	for(int i=0;i<s3.size();i++){
		cout<<s3[i];
		Sleep(10);
	}
	cout<<endl;
	cout<<"|                                                          |"<<endl;
	cout<<"@----------------------------------------------------------@"<<endl;
	return 0;
}


以上就是本期的内容了,有问题请私信指出(~ ̄▽ ̄)~

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到