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

用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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI麥爾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值