鱼C论坛

 找回密码
 立即注册
查看: 1569|回复: 17

[已解决]实现一个行走的人,有大神挑战下吗

[复制链接]
发表于 2018-10-27 17:04:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

这个图片是百度来的,有没有大神能编写一个程序出来跟这个图片差不多

这个图片是百度来的,有没有大神能编写一个程序出来跟这个图片差不多
最佳答案
2018-11-8 13:38:32
#include "ximage.h"
#include <iostream>
#include <string>

static void HideCursor()
{
        CONSOLE_CURSOR_INFO cursor_info = {100, false};
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

static void GotoXY(SHORT x, SHORT y)
{
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{x, y});
}

// 字符与RGB的对应的映射关系
static char RgbToChar(uint8_t r, uint8_t g, uint8_t b)
{
        static const std::string ascii_char = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,"^`'. ";
        int gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b);
        double unit = (256.0 + 1) / ascii_char.size();
        return ascii_char[int(gray / unit)];
}

class AsciiGif
{
public:
        AsciiGif()
        {
                this->image = nullptr;
        }
        ~AsciiGif()
        {
                delete this->image;
        }

        bool Load(const std::string &filename)
        {
                if(this->image)
                        delete this->image;

                this->image = new CxImage;
                this->image->SetRetreiveAllFrames(true);
                this->image->Load(filename.c_str(), CXIMAGE_FORMAT_GIF);
                if(!this->image->IsValid())
                        return false;
                return true;
        }

        bool Scale(double w_scale, double h_scale)
        {
                if(!this->image)
                        return false;

                for(int32_t i = 0; i < this->image->GetNumFrames(); ++i)
                {
                        CxImage *frame = this->image->GetFrame(i);
                        frame->Resample(int32_t(frame->GetWidth() * w_scale), int32_t(frame->GetHeight() * h_scale));
                }
                return true;
        }

        std::string GetFrame(int32_t index) const
        {
                if(!this->image)
                        return std::string();
                if(index >= this->image->GetNumFrames())
                        return std::string();

                CxImage *frame = this->image->GetFrame(index);
                uint32_t width = frame->GetWidth();
                uint32_t height = frame->GetHeight();
                std::string buf;
                for(int32_t h = height - 1; h >= 0; --h)
                {
                        for(uint32_t w = 0; w < width; ++w)
                        {
                                RGBQUAD color = frame->GetPixelColor(w, h);
                                buf += RgbToChar(color.rgbRed, color.rgbGreen, color.rgbBlue);
                        }
                        buf += '\n';
                }
                return buf;
        }

        int32_t GetNumFrames() const
        {
                return this->image->GetNumFrames();
        }

        uint32_t GetFrameDelay() const
        {
                return this->image->GetFrameDelay();
        }

private:
        CxImage *image;
};

int main()
{
        AsciiGif gif;
        gif.Load("src\\1.gif");
        gif.Scale(0.5 * 1.4, 0.25 * 1.4);
        
        HideCursor();                // CxImage内部设置过显示光标,在这里覆盖这个设置
        while(1)
        {
                for(int32_t i = 0; i < gif.GetNumFrames(); ++i)
                {
                        std::string frame = gif.GetFrame(i);
                        puts(frame.c_str());                        // std::cout太慢了,这里用puts
                        Sleep(gif.GetFrameDelay() * 10);
                        GotoXY(0, 0);
                }
        }
        return 0;
}

GIF.gif

完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-27 20:15:41 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-27 21:38:04 | 显示全部楼层
这个原理简单,但是文本的问题麻烦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 13:38:32 | 显示全部楼层    本楼为最佳答案   
#include "ximage.h"
#include <iostream>
#include <string>

static void HideCursor()
{
        CONSOLE_CURSOR_INFO cursor_info = {100, false};
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

static void GotoXY(SHORT x, SHORT y)
{
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{x, y});
}

// 字符与RGB的对应的映射关系
static char RgbToChar(uint8_t r, uint8_t g, uint8_t b)
{
        static const std::string ascii_char = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,"^`'. ";
        int gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b);
        double unit = (256.0 + 1) / ascii_char.size();
        return ascii_char[int(gray / unit)];
}

class AsciiGif
{
public:
        AsciiGif()
        {
                this->image = nullptr;
        }
        ~AsciiGif()
        {
                delete this->image;
        }

        bool Load(const std::string &filename)
        {
                if(this->image)
                        delete this->image;

                this->image = new CxImage;
                this->image->SetRetreiveAllFrames(true);
                this->image->Load(filename.c_str(), CXIMAGE_FORMAT_GIF);
                if(!this->image->IsValid())
                        return false;
                return true;
        }

        bool Scale(double w_scale, double h_scale)
        {
                if(!this->image)
                        return false;

                for(int32_t i = 0; i < this->image->GetNumFrames(); ++i)
                {
                        CxImage *frame = this->image->GetFrame(i);
                        frame->Resample(int32_t(frame->GetWidth() * w_scale), int32_t(frame->GetHeight() * h_scale));
                }
                return true;
        }

        std::string GetFrame(int32_t index) const
        {
                if(!this->image)
                        return std::string();
                if(index >= this->image->GetNumFrames())
                        return std::string();

                CxImage *frame = this->image->GetFrame(index);
                uint32_t width = frame->GetWidth();
                uint32_t height = frame->GetHeight();
                std::string buf;
                for(int32_t h = height - 1; h >= 0; --h)
                {
                        for(uint32_t w = 0; w < width; ++w)
                        {
                                RGBQUAD color = frame->GetPixelColor(w, h);
                                buf += RgbToChar(color.rgbRed, color.rgbGreen, color.rgbBlue);
                        }
                        buf += '\n';
                }
                return buf;
        }

        int32_t GetNumFrames() const
        {
                return this->image->GetNumFrames();
        }

        uint32_t GetFrameDelay() const
        {
                return this->image->GetFrameDelay();
        }

private:
        CxImage *image;
};

int main()
{
        AsciiGif gif;
        gif.Load("src\\1.gif");
        gif.Scale(0.5 * 1.4, 0.25 * 1.4);
        
        HideCursor();                // CxImage内部设置过显示光标,在这里覆盖这个设置
        while(1)
        {
                for(int32_t i = 0; i < gif.GetNumFrames(); ++i)
                {
                        std::string frame = gif.GetFrame(i);
                        puts(frame.c_str());                        // std::cout太慢了,这里用puts
                        Sleep(gif.GetFrameDelay() * 10);
                        GotoXY(0, 0);
                }
        }
        return 0;
}

GIF.gif

完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 17:52:58 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-11-8 13:38
完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82

牛逼,原理是啥?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 17:57:13 | 显示全部楼层
人造人 发表于 2018-11-8 13:38
完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82

卧槽  大佬牛逼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 19:05:20 | 显示全部楼层
人造人 发表于 2018-11-8 13:38
完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82

厉害了,感觉用像素点代替,这不就是动画嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 19:28:38 | 显示全部楼层
人造人 发表于 2018-11-8 13:38
完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82

大佬牛逼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 21:31:13 | 显示全部楼层
千公子 发表于 2018-11-8 17:52
牛逼,原理是啥?

gif就是好多的图片组成一个文件
这个程序就是循环显示每一张图片
核心原理就是把像素对应的颜色转换成字符
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 21:32:20 | 显示全部楼层
ssxx43 发表于 2018-11-8 19:05
厉害了,感觉用像素点代替,这不就是动画嘛

嗯,用相同的原理完全可以把视频字符化
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-10 10:31:01 | 显示全部楼层
six  six   six·····6了 6了···
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-30 00:48:10 | 显示全部楼层
人造人 发表于 2018-11-8 13:38
完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82

麻烦问下你用的什么软件打开的啊 我用dev和vc打开总找不到自定义那些头文件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-30 00:51:34 | 显示全部楼层
GAI_0205 发表于 2018-11-30 00:48
麻烦问下你用的什么软件打开的啊 我用dev和vc打开总找不到自定义那些头文件

vs2017
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-30 01:08:13 | 显示全部楼层

直接打开main的cpp文件吗 有很多其他的附属文件会自己跳出来吗刚学c
没多久不是很懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-30 01:21:20 | 显示全部楼层
GAI_0205 发表于 2018-11-30 01:08
直接打开main的cpp文件吗 有很多其他的附属文件会自己跳出来吗刚学c
没多久不是很懂

你用的是vs2017 ?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-30 23:10:24 | 显示全部楼层

我刚下了vs2017,打开那个cxlmage后运行时打开不了那些.h源文件  怎么把它放进去啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-1 11:27:37 | 显示全部楼层
GAI_0205 发表于 2018-11-30 23:10
我刚下了vs2017,打开那个cxlmage后运行时打开不了那些.h源文件  怎么把它放进去啊

直接打开 CxImage.sln
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-1 12:46:59 | 显示全部楼层
厉害啊。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 00:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表