Good day! I am trying to implement the Tamagotchi game (C ++, a console application). Over time, an object of the Tamagotchi class should starve: its hunger level should increase by one for each unit of time (for example, by +1 for every 60 seconds). How to realize this opportunity?

The main interface of the program is implemented on a while loop: the user enters commands that are interpreted by the program as “feed”, “walk”, I don't know, “play”, and so on.

Actually, the question is from 2 parts:

  1. How to implement a change in the value of the variable (hunger) once a minute? Can this be done in a modern way? With ++ 11, there, all affairs. The only thing that comes to mind is to create a cycle that will work until the difference between the start of the cycle and the current moment t0-t1 is more than 60.
  2. Is it possible to realize this change of value against the background of the running while loop (the program communicates with the user) without threading? That is, so that I do not have to wait for the end of the 60-second cycle to enter a command into the console.

Thank you in advance.

    3 answers 3

    1. You can use the library time.h, which allows you to get the current time. Next, write a method that will work every second.

    2. Yes you can.

    For example, you can use kbhit () + getch () to check if the key was pressed (I am sure that there is a more elegant way)

    The primitive game cycle looks like this.

    while (true){ input(); update(); render(); } 

    in the Input () method, you check whether the user entered the data or not. In the update method, all data is updated, for example, a pet eats or not. In the render method, you draw the data, but if you have a console application, then you don't need it.

    I advise you to read the book Game Programming Patterns (there is a translation in Russian). There it is told in a very clear language how everything happens. The book has a chapter Game Loop, where game cycles are described.

    • @Rpro, it is in the update() function that you look at all Tamagotchi in a cycle and depending on the current time and the time of the last feeding (you remember it in the structure describing each Tamagotchi) you change his hunger level. - avp
    • @avp But in this case, the update () function will not be called until the user enters any input (); In this case, if I leave for a long tea party, my Tamagotchi will not even die, because the value of hunger will not be calculated until I enter something into the console. Right? - Rpro
    • @Rpro, is not true. As long as you do not enter the time anyway. You will find out the current time after input, then update calculates everything and immediately then render draws the result (already starved to death). If you want to sit and watch, then you just need to modify the input so that it is interrupted, say once a second, if nothing is entered and returns this event (timeout). Then you will observe the whole process in the same cycle. - avp
    • @avp cheers. Before interrupting the input, I did not guess. Thank you very much - Rpro

    Well, it seems to me that the easiest thing is in a separate thread.

    It is possible without it, only to make the indicator not a variable, but a function that simply calculates the value based on a specific time.

    • I do not know how to flow with: Well, if I, for example, make an indicator a function that calculates the value for the current time, then I will have to constantly call this function? With that more than once a minute. How to implement it? - Rpro
    • How would you like to do this if the variable changes its value by itself, without your intervention? You didn’t ask the variable to break the cycle - it means you knew what to do with such a variable ... - Harry

    As the speaker wrote above, you can create some functionality that allows you to measure the time between calls to a certain function, accumulate this delta in a variable of type float (or any other, depending on the functions and libraries used) and increase the value of hunger when the threshold is 60 seconds. This functionality is usually called a timer.

    Here is an example of an English-language stackoverflow for getting time in Windows. Pseudocode example for the description above:

     SystemTime oldTime, newTime; GetSystemTime(&oldTime); // для верной инициализации WORD seconds = 0; while (true) { //какой-то код GetSystemTime(&newTime); seconds = newTime.wSeconds - oldTime.wSeconds; if (seconds >= 60) { increaseHungry(); oldTime = newTime; } } 

    The variant in your case may differ slightly depending on the operating system and libraries used, since libraries have their own implementation of timers, for example, in the same Qt.

    Also in C ++ 11 there is a library <chrono> , you can use it in the same key as the example with GetSystemTime() , the link to the example, in particular, the data type std::time_t and the function std::chrono::system_clock::now() to get the current time.