鱼C论坛

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

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

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

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

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

x

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

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

  4. static void HideCursor()
  5. {
  6.         CONSOLE_CURSOR_INFO cursor_info = {100, false};
  7.         SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  8. }

  9. static void GotoXY(SHORT x, SHORT y)
  10. {
  11.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{x, y});
  12. }

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

  21. class AsciiGif
  22. {
  23. public:
  24.         AsciiGif()
  25.         {
  26.                 this->image = nullptr;
  27.         }
  28.         ~AsciiGif()
  29.         {
  30.                 delete this->image;
  31.         }

  32.         bool Load(const std::string &filename)
  33.         {
  34.                 if(this->image)
  35.                         delete this->image;

  36.                 this->image = new CxImage;
  37.                 this->image->SetRetreiveAllFrames(true);
  38.                 this->image->Load(filename.c_str(), CXIMAGE_FORMAT_GIF);
  39.                 if(!this->image->IsValid())
  40.                         return false;
  41.                 return true;
  42.         }

  43.         bool Scale(double w_scale, double h_scale)
  44.         {
  45.                 if(!this->image)
  46.                         return false;

  47.                 for(int32_t i = 0; i < this->image->GetNumFrames(); ++i)
  48.                 {
  49.                         CxImage *frame = this->image->GetFrame(i);
  50.                         frame->Resample(int32_t(frame->GetWidth() * w_scale), int32_t(frame->GetHeight() * h_scale));
  51.                 }
  52.                 return true;
  53.         }

  54.         std::string GetFrame(int32_t index) const
  55.         {
  56.                 if(!this->image)
  57.                         return std::string();
  58.                 if(index >= this->image->GetNumFrames())
  59.                         return std::string();

  60.                 CxImage *frame = this->image->GetFrame(index);
  61.                 uint32_t width = frame->GetWidth();
  62.                 uint32_t height = frame->GetHeight();
  63.                 std::string buf;
  64.                 for(int32_t h = height - 1; h >= 0; --h)
  65.                 {
  66.                         for(uint32_t w = 0; w < width; ++w)
  67.                         {
  68.                                 RGBQUAD color = frame->GetPixelColor(w, h);
  69.                                 buf += RgbToChar(color.rgbRed, color.rgbGreen, color.rgbBlue);
  70.                         }
  71.                         buf += '\n';
  72.                 }
  73.                 return buf;
  74.         }

  75.         int32_t GetNumFrames() const
  76.         {
  77.                 return this->image->GetNumFrames();
  78.         }

  79.         uint32_t GetFrameDelay() const
  80.         {
  81.                 return this->image->GetFrameDelay();
  82.         }

  83. private:
  84.         CxImage *image;
  85. };

  86. int main()
  87. {
  88.         AsciiGif gif;
  89.         gif.Load("src\\1.gif");
  90.         gif.Scale(0.5 * 1.4, 0.25 * 1.4);
  91.        
  92.         HideCursor();                // CxImage内部设置过显示光标,在这里覆盖这个设置
  93.         while(1)
  94.         {
  95.                 for(int32_t i = 0; i < gif.GetNumFrames(); ++i)
  96.                 {
  97.                         std::string frame = gif.GetFrame(i);
  98.                         puts(frame.c_str());                        // std::cout太慢了,这里用puts
  99.                         Sleep(gif.GetFrameDelay() * 10);
  100.                         GotoXY(0, 0);
  101.                 }
  102.         }
  103.         return 0;
  104. }
复制代码


GIF.gif

完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-27 20:15:41 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-27 21:38:04 | 显示全部楼层
这个原理简单,但是文本的问题麻烦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  4. static void HideCursor()
  5. {
  6.         CONSOLE_CURSOR_INFO cursor_info = {100, false};
  7.         SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  8. }

  9. static void GotoXY(SHORT x, SHORT y)
  10. {
  11.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{x, y});
  12. }

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

  21. class AsciiGif
  22. {
  23. public:
  24.         AsciiGif()
  25.         {
  26.                 this->image = nullptr;
  27.         }
  28.         ~AsciiGif()
  29.         {
  30.                 delete this->image;
  31.         }

  32.         bool Load(const std::string &filename)
  33.         {
  34.                 if(this->image)
  35.                         delete this->image;

  36.                 this->image = new CxImage;
  37.                 this->image->SetRetreiveAllFrames(true);
  38.                 this->image->Load(filename.c_str(), CXIMAGE_FORMAT_GIF);
  39.                 if(!this->image->IsValid())
  40.                         return false;
  41.                 return true;
  42.         }

  43.         bool Scale(double w_scale, double h_scale)
  44.         {
  45.                 if(!this->image)
  46.                         return false;

  47.                 for(int32_t i = 0; i < this->image->GetNumFrames(); ++i)
  48.                 {
  49.                         CxImage *frame = this->image->GetFrame(i);
  50.                         frame->Resample(int32_t(frame->GetWidth() * w_scale), int32_t(frame->GetHeight() * h_scale));
  51.                 }
  52.                 return true;
  53.         }

  54.         std::string GetFrame(int32_t index) const
  55.         {
  56.                 if(!this->image)
  57.                         return std::string();
  58.                 if(index >= this->image->GetNumFrames())
  59.                         return std::string();

  60.                 CxImage *frame = this->image->GetFrame(index);
  61.                 uint32_t width = frame->GetWidth();
  62.                 uint32_t height = frame->GetHeight();
  63.                 std::string buf;
  64.                 for(int32_t h = height - 1; h >= 0; --h)
  65.                 {
  66.                         for(uint32_t w = 0; w < width; ++w)
  67.                         {
  68.                                 RGBQUAD color = frame->GetPixelColor(w, h);
  69.                                 buf += RgbToChar(color.rgbRed, color.rgbGreen, color.rgbBlue);
  70.                         }
  71.                         buf += '\n';
  72.                 }
  73.                 return buf;
  74.         }

  75.         int32_t GetNumFrames() const
  76.         {
  77.                 return this->image->GetNumFrames();
  78.         }

  79.         uint32_t GetFrameDelay() const
  80.         {
  81.                 return this->image->GetFrameDelay();
  82.         }

  83. private:
  84.         CxImage *image;
  85. };

  86. int main()
  87. {
  88.         AsciiGif gif;
  89.         gif.Load("src\\1.gif");
  90.         gif.Scale(0.5 * 1.4, 0.25 * 1.4);
  91.        
  92.         HideCursor();                // CxImage内部设置过显示光标,在这里覆盖这个设置
  93.         while(1)
  94.         {
  95.                 for(int32_t i = 0; i < gif.GetNumFrames(); ++i)
  96.                 {
  97.                         std::string frame = gif.GetFrame(i);
  98.                         puts(frame.c_str());                        // std::cout太慢了,这里用puts
  99.                         Sleep(gif.GetFrameDelay() * 10);
  100.                         GotoXY(0, 0);
  101.                 }
  102.         }
  103.         return 0;
  104. }
复制代码


GIF.gif

完整源码
链接:https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

牛逼,原理是啥?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

卧槽  大佬牛逼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

厉害了,感觉用像素点代替,这不就是动画嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

大佬牛逼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

gif就是好多的图片组成一个文件
这个程序就是循环显示每一张图片
核心原理就是把像素对应的颜色转换成字符
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

嗯,用相同的原理完全可以把视频字符化
^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-10 10:31:01 | 显示全部楼层
six  six   six·····6了 6了···
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

麻烦问下你用的什么软件打开的啊 我用dev和vc打开总找不到自定义那些头文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

vs2017
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

直接打开main的cpp文件吗 有很多其他的附属文件会自己跳出来吗刚学c
没多久不是很懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

你用的是vs2017 ?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

我刚下了vs2017,打开那个cxlmage后运行时打开不了那些.h源文件  怎么把它放进去啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

直接打开 CxImage.sln
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-1 12:46:59 | 显示全部楼层
厉害啊。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-13 20:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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