用c++ 让控制台蹦迪(输出随机彩色字符串)
#include <bits/stdc++.h>
using namespace std;
#ifndef GAMEDEMO_OUTCOLOR_H
#define GAMEDEMO_OUTCOLOR_H
#define HEAD "\033["
#define F_BLACK "30"
#define F_RED "31"
#define F_GREEN "32"
#define F_YELLOW "33"
#define F_BLUE "34"
#define F_MAGENTA "35"
#define F_CYAN "36"
#define F_WHITE "37"
#define Q_HEI "30"
#define Q_HONG "31"
#define Q_LV "32"
#define Q_HUANG "33"
#define Q_LAN "34"
#define Q_PIN_HONG "35"
#define Q_QING "36"
#define Q_BAI "37"
#define BG_BLACK "40"
#define BG_RED "41"
#define BG_GREEN "42"
#define BG_YELLOW "43"
#define BG_BLUE "44"
#define BG_MAGENTA "45"
#define BG_CYAN "46"
#define BG_WHITE "47"
#define B_HEI "40"
#define B_HONG "41"
#define B_LV "42"
#define B_HUANG "43"
#define B_LAN "44"
#define B_PIN_HONG "45"
#define B_QING "46"
#define B_BAI "47"
#define INIT "0"
#define BOLD "1"
#define DIM "2"
#define ITALIC "3"
#define U_LINE "4"
#define BLINK "5"
#define REVERSAL "7"
#define REVE "7"
#define CONCEAL "8"
#endif // GAMEDEMO_OUTCOLOR_H
const string FORE_COLOR[8] = {F_BLACK,F_RED,F_GREEN,F_YELLOW,F_BLUE,F_MAGENTA,F_CYAN,F_WHITE};
const string BACKGROUND[8] = {BG_BLACK,BG_RED,BG_GREEN,BG_YELLOW,BG_BLUE,BG_MAGENTA,BG_CYAN,BG_WHITE};
const string RESULT[8] = {INIT,BOLD,DIM,ITALIC,U_LINE,BLINK,REVERSAL,CONCEAL};
/**
* 在输出的字符串前插入返回值即可设置字符串格式
* 例如 cout \<\< setColor(ITALIC,Q_PIN_HONG,BG_BLUE) \<\< 123; 可以显示一个斜体前景品红色背景蓝色的字符串123
* @param result 特效
* @param forecolor 前景色
* @param background 背景色
* @return 设置字体的特效的字符串
*/
string setColor(string result,string forecolor,string background){
string str = ";";
return string(HEAD)+string(result)+ str+string(forecolor)+ str + string(background)+string("m");
}
/**
* 获取用以初始化的字符串
* @return "\033[0m"
*/
string getInitStr(){
return "\033[0m";
}
/**
* 初始化字体设置
*/
void init(){
printf(getInitStr().c_str());
}
int main() {
int r,f,b;
srand(time(0));
while (1){
r = rand()%8;
f = rand()%8;
b = rand()%8;
if(f==b){
continue;
}
cout <<setColor(RESULT[r],FORE_COLOR[f],BACKGROUND[b]) <<char(rand()%(126-33+1)+33);
}
return 0;
}
复制即可运行。