C语言中的动画文本图像

C语言中的动画文本图像,c,command-line,animation,C,Command Line,Animation,我必须用C语言为一个类编写一个文本图像程序。没什么复杂的,只是用文字写一个图像 关于这个:tower.blinkenlights.nl,我如何设置我的文本动画?这是什么平台? 如果你想要多平台的东西,我建议你使用 如果只是针对Windows,您可能可以使用conio.h和graphics.h等。 我发现其中似乎有一些基本的绘图/动画的例子在C中使用一些窗口标题。请注意,这是非常不可移植的代码,并且是特定于Windows的 更新:我还发现了一些使用图形的例子。这是什么平台? 如果你想要多平台的东西

我必须用C语言为一个类编写一个文本图像程序。没什么复杂的,只是用文字写一个图像

关于这个:tower.blinkenlights.nl,我如何设置我的文本动画?

这是什么平台? 如果你想要多平台的东西,我建议你使用

如果只是针对Windows,您可能可以使用conio.h和graphics.h等。 我发现其中似乎有一些基本的绘图/动画的例子在C中使用一些窗口标题。请注意,这是非常不可移植的代码,并且是特定于Windows的

更新:我还发现了一些使用图形的例子。这是什么平台? 如果你想要多平台的东西,我建议你使用

如果只是针对Windows,您可能可以使用conio.h和graphics.h等。 我发现其中似乎有一些基本的绘图/动画的例子在C中使用一些窗口标题。请注意,这是非常不可移植的代码,并且是特定于Windows的


更新:我还发现其中包括一些使用图形的示例。h

如果允许使用外部库,看起来会有所帮助。如果不是,您可能会使用system()函数并传递“cls”(我假设这里是Windows。对于Linux或Mac,使用“clear”):

#包括
#ifdef WIN32
系统(“cls”);
#否则
系统(“清除”);
#恩迪夫

在那之后,打印出一帧并等待下一帧打印出来应该是一项简单的工作。

如果允许使用外部库,看起来会有所帮助。如果不是,您可能会使用system()函数并传递“cls”(我假设这里是Windows。对于Linux或Mac,使用“clear”):

#包括
#ifdef WIN32
系统(“cls”);
#否则
系统(“清除”);
#恩迪夫

在此之后,打印出一帧并等待下一帧打印出来应该是一项简单的工作。

下面是一个示例,向您展示如何制作动画。这就是《星球大战》的动画。这是一个巨大的文本文件,分为几个单独的帧。请阅读网站上的常见问题以了解更多信息

一般的想法是你有一套框架。每个帧在动画中包含一个图像。清除屏幕,显示一帧,然后等待一段时间。反复这样做,你就会有一个文本动画

因此,首先要创建一个带有框架的文件。称之为frames.txt。为了允许可变长度的帧,我们在每个帧后面都有一条以@开头的线后跟该帧应保留在屏幕上的动画标记数。例如,如果以每秒15帧的速度绘制,且线条为@!15,则帧将在屏幕上显示1秒(15个刻度)

然后编译并运行这个程序。在Linux或OSX上,我只需将其保存为text_animate.cpp并运行make text_animate。在Windows上,也许你唯一需要做的就是将显示system(“clear”)的行改为system(“cls”),但我不确定

#include <iostream>
#include <string>
#include <ctime>
#include <fstream>

const char *FRAME_DELIM = "@!";
unsigned int FRAME_DELIM_LEN = strlen(FRAME_DELIM);

int main(int argc, char *argv[]) {
    if( argc != 2 ) {
        std::cout << "Usage: text_animate <frames file>\n";
        exit(1);
    }
    std::string frames_fn = argv[1];
    struct timespec sleep_time = {0, 1000000000 / 15}; // 15 fps

    std::ifstream file_stream;
    file_stream.open(frames_fn.c_str(), std::ios::in);
    if( !file_stream.is_open() ) {
        std::cout << "Couldn't open [" << frames_fn.c_str() << "]\n";
        return -1;
    }

    std::string frame_line;
    unsigned int frame_ticks = 0;
    while( true ) {
            system("clear");

            bool is_frame_delim = false;
            do {
                getline(file_stream, frame_line);
                if( file_stream.fail() && file_stream.eof()) {
                    file_stream.clear();
                    file_stream.seekg(0, std::ios::beg);

                    getline(file_stream, frame_line);
                }
                else if( file_stream.fail() ) {
                    std::cout << "Error reading from file.\n";
                    break;
                }

                is_frame_delim = strncmp(frame_line.c_str(),FRAME_DELIM,
                                         FRAME_DELIM_LEN) == 0;
                if( !is_frame_delim ) {
                    std::cout << frame_line << "\n";
                }
            } while( !is_frame_delim );

        frame_ticks = atoi(&(frame_line.c_str()[FRAME_DELIM_LEN + 1]));

        while( frame_ticks-- > 0 ) {
                    nanosleep(&sleep_time, NULL);
        }
    }

    file_stream.close();

    return 0;
}
#包括
#包括
#包括
#包括
const char*FRAME_DELIM=“@!”;
无符号int FRAME_DELIM_LEN=strlen(FRAME_DELIM);
int main(int argc,char*argv[]){
如果(argc!=2){

std::cout这里有一个示例向您展示如何制作动画。这正是《星球大战》的动画。它是一个巨大的文本文件,分为多个帧。请阅读网站上的常见问题解答以了解更多信息

一般的想法是,您有一组帧。每个帧在动画中包含一个图像。您可以清除屏幕,显示一个帧,然后等待一段时间。反复这样做,您就有了一个文本动画

因此,首先您要使用帧创建一个文件。将其命名为frames.txt。为了允许可变长度的帧,我们在每个帧后面加一条线,该线以@开头,然后是该帧应留在屏幕上的动画标记数。例如,如果您以每秒15帧的速度绘制,且该线为@!15,则该帧将在屏幕上显示1秒(15个刻度)

然后编译并运行这个程序。在Linux或OSX上,我只需将它保存为text_animate.cpp并运行make text_animate。在Windows上,您可能需要做的唯一一件事是将显示system(“clear”)的行更改为system(“cls”),但我不确定

#include <iostream>
#include <string>
#include <ctime>
#include <fstream>

const char *FRAME_DELIM = "@!";
unsigned int FRAME_DELIM_LEN = strlen(FRAME_DELIM);

int main(int argc, char *argv[]) {
    if( argc != 2 ) {
        std::cout << "Usage: text_animate <frames file>\n";
        exit(1);
    }
    std::string frames_fn = argv[1];
    struct timespec sleep_time = {0, 1000000000 / 15}; // 15 fps

    std::ifstream file_stream;
    file_stream.open(frames_fn.c_str(), std::ios::in);
    if( !file_stream.is_open() ) {
        std::cout << "Couldn't open [" << frames_fn.c_str() << "]\n";
        return -1;
    }

    std::string frame_line;
    unsigned int frame_ticks = 0;
    while( true ) {
            system("clear");

            bool is_frame_delim = false;
            do {
                getline(file_stream, frame_line);
                if( file_stream.fail() && file_stream.eof()) {
                    file_stream.clear();
                    file_stream.seekg(0, std::ios::beg);

                    getline(file_stream, frame_line);
                }
                else if( file_stream.fail() ) {
                    std::cout << "Error reading from file.\n";
                    break;
                }

                is_frame_delim = strncmp(frame_line.c_str(),FRAME_DELIM,
                                         FRAME_DELIM_LEN) == 0;
                if( !is_frame_delim ) {
                    std::cout << frame_line << "\n";
                }
            } while( !is_frame_delim );

        frame_ticks = atoi(&(frame_line.c_str()[FRAME_DELIM_LEN + 1]));

        while( frame_ticks-- > 0 ) {
                    nanosleep(&sleep_time, NULL);
        }
    }

    file_stream.close();

    return 0;
}
#包括
#包括
#包括
#包括
const char*FRAME_DELIM=“@!”;
无符号int FRAME_DELIM_LEN=strlen(FRAME_DELIM);
int main(int argc,char*argv[]){
如果(argc!=2){

std::也许你应该尝试更详细地解释一下你想要实现什么以及问题是什么。我必须编写一个将文本显示为图像的程序。比如printf(“******\n”);printf(“*********\n”);printf(******\n”);printf(***\n”);等等。但是我很好奇如何设置文本的动画,就像这个家伙打开你的终端并在:tower.blinkenlights.nlto hlovdal的point:Terminal(有或没有诅咒?)或gui(哪一个?Xlib?、Qt?、SVGAlib?Windows?)中键入的那样?该链接不起作用。还有@hlovdal所说的。
$tower.blinkenlights.nl-bash:tower.blinkenlights.nl:未找到命令
可能您应该尝试更详细地解释一下您想要实现的目标和问题所在。我必须编写一个将文本显示为图像的程序。如printf(“********\n”);printf(“******\n”);printf(“***\n”);等等。但是我很好奇如何设置文本动画,就像这个家伙打开终端并键入:tower.blinkenlights.nlto hlovdal的要点:终端(带或不带诅咒?)或gui(哪一个?Xlib?、Qt?、SVGAlib?Windows?)?该链接不起作用。还有@hlovdal所说的。
$tower.blinkenlights.nl-bash:tower.blinkenlights.nl:未找到命令
我不希望显示图形,但使用文本字符使其看起来像图形。ncurses和conio正是用于此目的。嗯,不是ju
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>

const char *FRAME_DELIM = "@!";
unsigned int FRAME_DELIM_LEN = strlen(FRAME_DELIM);

int main(int argc, char *argv[]) {
    if( argc != 2 ) {
        std::cout << "Usage: text_animate <frames file>\n";
        exit(1);
    }
    std::string frames_fn = argv[1];
    struct timespec sleep_time = {0, 1000000000 / 15}; // 15 fps

    std::ifstream file_stream;
    file_stream.open(frames_fn.c_str(), std::ios::in);
    if( !file_stream.is_open() ) {
        std::cout << "Couldn't open [" << frames_fn.c_str() << "]\n";
        return -1;
    }

    std::string frame_line;
    unsigned int frame_ticks = 0;
    while( true ) {
            system("clear");

            bool is_frame_delim = false;
            do {
                getline(file_stream, frame_line);
                if( file_stream.fail() && file_stream.eof()) {
                    file_stream.clear();
                    file_stream.seekg(0, std::ios::beg);

                    getline(file_stream, frame_line);
                }
                else if( file_stream.fail() ) {
                    std::cout << "Error reading from file.\n";
                    break;
                }

                is_frame_delim = strncmp(frame_line.c_str(),FRAME_DELIM,
                                         FRAME_DELIM_LEN) == 0;
                if( !is_frame_delim ) {
                    std::cout << frame_line << "\n";
                }
            } while( !is_frame_delim );

        frame_ticks = atoi(&(frame_line.c_str()[FRAME_DELIM_LEN + 1]));

        while( frame_ticks-- > 0 ) {
                    nanosleep(&sleep_time, NULL);
        }
    }

    file_stream.close();

    return 0;
}