Advertisement

Timing and Timers for Bomberman?

Started by November 24, 2002 01:07 PM
10 comments, last by WoolyUK 22 years, 3 months ago
Hi all! Quick question, im making a bomberman game and need to create a timer so that after 2 seconds a bomb will explode. But obviously the rest of the game must continue without stopping and waiting for the bomb How do I go about this please? Tried searching these forums but search still not working Thanks for any help! Adam [edited by - WoolyUK on November 24, 2002 2:14:11 PM]
Have a bomb struct/class where you store the bomb position,etc and the bomb time. Then in your game loop, check to see if time > 2 seconds and if so blow up otherwise increment with the current frame time. (Assumes the game is timing based, otherwise additionally store the time of placing the bomb, and compare that to the current time every game loop interation).

Luigi Pino
The 23rd Dimension
Advertisement
The easiest way might be to have a global timer that is set at the beginning of every game loop such as currenttime = timeGetTime(). When the bomb is set, set some timer variable in your bomb object class equal to the current time such as bomb.timer = currenttime. Then, every loop after that check to see if the difference between the currenttime and the bomb.timer is greater than or equal to 2 seconds. If the time difference is less than 2 seconds do nothing, otherwise set the bomb off and clear the bomb timer.
make your bomb class its own thread

when you create a new bomb object (place a bomb), start the thread, have the thread wait 2 seconds, and then explode the bomb, delete the bomb object.
Thanks!
If that's not the most gratuitous and inappropriate use of multithreading ever... jesus christ man... overkill city

I would go about it this way:
- the Bomb object has a State property (an enumeration of {Disarmed, Ticking, Exploding}... initialy Disarmed), and a Timer property (say, milliseconds as an integer)

- calling the Arm method of the Bomb sets this State to Ticking and intialises Timer to the number of seconds til the Bomb explodes (the Timer may be set by another method called SetTimer, or the init time may be another property of the Bomb)

- in your game loop, check all your Bombs' State property to see if they are Ticking... if so, call an Tick method (passing in the amount of time that has elapsed since the last run thru the game loop) which subtracts the time elapsed from the Bomb's Timer property. Then (still in the Tick method) test whether the Timer property is less than zero... if so, switch the Bomb's State property to Exploding

- The State property can then be used to tell how you should render the Bomb's graphics and sound (eg, Exploding may be a big mushroom cloud with a 'BOOM', Ticking will be 'ticking' and display the Timer, and Disarmed just sits there looking like a bomb)


Theres's a rough starting point... hope that helps


[edited by - Bad Monkey on November 25, 2002 2:56:58 AM]
quote:
Original post by Chaucer
make your bomb class its own thread

when you create a new bomb object (place a bomb), start the thread, have the thread wait 2 seconds, and then explode the bomb, delete the bomb object.


ROOFLE! are you trying to kill him!? ;-)

just store a nice "int creationTime" and set it to SDL_GetTicks() erm... well with SDL anyway and when SDL_GetTicks() is > creationTime + 2000....

boom
Advertisement
An event queue would be useful here don''t you think?

Lets say you have a bomb exploding event.

Everytime a bomb starts ticking, you place this event in the event queue with the time that the bomb is going to explode.

In your main loop (or whatever) you just check the first item in the queue. When the first events time is up, you explode the bomb and remove that event from the queue.

The queue used would most likely be a priority queue based on time (with earlier events having greater priority of course ).
Thanks for the comments!

How do I actually make the timer though?

Thanks again!
Adam
There''s a timing tutorial on NeHe''s site at
http://nehe.gamedev.net/tutorials/lesson.asp?l=21
It also deals with other stuff that might help in your game.
There''s also some tutorials at
http://www.gametutorials.com/Tutorials/tutorials.htm
look under OpenGL.
"If all else fails, lower your standards."
Hi again

OK I''ve been at this for days and still cant get anything working this is driving me mad! hehe

Below is the code I am currently using to keep my movement/fps speed correct and uses the timing code from a gamedev article.

class framerate
{
public:
float targetfps;
float fps;
LARGE_INTEGER tickspersecond;
LARGE_INTEGER currentticks;
LARGE_INTEGER framedelay;

float speedfactor;

void Init(float tfps);
void SetSpeedFactor();
} framer;

float tticks;

float tseconds;
void framerate::Init(float tfps)
{
targetfps = tfps;
QueryPerformanceCounter(&framedelay);
QueryPerformanceFrequency(&tickspersecond);
//startticks = framedelay;
}
void framerate::SetSpeedFactor()
{
QueryPerformanceCounter(&currentticks);
//This frame''s length out of desired length
speedfactor = (float)(currentticks.QuadPart-framedelay.QuadPart)/((float)tickspersecond.QuadPart/targetfps);
fps = targetfps/speedfactor;
if (speedfactor <= 0)
speedfactor = 1;
framedelay = currentticks;
}



I init the timer with Init() at the start of the program and call SetSpeedFactor() each frame to set the game speed.

How can I modify/add to the above code so that I can also keep a timer of elapsed seconds in real seconds since the game started please?

It might be easier now you can see the code im using

Pleeease this is driving me mad and I cant do anything else until I fix this hehe

Thanks again for all help

Adam

This topic is closed to new replies.

Advertisement