几个简单的windows API

//将光标移动到x,y位置
void gotoxy(int x, int y)
{
COORD c;
c.X = x; c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

//设置窗口颜色 用到两个Windows API 不做详细介绍
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//设置颜色
}

//获取键盘输入

void listen_key_borad()
{
char ch;

if (_kbhit()) //kbhit 非阻塞函数
{
ch = _getch(); //使用 getch 函数获取键盘输入
switch (ch)
{
case 'w':
case 'W':
if (this->m_direction == DOWN)
break;
this->m_direction = UP;
break;
case 's':
case 'S':
if (this->m_direction == UP)
break;
this->m_direction = DOWN;
break;
case 'a':
case 'A':
if (this->m_direction == RIGHT)
break;
this->m_direction = LEFT;
break;
case 'd':
case 'D':
if (this->m_direction == LEFT)
break;
this->m_direction = RIGHT;
break;
case '+':
if (speed >= 25)
{
speed -= 25;
}
break;
case '-':
if (speed < 250)
{
speed += 25;
}
break;
}
}
}

posted on 2019-03-13 19:48  船王张  阅读(277)  评论(0)    收藏  举报