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;
        }
    }
}

编译后有惊喜 噗噗噗

时间: 2024-12-15 01:56:46

win7 dos窗口模拟帧刷新的相关文章

windows环境变量设置无效解决办法——DOS窗口设置环境变量

公司配置的电脑是Win7,使用的账户并不是管理员账户,我在计算机->属性中设置环境变量无效. 后来在DOS窗口中设置环境变量成功. 1. set [环境变量名称]=[所有环境变量值]:set Path=%MAVEN_HOME%\bin;C:\Program Files\Java\jdk1.7.0_71\bin; 这种方法设置后只在当前DOS窗口下有效,退出后重新打开则无效,可以使用命令:echo %Path%查看. 2. setx [环境变量名称] "[增加环境变量值]":set

如何在DOS窗口中显示UTF-8字符

在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即编码是中文字符集或者西文字符集. 如果想正确显示UTF-8字符,可以按照以下步骤操作: 1.打开CMD.exe命令行窗口 2.通过 chcp命令改变代码页,UTF-8的代码页为65001 chcp 65001 执行该操作后,代码页就被变成UTF-8了.但是,在窗口中仍旧不能正确显示UTF-8字符.

如何在DOS窗口复制和粘贴命令

在键盘上按下windows+R键,打开运行窗口. 在"打开"处输入cmd,并按下enter键,打开DOS窗口. 把鼠标移动到DOS窗口标题处,单击鼠标右键,选择属性. 把编辑选项处的"快速编辑模式"勾选,并点击确定按钮. 用鼠标左键选中命令后,单击鼠标右键,即完成了命令的复制. 在粘贴处再次单击鼠标右键,即完成了命令的粘贴. 原文链接:http://jingyan.baidu.com/article/cb5d6105fbb497005d2fe079.html

解决python在windows上运行弹出cmd窗口(dos窗口)

运行python程序的时候会在背景显示一个cmd,要想不显示其实很简单(虽然是我找了1个小时...才了解的基本知识) 方法1:pythonw xxx.py 方法2:将.py改成.pyw (这个其实就是使用脚本解析程序pythonw.exe) 原文: 1) Try changing the file extension to .pyw. Double-clicking a .pyw will use pythonw.exe instead of python.exe. python.exe运行的时

HTML高级标签(2)————窗口分帧(1)————分帧演示

我们可以简单的编写一个多帧的窗口,并且可以任意的划分窗口区域. <frameset rows="*,*,*"> <frameset cols="*,*"> <frame src="http://www.baidu.com"> <frame src="http://www.csdn.net"> </frameset> <frameset cols="*,

【Java编程】Java在dos窗口编译与执行的批处理

最近在Java编程过程中,常用到dos窗口对程序进行编译与运行.但是不方便之处在于每次都要输入命令进入将要编译的程序的目录(其实也有简单的方法,在文章末尾给出).于是编写了一个配置文件,可以一次修改,经常使用.(只需要在下面程序中将路径修改为你所经常存放程序的目录).前提当然是你已正确配置环境变量.具体方法为:将下面的程序放在新建的文本文件中,并将后缀名改为.bat.然后双击该文件,按提示操作即可!程序源码如下:(红色字体是需要根据你的存放路径修改的) echo off cls title Ja

在windows中的DOS窗口执行adb shell出错的解决办法

1,首先要检查是否已经配置好环境变量或者在platform-tools目录(adb.exe存在的路径)下执行的adb shell 2,步骤1没问题的话,那么运行:adb kill-server 再执行:adb shell 好了,问题解决(PS:反正我的解决了) 在windows中的DOS窗口执行adb shell出错的解决办法,布布扣,bubuko.com

Win7取消窗口边缘最大化

控制面板\所有控制面板项\轻松访问中心(Win+U) 1.使鼠标更易于使用 2.防止将窗口移动到屏幕边缘时自动排列窗口",然后点击确定即可. Win7取消窗口边缘最大化,布布扣,bubuko.com

解决dos窗口乱码问题

大家有没有遇到这样的情况,看着就糟心 打开dos窗口, 输入命令 chcp 936 (936表示中文编码GBK, 也可以设置其他编码), 回车一下执行.  鼠标右键 -> 属性  (关键一步): 什么也不修改, 直接点确定(之前是修改了字体, 保存后发现编码竟然生效了, 后来发现什么也不改直接确定也可以, 不明白这是什么原理, 有知道的可以回复一下)  关闭dos, 重新打开, 发现乱码问题解决了!