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ループから抜け出すために最初に押したキーが使用されます。

考えられる解決策:少しハッキーですが、getch()whileループの直後に追加するとおそらく修正されます。

それらのBorlandライブラリの代わりにncursesを使用することも提案できますか?

于 2012-03-04T06:18:23.513 に答える