数字瀑布

2019-03-14  本文已影响0人  Jingtianer

马上要到五四述职了,班长想做一个类似黑客帝国的代码流,经过一上午的百度,终于东拼西凑出了这份代码

运行环境

项目 信息
编译环境 visual studio 2017
测试平台 windows 10 (x64)
CPU Intel(R) Core(TM) i5-7300HQ
内存 8GB

代码

#include "pch.h"
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <stdarg.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
HANDLE handle = []() { return GetStdHandle(STD_OUTPUT_HANDLE); }();
typedef struct
{
    int x, y;
    char ch;
} STU;
STU st[100];
//出现位置

void gotoxy(int x, int y)
{
    HANDLE hout;
    COORD pos;
    pos.X = x;
    pos.Y = y;

    hout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, pos);
}

/*隐藏光标*/
void show_cursor(int hide)
{
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hout;

    hout = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hout, &cciCursor))
    {
        cciCursor.bVisible = hide;
        SetConsoleCursorInfo(hout, &cciCursor);
    }
}

/*设置颜色*/
void set_color(int color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void full_screen()
{
    HWND hwnd = GetForegroundWindow();
    int cx = GetSystemMetrics(SM_CXSCREEN);                              /* 屏幕宽度 像素 */
    int cy = GetSystemMetrics(SM_CYSCREEN);                              /* 屏幕高度 像素 */
    LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE); /* 获取窗口信息 */ /* 设置窗口信息 最大化 取消标题栏及边框 */
    SetWindowLong(hwnd, GWL_STYLE, (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
    SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}
void cprintf(char str, WORD color)
{
    //setFont(size);

    Sleep(200);

    WORD colorOld;
    HANDLE handle = ::GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(handle, &csbi);
    colorOld = csbi.wAttributes;
    SetConsoleTextAttribute(handle, color);
    cout << str;
    SetConsoleTextAttribute(handle, colorOld);
}
void setFont(int size)
{
    COORD FontSize = { size, size * 2 };
    CONSOLE_FONT_INFOEX font = { 0 };
    GetCurrentConsoleFontEx(handle, true, &font);
    font.dwFontSize = FontSize;
    font.cbSize = sizeof(CONSOLE_FONT_INFOEX);
    SetCurrentConsoleFontEx(handle, true, &font);
}
int main()
{
    full_screen();
    system("pause");
    system("cls");

    setFont(45);
    string str = "Welcome to CS 1802\n";
    for (int i = 0; i < str.length(); i++)
    {
        cprintf(str[i], 2);
    }
    system("color");
    system("cls");
    setFont(35);

    int i, j;
    show_cursor(0);
    srand(time(NULL));
    //初始化结构体
    for (i = 0; i < 100; i++)
    {
        st[i].x = rand() % 80;
        st[i].y = rand() % 20;
        st[i].ch = rand() % (49 - 47) + 48;
    }
    while (1)
    {
        for (i = 0; i < 100; i++)
        {
            gotoxy(st[i].x, st[i].y);
            set_color(0x2);
            putchar(st[i].ch);
            gotoxy(st[i].x, st[i].y - 5);
            putchar(' ');
            st[i].y++;
            st[i].ch = rand() % (49 - 47) + 48;
            if (st[i].y - 5 >= 18)
            {
                gotoxy(st[i].x, st[i].y - 1);
                putchar(' ');
                gotoxy(st[i].x, st[i].y - 2);
                putchar(' ');
                gotoxy(st[i].x, st[i].y - 3);
                putchar(' ');
                gotoxy(st[i].x, st[i].y - 4);
                putchar(' ');
                gotoxy(st[i].x, st[i].y - 4);
                putchar(' ');
            }
            if (st[i].y > 23)
            {
                st[i].x = rand() % 80;
                st[i].y = rand() % 20;
            }
            gotoxy(st[i].x, st[i].y);
            set_color(0xA);
            putchar(st[i].ch);
        }
        Sleep(120);
    }
    return 0;
}

来源

c语言实现数字雨

C++控制台输出彩色文字

今日收获

  1. 学会了c/c++下的可变参数写法,不知道能不能自己实现一个printf。
  2. 没了hhh,我大概就是那种只需要ctrl c v三个键的程序员吧...
上一篇下一篇

猜你喜欢

热点阅读