-
December 25th, 2013, 07:44 AM
#1
timer with non-return calculations.(Help)
First of all, hello everyone and thanks, for taking your time to read my thread. Sorry, if i made some mistakes in thread title.
What i want to do? Well my game runs in a while loop, as you can see in the code below. I need a timer, which won't stop this loop or freeze it, but it will wait, until timer is over and executes code without interfering with the while or for loop.
Code:
Code:
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <ctime>
using namespace std;
char *map[25][25];
int carX = 19, carY = 9;
int random_obj_pos = rand() % 17 + 1;
int tempPlus = -1;
bool obstacleSpawned = false;
enum {
BLACK = 0,
DARK_BLUE = 1,
DARK_GREEN = 2,
TEAL = 3,
DARK_RED = 4,
DARK_PURPLE = 5,
GOLD = 6,
GREY = 7,
DARK_WHITE = 8,
BLUE = 9,
GREEN = 10,
CYAN = 11,
RED = 12,
PURPLE = 13,
YELLOW = 14,
WHITE = 15
};
void setColor(const int foreground, const int background) {
int Color = foreground + (background * 16);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, Color);
return;
}
void placeCursor(const int x, const int y) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD placeCursorHere;
placeCursorHere.X = x;
placeCursorHere.Y = y;
SetConsoleCursorPosition(hConsole, placeCursorHere);
return;
}
// TODO, dosen't work...
void deleteCursor(const int x, const int y) {
deleteCursor(x, y);
return;
}
void loadMap() {
// Loads te map with spaces and walls.
for (int a = 0; a < 20; a++)
{
for (int b = 0; b < 20; b++)
{
map[a][0] = "|";
map[a][18] = "|";
map[a][b] = " ";
}
}
}
void loadObstacles() {
// Load obstacles
for (int j = 1; j <= 3; j++)
{
for (int i = 1; i <= 3; i++)
{
map[j + tempPlus][random_obj_pos] = "x";
}
}
}
void moveObjects() {
if (!obstacleSpawned)
{
random_obj_pos = rand() % 17 + 1;
obstacleSpawned = true;
} else {
tempPlus++;
if (tempPlus > 19)
{
obstacleSpawned = false;
tempPlus = -1;
}
}
}
bool delayTimer(double secoundsToDelay) {
clock_t startTime = clock();
clock_t testTime;
clock_t timePassed;
double secondsPassed;
while(true)
{
testTime = clock();
timePassed = startTime - testTime;
secondsPassed = timePassed / (double)CLOCKS_PER_SEC;
if(secondsPassed >= secoundsToDelay)
{
return true;
}
}
return false;
}
void drawMap() {
// display the map.
for (int a = 0; a < 20; a++)
{
for (int b = 0; b < 20; b++)
{
cout << map[a][b];
if (b >= 19)
cout << endl;
}
}
}
inline void mySleep(clock_t sec)
{
clock_t start_time = clock();
clock_t end_time = sec * 1000 + start_time;
while(clock() != end_time);
}
void resetCarPosition(int oldCarX, int oldCarY, int newCarX, int newCarY) {
map[oldCarX][oldCarY] = " ";
map[newCarX][newCarY] = "*";
carX = newCarX;
carY = newCarY;
return;
}
int main() {
int UpArrow = 0;
int DownArrow = 0;
int LeftArrow = 0;
int RightArrow = 0;
int tempX = carX, tempY = carY;
do {
UpArrow = GetAsyncKeyState(VK_UP);
DownArrow = GetAsyncKeyState(VK_DOWN);
LeftArrow = GetAsyncKeyState(VK_LEFT);
RightArrow = GetAsyncKeyState(VK_RIGHT);
if (UpArrow != 0 && map[carX - 1][carY] == " ") {
if (tempX > 1)
tempX--;
} else if (DownArrow != 0 && map[carX + 1][carY] == " ") {
if (tempX < 19)
tempX++;
} else if (LeftArrow != 0 && map[carX][carY - 1] == " ") {
if (tempY > 1)
tempY--;
} else if (RightArrow != 0 && map[carX][carY + 1] == " ") {
if (tempY < 17)
tempY++;
}
placeCursor(0, 0);
setColor(TEAL, BLACK);
loadMap();
moveObjects();
loadObstacles();
resetCarPosition(carX, carY, tempX, tempY);
drawMap();
//mySleep(3);
Sleep(50);
if (GetAsyncKeyState(VK_ESCAPE))
exit(0);
} while (true);
return 0;
}
-
December 26th, 2013, 05:32 AM
#2
Re: timer with non-return calculations.(Help)
I need a timer, which won't stop this loop or freeze it, but it will wait, until timer is over and executes code without interfering with the while or for loop
What OS are you using? If you are using Windows, have a look at Waitable Timers
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|