在C语言中如何同时打印两个函数?

发布于 2024-12-20 12:31:19 字数 1755 浏览 1 评论 0原文

我在控制台应用程序上用 C 语言做一个游戏,我需要在用户同时玩游戏时打印一个计时器,我不知道如何很好地编程,实际上我刚刚开始使用这个编译器,所以我不了解很多多线程和东西,但这就是我所了解的,这是两个函数的示例,第一个是“计时器”,第二个只是一些问题,我的疑问是我是否可以同时运行这两个功能,以及我如何可以打印计时器,同时我可以在不中断控制台的情况下回答这两个问题。

示例:

#include <windows.h>
#include <stdio.h>
#include <time.h>


DWORD WINAPI Timer(int limit)
{
    int secs = 1;
    time_t unix;

    struct tm * timeinfo;
    time(&unix);
    timeinfo = localtime(&unix);

    int t1 = timeinfo->tm_sec;
    int t2 = timeinfo->tm_sec;
    int i = 0;

    while(1 == 1)
    {
       time(&unix);
       timeinfo = localtime(&unix);
       if((t1 + i)  == timeinfo->tm_sec)
       {
              system("cls");
              printf("time left %d seconds\n", timeinfo->tm_sec - t2 - limit);
              i++;
       }
       if(timeinfo->tm_sec >= (t1 + limit))
       {
            break;
            printf("Your time its done");
       }
    }
    return 0;
}

DWORD WINAPI Questionary()
{
    puts("testing\n\n");

    int age, height;
    printf("Please write your age: ");
    scanf("%d", &age);
    printf("Please write your height: ");
    scanf("%d", &height);
    printf("\n\nThe numbers written are %d y %d", age, height);
    return NULL;
}
int main()
{
    int i, limit;
    HANDLE tempo;
    HANDLE questions;
    DWORD ThreadId;

    printf("\nHow much time would you like for your timer countdown? ");
    scanf("%d", &limit);

    //Funcion Handle
    questions= CreateThread(NULL,0,Questionary,1,0,&ThreadId);
    WaitForSingleObject(preguntas,INFINITE);

    tempo= CreateThread(NULL,0,Timer(limit),1,0,&ThreadId);
    WaitForSingleObject(tiempofinal,limit*40);


    return 0;
}

Im doing a game in C on console application and I need to print a timer while the user play the game at the same time, I don't know how to program that well, actually I just start using this compiler so I don't know a lot of multithreads and stuff but this is the far as I get, here is an example of two functions the first one is the "timer" and the second its just some question, my doubt is if I can run these 2 functions at the same time, and how I can print the timer while I can answer those 2 question without interrupting the console.

EXAMPLE:

#include <windows.h>
#include <stdio.h>
#include <time.h>


DWORD WINAPI Timer(int limit)
{
    int secs = 1;
    time_t unix;

    struct tm * timeinfo;
    time(&unix);
    timeinfo = localtime(&unix);

    int t1 = timeinfo->tm_sec;
    int t2 = timeinfo->tm_sec;
    int i = 0;

    while(1 == 1)
    {
       time(&unix);
       timeinfo = localtime(&unix);
       if((t1 + i)  == timeinfo->tm_sec)
       {
              system("cls");
              printf("time left %d seconds\n", timeinfo->tm_sec - t2 - limit);
              i++;
       }
       if(timeinfo->tm_sec >= (t1 + limit))
       {
            break;
            printf("Your time its done");
       }
    }
    return 0;
}

DWORD WINAPI Questionary()
{
    puts("testing\n\n");

    int age, height;
    printf("Please write your age: ");
    scanf("%d", &age);
    printf("Please write your height: ");
    scanf("%d", &height);
    printf("\n\nThe numbers written are %d y %d", age, height);
    return NULL;
}
int main()
{
    int i, limit;
    HANDLE tempo;
    HANDLE questions;
    DWORD ThreadId;

    printf("\nHow much time would you like for your timer countdown? ");
    scanf("%d", &limit);

    //Funcion Handle
    questions= CreateThread(NULL,0,Questionary,1,0,&ThreadId);
    WaitForSingleObject(preguntas,INFINITE);

    tempo= CreateThread(NULL,0,Timer(limit),1,0,&ThreadId);
    WaitForSingleObject(tiempofinal,limit*40);


    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2024-12-27 12:31:19

您可以打印问题,而不是在阻塞调用(如 scanf)中等待答案,而是在轮询新输入时循环,并使用 ANSI 转义码 在另一个位置打印时间。

类似下面的伪代码:

print(question)
while (current_time < timer_end)
{
    if (have_input())
        break

    save_current_cursor_position()
    move_cursor_position(x, y)
    print(timer)
    restore_saved_cursor_position()

    very_short_sleep()
}

You can print the question, and instead of waiting for an answer in a blocking call (like scanf) you loop while polling for new input, and use ANSI escape codes to print the time at another position.

Something like the following pseudo-code:

print(question)
while (current_time < timer_end)
{
    if (have_input())
        break

    save_current_cursor_position()
    move_cursor_position(x, y)
    print(timer)
    restore_saved_cursor_position()

    very_short_sleep()
}
北恋 2024-12-27 12:31:19

您想要的线程功能无法通过面向行的函数来完成,像 ncurses 这样的函数对于多线程控制台读/写操作是必要的。

Joachim Pileborg 所写的是你最好的机会,限制是你的时钟在等待输入时不能滴答作响。如果你每 1 秒轮询一次,写下时间然后等待答案,大多数人都不会1秒回复一次,所以每10秒或许是最明智的。

在仍然每秒轮询的同时呈现每秒时间滴答的另一种方法是将这一秒的输入保存在累积缓冲区上,并在下一次输入轮询时将其重写在屏幕上(除了时间滴答之外)。这确实存在在循环交替时丢失某些输入的危险,并且编码更复杂。

在这种情况下,图形界面会更适合您的需求。

您可以使用英语翻译来修改程序中的变量名,以便快速理解程序内容,这就是市长的全部内容。

What you want with threads, can't be accomplished with the line oriented functions, something like ncurses would be necessary for multithreaded console read/write operation.

What Joachim Pileborg, wrote is your best chance, the limitation is that your clock can't be ticking while you wait for input., if you poll every 1 second, writing the time and then waiting for the answer, most humans would not be able to reply on 1 second, so every 10 seconds would perhaps be the most sensible.

An alternate method to present the time ticking every second while still polling every second would be to save the input of that second on a accumulative buffer, and rewrite it on the screen on next input polling, besides the time ticking. This does have the danger of missing some input done while the loops alternate and is more complicated to code.

This is a case where a Graphical interface would be better suited for your needs.

Y seria buena idea que traduzcas al inglés tus nombres de variables para ayudar a una más rápida comprensión del contenido de tu programa, ya que es lo que entiende la mayoría por acá.

彩虹直至黑白 2024-12-27 12:31:19

据我所知,无法同时从不同线程写入控制台。尽管看起来两个线程正在同时写入,但实际上其中一个线程一次写入(捕获控制台写入)。这样虽然两个线程同时运行,但是没有意义。

我认为你可以在一个线程中解决你的问题,虽然有点棘手,但这并不是不好。但我认为你可以使用 gui 组件,并从两个不同的线程,你可以设置该组件的字段,这满足你的要求。

As i know there is no way to write to console from different threads at the same time. Altough it looks two thread are writing at the same time, actually one of them that write at a time (Which catch the console to write). In this way altough two thread is running at the same time, it does not make sense.

I think you can solve your problem as a little tricky in one thread but it is not not good. But i think you can gui components and from two different threads, you can set field of this component and this satisfy your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文