首页 > 代码库 > win7 dos窗口模拟帧刷新
win7 dos窗口模拟帧刷新
前几天是白色情人节,临时脑抽写了个表白神器 高端大气上档次,就是不知道该送给谁,经过两天的反射弧思考决定还是写给博客娘吧。- -~
这个程序就是打开后,在Dos窗口内模拟写出几行字母。其实主要就是模拟一下帧刷新。像游戏引擎那样每一帧都打印一个满屏,那么所有的动画也就出来了。想在里边写个小游戏也未尝不可。
但是理想丰满现实骨感,Dos窗口不断帧刷新效果很一般,是不是会出现跳动的情况,很蛋疼。
win7 dos窗口的默认宽度是80个字符,高度是25个字符。经过简单的处理,通过调用Sleep函数来控制间隔时间来无限打印80x25的字符串集合,就能模拟帧刷新了,非常的简单。
c/c++版本代码
#include <stdio.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <windows.h> #include <vector> using namespace std; //全局变量 标记是否处于update状态 bool update_flag = false;
//全局数组,每一帧打印的所有字符 char* screen[25];
//全局变量,打印刷新计数器 int pro_count = 0; //结构体 代表80x25中的一个点 struct _pos { int _x; int _y; _pos(int x, int y) : _x(x), _y(y){} };
//刷新函数的函数指针 typedef void(*_update)(vector<_pos>&); _pos makepos(int x, int y) { _pos ret(x, y); return ret; } //初始化要打印的点阵 虽然屏幕是80个字符的宽度,但是字符要多一个,原因是\0的字符串结尾标识符占一个
//最后一行做特殊处理 没有\n换行符,以免每一帧的时候窗口被向下延伸一行 void init_screen() { for (int i = 0; i < 25; i++) { screen[i] = (char*)malloc(sizeof(char)* 81); memset(screen[i], ‘ ‘, sizeof(char)* 81); //if (i == 0) screen[i][0] = ‘x‘; screen[i][79] = ‘\n‘; screen[i][80] = ‘\0‘; if (i == 24) { screen[i][79] = ‘\0‘; screen[i][80] = ‘\0‘; } } }
//打印一帧的屏幕 上边缺失的换行符在这里补充 void display_screen() { printf("\n"); for (int i = 0; i < 25; i++) { printf(screen[i]); } }
//更改某一点的显示字符 void set_char(int x, int y, char target) { if (x >= 0 && x < 25 && y >= 0 && y < 81) { char* index = (screen[x] + y); *index = target; } }
//开始update函数 void start_update(_update update_function, int daley, vector<_pos>& _vec) { update_flag = true; while (update_flag) { Sleep(daley); update_function(_vec); } }
//停止update void stop_update() { update_flag = false; }
//每一帧执行的函数 void processing(vector<_pos>& _vec) { if (pro_count < _vec.size()) set_char(_vec[pro_count]._x, _vec[pro_count]._y, ‘o‘); display_screen(); pro_count++; }
//制作一个序列,这个序列用来保存需要 更改的点 void init_vec(vector<_pos>& vec) { vec.push_back(makepos(10, 5)); vec.push_back(makepos(11, 5)); vec.push_back(makepos(12, 5)); vec.push_back(makepos(13, 5)); vec.push_back(makepos(14, 5)); vec.push_back(makepos(10, 9)); vec.push_back(makepos(11, 9)); vec.push_back(makepos(12, 9)); vec.push_back(makepos(13, 9)); vec.push_back(makepos(14, 9)); vec.push_back(makepos(14, 10)); vec.push_back(makepos(14, 11)); vec.push_back(makepos(14, 12)); vec.push_back(makepos(14, 13)); vec.push_back(makepos(10, 18)); vec.push_back(makepos(11, 17)); vec.push_back(makepos(12, 16)); vec.push_back(makepos(13, 17)); vec.push_back(makepos(14, 18)); vec.push_back(makepos(14, 19)); vec.push_back(makepos(14, 20)); vec.push_back(makepos(14, 21)); vec.push_back(makepos(13, 22)); vec.push_back(makepos(12, 23)); vec.push_back(makepos(11, 22)); vec.push_back(makepos(10, 21)); vec.push_back(makepos(10, 20)); vec.push_back(makepos(10, 19)); vec.push_back(makepos(10, 25)); vec.push_back(makepos(11, 26)); vec.push_back(makepos(12, 27)); vec.push_back(makepos(13, 28)); vec.push_back(makepos(14, 29)); vec.push_back(makepos(13, 30)); vec.push_back(makepos(12, 31)); vec.push_back(makepos(11, 32)); vec.push_back(makepos(10, 33)); vec.push_back(makepos(10, 37)); vec.push_back(makepos(10, 38)); vec.push_back(makepos(10, 39)); vec.push_back(makepos(10, 40)); vec.push_back(makepos(10, 41)); vec.push_back(makepos(10, 42)); vec.push_back(makepos(11, 37)); vec.push_back(makepos(12, 37)); vec.push_back(makepos(12, 38)); vec.push_back(makepos(12, 39)); vec.push_back(makepos(12, 40)); vec.push_back(makepos(12, 41)); vec.push_back(makepos(12, 42)); vec.push_back(makepos(13, 37)); vec.push_back(makepos(14, 37)); vec.push_back(makepos(14, 38)); vec.push_back(makepos(14, 39)); vec.push_back(makepos(14, 40)); vec.push_back(makepos(14, 41)); vec.push_back(makepos(14, 42)); vec.push_back(makepos(10, 49)); vec.push_back(makepos(11, 50)); vec.push_back(makepos(12, 51)); vec.push_back(makepos(10, 53)); vec.push_back(makepos(11, 52)); vec.push_back(makepos(12, 51)); vec.push_back(makepos(13, 51)); vec.push_back(makepos(14, 51)); vec.push_back(makepos(10, 59)); vec.push_back(makepos(11, 58)); vec.push_back(makepos(12, 57)); vec.push_back(makepos(13, 58)); vec.push_back(makepos(14, 59)); vec.push_back(makepos(14, 60)); vec.push_back(makepos(14, 61)); vec.push_back(makepos(14, 62)); vec.push_back(makepos(13, 63)); vec.push_back(makepos(12, 64)); vec.push_back(makepos(11, 63)); vec.push_back(makepos(10, 62)); vec.push_back(makepos(10, 61)); vec.push_back(makepos(10, 60)); vec.push_back(makepos(10, 67)); vec.push_back(makepos(11, 67)); vec.push_back(makepos(12, 67)); vec.push_back(makepos(13, 67)); vec.push_back(makepos(14, 68)); vec.push_back(makepos(14, 69)); vec.push_back(makepos(14, 70)); vec.push_back(makepos(14, 71)); vec.push_back(makepos(13, 72)); vec.push_back(makepos(12, 72)); vec.push_back(makepos(11, 72)); vec.push_back(makepos(10, 72)); } int main(int argc, char* argv[]) { vector<_pos> poses; init_vec(poses); init_screen(); _update _s_fun = processing; start_update(_s_fun, 100, poses); system("pause"); }
下面是C#版本 C#版本追加了颜色变化效果,更浪♂van
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace confession { class Program { static void Main(string[] args) { InitScreen(); DisplayScreen(); List<Pos> poses = new List<Pos>(); InitPoses(poses); Update(100, poses); Console.ReadKey(); } static void InitPoses(List<Pos> poses) { poses.Add(Pos.MakePos(10, 5)); poses.Add(Pos.MakePos(11, 5)); poses.Add(Pos.MakePos(12, 5)); poses.Add(Pos.MakePos(13, 5)); poses.Add(Pos.MakePos(14, 5)); poses.Add(Pos.MakePos(10, 9)); poses.Add(Pos.MakePos(11, 9)); poses.Add(Pos.MakePos(12, 9)); poses.Add(Pos.MakePos(13, 9)); poses.Add(Pos.MakePos(14, 9)); poses.Add(Pos.MakePos(14, 10)); poses.Add(Pos.MakePos(14, 11)); poses.Add(Pos.MakePos(14, 12)); poses.Add(Pos.MakePos(14, 13)); poses.Add(Pos.MakePos(10, 18)); poses.Add(Pos.MakePos(11, 17)); poses.Add(Pos.MakePos(12, 16)); poses.Add(Pos.MakePos(13, 17)); poses.Add(Pos.MakePos(14, 18)); poses.Add(Pos.MakePos(14, 19)); poses.Add(Pos.MakePos(14, 20)); poses.Add(Pos.MakePos(14, 21)); poses.Add(Pos.MakePos(13, 22)); poses.Add(Pos.MakePos(12, 23)); poses.Add(Pos.MakePos(11, 22)); poses.Add(Pos.MakePos(10, 21)); poses.Add(Pos.MakePos(10, 20)); poses.Add(Pos.MakePos(10, 19)); poses.Add(Pos.MakePos(10, 25)); poses.Add(Pos.MakePos(11, 26)); poses.Add(Pos.MakePos(12, 27)); poses.Add(Pos.MakePos(13, 28)); poses.Add(Pos.MakePos(14, 29)); poses.Add(Pos.MakePos(13, 30)); poses.Add(Pos.MakePos(12, 31)); poses.Add(Pos.MakePos(11, 32)); poses.Add(Pos.MakePos(10, 33)); poses.Add(Pos.MakePos(10, 37)); poses.Add(Pos.MakePos(10, 38)); poses.Add(Pos.MakePos(10, 39)); poses.Add(Pos.MakePos(10, 40)); poses.Add(Pos.MakePos(10, 41)); poses.Add(Pos.MakePos(10, 42)); poses.Add(Pos.MakePos(11, 37)); poses.Add(Pos.MakePos(12, 37)); poses.Add(Pos.MakePos(12, 38)); poses.Add(Pos.MakePos(12, 39)); poses.Add(Pos.MakePos(12, 40)); poses.Add(Pos.MakePos(12, 41)); poses.Add(Pos.MakePos(12, 42)); poses.Add(Pos.MakePos(13, 37)); poses.Add(Pos.MakePos(14, 37)); poses.Add(Pos.MakePos(14, 38)); poses.Add(Pos.MakePos(14, 39)); poses.Add(Pos.MakePos(14, 40)); poses.Add(Pos.MakePos(14, 41)); poses.Add(Pos.MakePos(14, 42)); poses.Add(Pos.MakePos(10, 49)); poses.Add(Pos.MakePos(11, 50)); poses.Add(Pos.MakePos(12, 51)); poses.Add(Pos.MakePos(10, 53)); poses.Add(Pos.MakePos(11, 52)); poses.Add(Pos.MakePos(12, 51)); poses.Add(Pos.MakePos(13, 51)); poses.Add(Pos.MakePos(14, 51)); poses.Add(Pos.MakePos(10, 59)); poses.Add(Pos.MakePos(11, 58)); poses.Add(Pos.MakePos(12, 57)); poses.Add(Pos.MakePos(13, 58)); poses.Add(Pos.MakePos(14, 59)); poses.Add(Pos.MakePos(14, 60)); poses.Add(Pos.MakePos(14, 61)); poses.Add(Pos.MakePos(14, 62)); poses.Add(Pos.MakePos(13, 63)); poses.Add(Pos.MakePos(12, 64)); poses.Add(Pos.MakePos(11, 63)); poses.Add(Pos.MakePos(10, 62)); poses.Add(Pos.MakePos(10, 61)); poses.Add(Pos.MakePos(10, 60)); poses.Add(Pos.MakePos(10, 67)); poses.Add(Pos.MakePos(11, 67)); poses.Add(Pos.MakePos(12, 67)); poses.Add(Pos.MakePos(13, 67)); poses.Add(Pos.MakePos(14, 68)); poses.Add(Pos.MakePos(14, 69)); poses.Add(Pos.MakePos(14, 70)); poses.Add(Pos.MakePos(14, 71)); poses.Add(Pos.MakePos(13, 72)); poses.Add(Pos.MakePos(12, 72)); poses.Add(Pos.MakePos(11, 72)); poses.Add(Pos.MakePos(10, 72)); } static private string[] screen; static private bool update_flag; static private int pro_count; static void InitScreen() { screen = new string[25]; update_flag = true; pro_count = 0; for (int i = 0; i < 25; i++) { screen[i] = new string(‘ ‘,79); if (i != 24) screen[i] += "\n"; else screen[i] += ""; } } static void SetChar(int x, int y, char target) { if (x >= 0 && x < 25 && y >= 0 && y < 81) { char[] line = screen[x].ToCharArray(); line[y] = target; string newLine = new string(line); screen[x] = newLine; } } static void Update(int delay, List<Pos> poses) { while (update_flag) { Thread.Sleep(delay); Processing(poses); } } static void Processing(List<Pos> poses) { if (pro_count < poses.Count) { SetChar(poses[pro_count].X, poses[pro_count].Y, ‘o‘); } else { DisplayColor(); } DisplayScreen(); if (pro_count > 0x7ffffffe) pro_count = 0; pro_count++; } static void StartUpdate() { update_flag = true; } static void StopUpdate() { update_flag = false; } static void DisplayScreen() { Console.WriteLine(); for (int i = 0; i < 25; i++) { Console.Write(screen[i]); } } static void DisplayColor() { int _count = pro_count / 8; int _type = _count % 8; switch (_type) { case 0: Console.ForegroundColor = ConsoleColor.Cyan; break; case 1: Console.ForegroundColor = ConsoleColor.DarkGray; break; case 2: Console.ForegroundColor = ConsoleColor.Red; break; case 3: Console.ForegroundColor = ConsoleColor.White; break; case 4: Console.ForegroundColor = ConsoleColor.Yellow; break; case 5: Console.ForegroundColor = ConsoleColor.Green; break; case 6: Console.ForegroundColor = ConsoleColor.Magenta; break; case 7: Console.ForegroundColor = ConsoleColor.Blue; break; } } } class Pos { private int x; private int y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } Pos(int _x, int _y) { x = _x; y = _y; } public static Pos MakePos(int _x, int _y) { Pos ret = new Pos(_x, _y); return ret; } } }
编译后有惊喜 噗噗噗
win7 dos窗口模拟帧刷新
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。