控制台字符移动

刚才有网友问能否实现控制台内字符的移动,以前也未曾接触过,不过百度之后发现原理很简单,就是清空之后再重新绘制即可,简单记录之,方便以后使用。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <conio.h>
 4 #include <time.h>
 5 #include <windows.h>
 6 
 7 void Move(int position)
 8 {
 9     int i = 0;
10     system( "cls" );//清空屏幕
11     while(i++<position)
12     {
13         putchar(' ');
14     }
15     putchar('A');
16 }
17 
18 void main()
19 {
20     int lastTime = GetTickCount();
21     int position = 0;
22     while(1)
23     {
24         while(GetTickCount() - lastTime < 500)
25         {
26             Sleep(1);
27         }
28         lastTime = GetTickCount();
29         Move(position++);
30     }
31 }

以上代码实现了A字符的从左向右移动,涉及到定时等操作,如果想整屏移动只需控制横竖的显示位置即可。

posted @ 2016-03-07 09:50  shoufengwei  阅读(379)  评论(0编辑  收藏  举报