用c++在控制台(黑框)内画出一辆彩色跑车

发布于:2022-12-25 ⋅ 阅读:(474) ⋅ 点赞:(0)

用c++在控制台(黑框)内画出一辆彩色跑车

效果演示

在这里插入图片描述

用c++在控制台内画出一辆彩色小汽车

源码

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

// 改颜色打印 
int oPrint(int r, int g, int b, char c, int n) {
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hOut == INVALID_HANDLE_VALUE)return GetLastError();
	DWORD dwMode = 0;
	if (!GetConsoleMode(hOut, &dwMode))return GetLastError();
	dwMode |= 0x0004;
	if (!SetConsoleMode(hOut, dwMode))return GetLastError();
	for(int i = 0; i < n; i++) {
		wprintf(L"\x1b[38;2;%d;%d;%dm%c", r, g, b, c);
	}
	return 0;
}

// 打印边框 
inline void bk(int n) {
	oPrint(255, 255, 255, '1', n);
}

// 打印n个空格 
inline void pSpace(int n) {
	cout << setfill(' ') << setw(n) << "";
}

// 逐行输出一个小汽车
void pCar(int left) {
	 
	pSpace(9 + left);bk(14);
	cout << "\n";
	
	pSpace(8 + left);bk(1);
	oPrint(0, 112, 192, '0', 6);bk(1);
	oPrint(0, 112, 192, '0', 2);
	oPrint(244, 176, 132, '0', 3);
	oPrint(0, 112, 192, '0', 2);bk(1);
	cout << "\n";
	
	pSpace(7 + left);bk(1);
	oPrint(0, 112, 192, '0', 7);bk(1);
	oPrint(0, 112, 192, '0', 2);
	oPrint(244, 176, 132, '0', 3);
	oPrint(0, 112, 192, '0', 3);bk(1);
	cout << "\n";
	
	pSpace(6 + left);bk(1);
	oPrint(0, 112, 192, '0', 8);bk(1);
	oPrint(0, 112, 192, '0', 3);
	oPrint(244, 176, 132, '0', 1);
	oPrint(0, 112, 192, '0', 3);
	oPrint(68, 84, 106, '0', 1);bk(1);
	cout << "\n";
	
	pSpace(6 + left);bk(1);
	oPrint(0, 112, 192, '0', 8);bk(1);
	oPrint(0, 112, 192, '0', 3);
	oPrint(244, 176, 132, '0', 1);
	oPrint(0, 112, 192, '0', 2);
	oPrint(68, 84, 106, '0', 1);
	oPrint(0, 112, 192, '0', 2);bk(1);
	cout << "\n";
	
	pSpace(3 + left);bk(28);
	cout << "\n";
	
	pSpace(2 + left);bk(1);
	oPrint(255, 0, 0, '0', 28);bk(2);
	cout << "\n";
	
	pSpace(1 + left);bk(2);
	oPrint(255, 0, 0, '0', 30);bk(1);
	cout << "\n";
	
	pSpace(0 + left);bk(1);
	oPrint(255, 0, 0, '0', 3);bk(4);
	oPrint(255, 0, 0, '0', 16);bk(4);
	oPrint(255, 0, 0, '0', 6);bk(1);
	cout << "\n";
	
	pSpace(0 + left);bk(5);pSpace(2);bk(18);pSpace(2);bk(8);
	cout << "\n";
	
	pSpace(4 + left);bk(4);pSpace(16);bk(4);
	cout << "\n";
}

// 设置控制台大小 
void SetConsoleWindowSize( SHORT width, SHORT height ) {
    HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SMALL_RECT wrt = { 0, 0, width-1, height-1 };
    SetConsoleWindowInfo( hStdOutput, TRUE, &wrt ); // 设置窗体尺寸
    COORD coord = { width, height };
    SetConsoleScreenBufferSize( hStdOutput, coord ); // 设置缓冲尺寸
}
// 清除屏幕 
void clearScreen(){    
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = { 0, 0 };    // home for the cursor
    SetConsoleCursorPosition( hConsole, coordScreen );
}
// 隐藏光标 
void hiddenCursor() {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}

int main() {
	system("mode con cols=150 lines=20"); // 设置控制台初始大小 
	hiddenCursor(); // 隐藏光标 
	for(int i = 0; i < 100; i+=2) {
		pCar(i); // 画小汽车 
		Sleep(16); // 等待16毫秒 
		clearScreen(); // 清除屏幕 
	}
	getch(); 
    return 0;
}

网站公告

今日签到

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