C++实验课 实现银行叫号器功能 自动滚动+键盘上下左右键移动数字

利用伪双线程实现 包含光标移动 隐藏光标的实现


#include <iostream>
#include <windows.h>
#include <ctime>
#include <conio.h>
#include <list>
using namespace std;


//光标移动
void gotoxy(int x, int y)
{
    HANDLE h;
    COORD c;
    c.X = x;
    c.Y = y;
    h = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(h, c);
}
//隐藏光标
void hide_cursor()
{
    HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursor_info;
    GetConsoleCursorInfo(h_GAME, &cursor_info);
    cursor_info.bVisible = false;//不显示光标
    SetConsoleCursorInfo(h_GAME, &cursor_info);
}
//显示光标
void show_cursor()
{
    HANDLE h_GAME = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursor_info;
    GetConsoleCursorInfo(h_GAME, &cursor_info);
    cursor_info.bVisible = true;//不显示光标
    SetConsoleCursorInfo(h_GAME, &cursor_info);
}

static int x = 10, y = 5;

class test
{
private:
    int n = 5;
    list<int> l{ 1,2,3,4,5 };
    list<int>::iterator li ;
    clock_t t,t1,t2;
public:
    test() {
        t = clock();
        t1 = clock();
        t2 = clock();
        hide_cursor();
    }
    ~test()
    {
        show_cursor();
    }
    //模拟多线程
    void draw()
    {
        //y = 5;
        for (li = l.begin();li != l.end();++li)
        {
            gotoxy(x, y);
            y++;
            cout << *li;
        }
        y = y - 5;
        
    }
    void erase()
    {
        //y = 5;
        for (int i = 0; i < 5; i++)
        {
            gotoxy(x, y);
            y++;
            cout << "  ";
        }
        y = y - 5;
    }
    void move()
    {
        while (true)
        {
            move1();
            move2();
        }
    }
    void move1()
    {
        
        if (clock() - t1 >= 1000)//一秒更新一次
        {
            erase();

            l.pop_front();
            l.push_back(n);
            ++n;
            
            draw();
            t1 = clock();
        }
    }
    void move2()
    {
        
        if (clock() - t2 > 200)//只有读取到按键之后才更新
        {
            
            if (GetAsyncKeyState(VK_LEFT))
            {
                erase();
                --x;
                draw();
            }
            if (GetAsyncKeyState(VK_RIGHT))
            {
                erase();
                ++x;
                draw();
            }
            if (GetAsyncKeyState(VK_UP))
            {
                erase();
                --y;
                draw();
            }
            if (GetAsyncKeyState(VK_DOWN))
            {
                erase();
                ++y;
                draw();
            }
            if (GetAsyncKeyState(VK_ESCAPE))
            {
                exit(0);
            }
            
            t2 = clock();
        }
        
    }
};
int main()
{
    test t;
    t.move();
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值