3

我正在尝试为汽车游戏制作一个屏幕并让屏幕等待一个键进入下一个屏幕,问题是使用此代码它改变颜色的速度太快了。我已经尝试过delay()sleep()但没有正常工作。此外,在按下键后,它会关闭并且不等待我输入键。我只是想让标题在白色和红色之间闪烁,直到按下一个键,并了解为什么它在按下一个键后退出。

这是我的代码:

#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "C|\\BORLANDC\\BGI");
    outtextxy(250,280,"POINTER DRIVER 1.0");
    outtextxy(250,290,"LCCM 10070249");
    do
    {
        setcolor(WHITE);
        outtextxy(250,380,"PRESS ANY KEY TO CONTINUE");
        // delay(10); nothing works here :(
        setcolor(RED);
        outtextxy(250,380,"PRESS ANY KEY TO CONTINUE");
    } while(!kbhit());
    cleardevice();
    outtextxy(250,290,"HELLO"); //here it draws mega fast and then exits
    getch();
    closegraph();
    return 0;
}
4

2 回答 2

1

而不是使用delay(10),也许尝试使用某种计时器变量来做到这一点。尝试以下操作(修改do-while循环):

unsigned flashTimer = 0;
unsigned flashInterval = 30; // Change this to vary flash speed
do
{
    if ( flashTimer > flashInterval )
        setcolor(RED);
    else
        setcolor(WHITE);

    outtextxy(250,380,"PRESS ANY KEY TO CONTINUE");

    ++flashTimer;
    if ( flashTimer > flashInterval * 2 )
        flashTimer = 0;

    // Remember to employ any required screen-sync routine here
} while(!kbhit());
于 2012-03-04T06:18:57.090 回答
0

kbhit()返回true,但在返回之前不删除该字符。到达该getch()行后,它会使用您按下的第一个键来跳出 while 循环。

可能的解决方案:虽然有点hacky,但添加一个getch()在你的while循环之后添加一个可能会解决它。

我还可以建议使用 ncurses 而不是那些 Borland 库吗?

于 2012-03-04T06:18:23.513 回答