Ubuntu 下C++数字雨

以前写过一个Window下的数字雨,像黑客帝国里那样的01数字,现在补充一版Linux下的。使用了curses库,安装方法与使用方法参照

Linux下curses函数库的详细介绍_libcurses库-CSDN博客

5-linux学习笔记之-----curses-CSDN博客

效果如下:

代码如下:

复制代码
#include <time.h>
#include <curses.h>
#include <stdio.h>
#include <chrono>
#include <thread>

typedef struct		//记录雨滴的结构体
{
    int x;
    int y;
    char ch;
}RAINDROP;

const int BUFFER_SIZE = 50;    //雨线数量
int WIDTH = 80;
int HEIGHT = 30;
const int RAIN_LENGTH = 18;     //雨线长度

RAINDROP raindropLine[BUFFER_SIZE];
WINDOW *HOUT = initscr();//获得标准输出的句柄

int main()
{
    if (has_colors() == TRUE)
    {
        start_color();  //初始化颜色显示
        init_pair(1, COLOR_RED, COLOR_WHITE);   //只能是颜色库里面8中颜色的组合
        init_pair(2, COLOR_BLUE, COLOR_GREEN);
        init_pair(3, COLOR_BLACK, COLOR_GREEN);
        init_pair(4, COLOR_GREEN, COLOR_BLACK);
    }

    HEIGHT = LINES;				//根据控制台的宽高设置显示的宽高
    WIDTH = COLS;

    noecho();
    srand((unsigned int)time(NULL));
    for (int i=0; i<BUFFER_SIZE; i++)			//随机设置雨滴下落的位置
    {
        raindropLine[i].x = rand()%WIDTH;
        raindropLine[i].y = rand()%HEIGHT;
        raindropLine[i].ch = rand() %2 + 48;				//设置雨滴内容0或1
    }

    while(true)
    {
        curs_set(0);
        //GetConsoleScreenBufferInfo(HOUT, &info);	//当窗体大小变化时,重新设置宽高信息
        HEIGHT = LINES;
        WIDTH = COLS;
        for (int i=0; i<BUFFER_SIZE; ++i)
        {
            if (raindropLine[i].y <= HEIGHT)
            {
                mvaddch(raindropLine[i].y, raindropLine[i].x, raindropLine[i].ch | COLOR_PAIR(4));  //设置雨滴颜色
            }

            mvaddch(raindropLine[i].y - RAIN_LENGTH, raindropLine[i].x, ' ');
            raindropLine[i].y++;
            raindropLine[i].ch = rand() % 2 + 48;
            if (raindropLine[i].y > HEIGHT + RAIN_LENGTH)
            {
                raindropLine[i].x = rand() % WIDTH;
                raindropLine[i].y = rand() % HEIGHT;
            }
            if ( raindropLine[i].y <= HEIGHT)
            {
                mvaddch( raindropLine[i].y, raindropLine[i].x, raindropLine[i].ch | COLOR_PAIR(4) | A_BOLD);  //高亮最下方的雨滴

            }
        }
        refresh();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    getchar();
    return 0;
}

编译命令

gcc main.cpp -o rain -lpthread -lcurses

相关推荐
羊小蜜.几秒前
C++17: map & multimap—— 键值映射容器
开发语言·c++·stl
i建模3 分钟前
Ubuntu 中使用 LVM(逻辑卷管理)挂载磁盘
linux·运维·ubuntu
tankeven11 分钟前
HJ156 走迷宫
c++·算法
cyber_两只龙宝12 分钟前
【Docker】Dockerfile构建镜像实验全流程详解
linux·运维·docker·云原生
de_wizard16 分钟前
Linux 下安装 Golang环境
linux·运维·golang
相醉为友18 分钟前
001 Linux个性操作记录——交叉编译工具链高兼容性调用函数备用
linux·运维·服务器
夹芯饼干27 分钟前
Linux命令(第三节课)
linux·运维·服务器
oushaojun241 分钟前
现代linux gpio编程接口(by deepseek)
linux·libgpiod
皓木.1 小时前
软件测试Linux
linux·运维·服务器
计算机安禾1 小时前
【数据结构与算法】第16篇:串(String)的定长顺序存储与朴素模式匹配
c语言·数据结构·c++·学习·算法·visual studio code·visual studio