I'm creating a choice-based survival game. I want it so that, upon entering the option screen, the options are displayed and a timer is triggered. If the user doesn't enter in a choice within, say, 10 seconds, they die.
The problem I've run into is trying to dual-execute the timer and cin statement. Ideally I want it to display "You died." (or something like it) after 10 seconds but
before the user enters something. But they should be able to choose an option at any point within those 10 seconds.
I know I could just have it evaluate a timer after the user enters a value, so they have as long as they want to enter something, but if it takes longer than 10 seconds it returns dead after the value is entered.
I've tried using a built-in timer function, but I only was able to do the post-evaluation with it. I also tried threads, but not only did I get confused it also could only give me post-evaluation.
Edit: Didn't supply code, oops. Here it is, though not very helpful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <ctime>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <windows.h>
#include <process.h>
//Yeah, I know I have some unnecessary libraries. I just have them there in case I need them.
using namespace std;
int main()
{
int choice = 0;
cout << "Choose an option:\n";
cout << "[1] - Option 1\n";
cout << "[2] - Option 2\n";
cout << "[3] - Option 3\n";
cout << "[4] - Option 4\n";
cout << "[5] - Option 5\n";
cout << "[6] - Option 6\n";
cout << "[7] - Option 7\n\n";
if (choice == 1)
cout << "test1";
if (choice == 2)
cout << "test2";
if (choice == 3)
cout << "test3";
// I just have some basic option returns here.
return 0;
}
|
Like I said, I only have the gutted code for the options. I'd supply my attempts at timers and clocks, but they're honestly a joke because I didn't know what I was doing.