Timer for a game in C++

Hello, my name is Gabriel and I'm a new member on this website

As you may have already noticed, I'm working on a game in C++. The game is based on the first version of Bejeweled where you simply had to match 3 jewels to increase youe score in a given time. This is the idea of my little game, but I need the timer so the game makes sense, like there is a small challenge for the player. My question is: Is it possible to add a timer to the game that can be incremented whenever the player matches 3 to 5 jewels? If it is, would someone of you mind helping me with a code or something?

Something like, whenever the function that verifies if the player did a good move has the value 1(true), another function or a simple if instruction will add a couple of seconds to the remaining time.

Thank you all in advance for spending time reading this! And sorry if my English is bad or a bit rusty but I'm not a native English speaker.
It is absolutely possible. What libraries are you using?
If you are only looking to keep track of elapsed time, you can merely check the current time every time the user makes a move. Simply subtract the timestamp at the beginning of the game from the current timestamp to determine the elapsed time.

If you need to set a timer that performs some functionality upon expiration, that takes some additional infrastructure. Many libraries contain timer callback hooks, or you could roll your own.
@Computergeek01 At the moment I'm using graphics, malloc, string, stdlib, iostream and stdio.

@doug4 I've worked around a bit and ended up with the first thing you just said. I'm able now to track the remaining time. My question is now, how can I add a bunch of seconds to the timer whenever the player makes a good move? This is how it looks for the moment:

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
36
37
38
#include <stdio.h>
#include <time.h>


int main ()
{



	unsigned int x_seconds=0,x_milliseconds;

	unsigned int totaltime=0,count_down_time_in_secs=0,time_left=0;

	clock_t x_startTime,x_countTime;
	count_down_time_in_secs=100;


    x_startTime=clock(); 
    time_left=count_down_time_in_secs-x_seconds; 

	while (time_left>0)
	{
		x_countTime=clock();
		x_milliseconds=x_countTime-x_startTime;
		x_seconds=(x_milliseconds/(CLOCKS_PER_SEC));
        if(time_left!=count_down_time_in_secs-x_seconds)
        {
            time_left=count_down_time_in_secs-x_seconds;
            printf("Remaining time: %d\n",time_left);
        }

	}


	printf( "\n\n\nTime's out\n\n\n");

return 0;
}
Last edited on
That doesnt sound like c++ at all. malloc is C, and you're using a bunch of C headers.

Use actual c++ and a great library for that can be SFML (for the graphics part). If you want to allocate memory use smart pointers, use std::cout and not printf etc
Adding this feature to your current game loop would be pretty easy. You would simply check for the condition and increment the game timer within your game loop (Lines 21 - 32 as of now). So something like this:

1
2
3
4
5
if(Good_Move) //Test for boolean condition
{
    x_countTime = x_countTime + GoodMoveIncrementAmount; //Increment timer 
    Good_move = false; //Reset condition
}


TarikNeaj is right of course, SFML would be a great choice for this project. But the concept of this operation doesn't change too much, just the variable types.
@TarikNeaj Thank you for the tips! I'm a beginner and this is how my teacher told me to do. I have only worked in C++ so I can't make the difference between C and C++ headers. Also, my knowledge regarding C++ libraries is not so big. Would you mind telling me what's the difference between std::cout and printf/cout (simple)? Also, what does std:: stands for and do I need another library to use smart pointers?
std:: is the standard library. You can read about cout and such here in the input and output tutorial of this page - http://www.cplusplus.com/doc/tutorial/basic_io/

If you want it put very simply, printf is for C and cout is for c++.

Im probably not the most qualified to explain everything, but this site has great tutorials, there are lots of c++ tutorials on youtube as well if you learn by watching.

TheNewBoston has about 73 videos of the basics of c++, they're not the absolute best(there are some bad programming practices, but you'll learn to avoid them once you learn more), but they're great for beginners as they teach you basic concepts and what to learn. Watch a video, google about it to learn more, and then try and use it in a program -

https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

And about Smart pointers, since you're new you should skip it for now in my opinion, you can play around with memory managment, but the c++ way is using new and delete (which is malloc and free of c++) - http://www.cplusplus.com/doc/tutorial/dynamic/

bjarne stroustrup (creator of c++) as well as other higher ups in the c++ language 100% recommend everyone to learn from bjarne stroustrup book - A tour of c++ (it's kinda fantastic, and cheap, but there are other ways to get it if you catch my drift)
Last edited on
@Computergeek01 Thank you for those coding lines! I'll adapt them to my program when I get back home and I'll replay with feedback!

@TarikNeaj Thank you for the useful information! I'll try to find Bjarne's book and buy it if possible or download it from somewhere as soon as I can find it and I'll check those tutorials too! I'm just a bit short on time as I'm supposed to present my game next week.

I will come back with this little project as soon as it's done if anyone is interested to check it.
Topic archived. No new replies allowed.