本文介绍了在游戏中添加计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目制作一个用Turbo C ++开发的游戏程序,我需要有关如何添加游戏计时器的帮助,我已经看过有关如何使用while循环创建计时器的视频,但是我不知道如何实现它我的游戏.我的游戏计划是显示6个已初始化的字母(例如"NAEBTS"),并在30秒内输入尽可能多的单词,这些单词具有对应的点数(6 = 10分,5 = 8分,4 = 6分,3 = 4分)).正确的单词及其对应的点将写入txt文件中.而且整个事情都与clrscr()处于循环中;

I am making a game program in turbo c++ for my project and I need help on how to add a game timer, I've seen videos on how to create timer using while loop but I don't know how to implement it to my game. My plan for my game is to have it show 6 initialized letters(ex. "N A E B T S") and within 30 secs input as many words as possible which has corresponding points(6=10pts, 5=8pts, 4=6pts, 3=4pts). The correct words are written in a txt file with their corresponding points. Also the whole thing is in loop with clrscr();

这是游戏代码的某些部分:

Here is some parts of the game code:

void start()
{
    char arr[10][50] = {" B A N S E T ",
                        " L E A Z D Z ",
                        " M B L U E J ",
                        " P R G N I S ",
                        " A C Q U K Y ",
                        " S A H L E S ",
                        " R E D G A E ",
                        " Z E D Z U B "};

    int i = 0;
    int sum = 0;
    int x = 0;
    do
    {
        clrscr();
        cout << "\n\t\t\t\t\t SCORE: " << sum << " pts"
             << "\n                  ******************************\n";
        cout << "                  *       " << arr[i] << "        *\n";
        cout << "                  ******************************\n\n";
        char a[50], b[50];
        int  c;
        if (arr[0])
        {
            ifstream fin;
            fin.open("lvl1.txt");
            if (fin.fail())
            {
                cout << "File doesn't exist!";
                exit(1);
            }
            cout << "\tEnter word: ";
            cin >> a;
            do
            {
                fin >> b >> c;
                if (fin.eof() == 1)
                {
                    cout << "Incorrect! Try Again!";
                    delay(1500);
                    exit(1);
                }
            } while (strcmp(a, b) != 0);
            fin.close();
            if (strcmp(a, b) == 0)
            {
                sum += c;
            }
        }
    } while(s != 0);
}

推荐答案

您可以将 PIT 用作计时器,

  • Mouse program in Turbo CPP to obtain time (polling)

使用旧版的 Turbo C ++ MS-DOS 进行地雷游戏.有关 PIT 的更多信息,请参见:

its a mines game in old Turbo C++ and MS-DOS. For more info about PIT see:

有指向 PIT 参考和示例的链接,我建议您查看 PCGPE .

there are links to PIT reference and examples I recommend you to see the PCGPE.

现在回到您的问题.您应该注册 PIT ISR 例程,以便在后台进行计时/超时...在这里,我只是在 DOSBOX 中关闭了示例:

Now back to your question. You should register PIT ISR routine doing your timing/timeouting in the background ... Here example I just busted in DOSBOX:

#include <dos.h>
#include <conio.h>
#include <iostream.h>

int stop=0;
int timeout_cnt=0;

const int int_PIT=0x08;
void interrupt (*isr_PIT0)(...)=NULL; // original ISR handler
void interrupt isr_PIT(...) // new ISR handler
    {
    isr_PIT0(); // call original handler
    // here do your stuff
    if (timeout_cnt) timeout_cnt--;
    else stop=1;
    }

void main()
    {
    clrscr();
    isr_PIT0=getvect(int_PIT);  // store original ISR
    setvect(int_PIT,isr_PIT);   // set new ISR
    cout << "start counting" << endl;
    stop=0;
    timeout_cnt=(3*182)/10;     // init timeout 18.2Hz -> 3 sec
    for (;!stop;)
        {
        // here do your stuff
        }
    cout << "timeouted" << endl;
    setvect(int_PIT,isr_PIT0);  // restore original ISR
    getch(); // this is duplicated just to avoid DOSBOX glitches
    getch();
    getch();
    }

您基本上只需要 dos.h ,所有其他内容仅用于打印和处理键盘.

You basically need just dos.h all the other stuff is just for printing and handling keyboard.

因此,我创建了 ISR ,它连接到 PIT ,该频率以18.2 Hz的频率调用.通过将 timeout_cnt 设置为超时时间值并重置 stop :

So I created ISR that hooks up to PIT which is called with 18.2 Hz frequency. The timeout is initiated by setting the timeout_cnt to timeout time value and reseting the stop:

stop = 0;
timeout_cnt = time[sec] * 18.2;

移植为整数...一旦计数器下溢,它将 stop 值设置为true.我还将原始的 ISR 处理程序称为 MS-DOS 中继.不要忘记在应用退出之前恢复原始的 ISR .

ported to integer... once counter underflows it sets the stop value to true. I also call the original ISR handler as MS-DOS relays on it. Do not forget to restore original ISR before apps exit.

timeout_cnt stop 变量应为 volatile ,但 IIRC 与旧的 Turbo C ++ ,因为没有优化可以优化它们.

btw the timeout_cnt and stop variables should be volatile but IIRC it does not matter in old Turbo C++ as there are no optimizations that could optimize them out to speak of.

如果您更改了 PIT 频率,则应以 18.2 Hz 调用原始处理程序,并在应用退出之前恢复原始的 PIT 频率.

In case you change the PIT frequency you should call the original handler with 18.2 Hz and restore original PIT frequency before apps exit.

这也可以用作一种多任务处理,因为您也可以在ISR处理程序中进行操作(与主代码无关),但是您需要注意,因为主代码可以随时暂停,例如在中间将字符串写到屏幕上,并且如果您的背景材料也正在打印,您的输出可能会失真等...因此,适用于多线程的类似规则.

This can be also used as a sort of multitasking as you can do stuff in the ISR handler too (regardless of the main code) but you need to be careful as the main code can be paused at any time like in middle of writing string to screen and if your background stuff is printing too you can have distorted output etc ... so similar rules like in multi-threading applies.

这篇关于在游戏中添加计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 10:01
查看更多