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

相关推荐
用户8055336980317 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK1 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
AlfredZhao2 天前
生产环境里,为什么不建议把普通端口直接暴露到公网?
linux·https·443·80
卷无止境2 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境2 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴3 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
戴为沐3 天前
Linux内存扩容指南
linux
zylyehuo3 天前
Linux 彻底且安全地删除文件
linux
用户805533698034 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297914 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux