Jump to content

C++ Timer question & number generator problem

- - - - -

  • Please log in to reply
5 replies to this topic

#1
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
Hi there

I have a question, I want to create a game- with bombs falling down from the top of the screen.

I have tried to create so that 10 bombs are generated and placed on a special position depending on what number the number generator gives.

The first problem I got is that when I run the program the generated number is never the same which in turn say every number from 1 to 10 is used and they are falling down.

The first question I got is how to create an timer to tell the program to wait 1-2 seconds between each creation of the bombs?

I want it so that the program works at the following way:
each second a bomb is created at a specific point depending on the number(already done) each second, so that not every bomb is created at the same time.

Code for the generator:

srand( ( unsigned ) time( 0 ) );

	int random_integer;


for( int index = 0; index < 20; index++ )

	{

		random_integer = ( rand()%10) +1;


		switch( random_integer )

		{

		case 1: addBomb(20, 0, 0.01f);  break;

		case 2: addBomb(120, 0, 0.01f); break;

		case 3: addBomb(230, 0, 0.01f); break;

		case 4: addBomb(400, 0, 0.01f); break;

		case 5: addBomb(550, 0, 0.01f); break;

		case 6: addBomb(660, 0, 0.01f); break;

		case 7: addBomb(770, 0, 0.01f); break;

		case 8: addBomb(880, 0, 0.01f); break;

		case 9: addBomb(990, 0, 0.01f); break;

		case 10: addBomb(1100, 0, 0.01f); break;

		}

	}




Oh and at the same time I might as well ask about a question I think I know the answer to but not sure:

When the bombs are created i use an update method in the game loop, and each time I set it to:


void Bombs::Update()

{

	y += speed;

}


So far I don't got any frames per second to limit the number of times it runs, and when I run the program the bombs falls down very fast, even though its just 1.0f.

What I think it is, is because of that I don't have any fps limit on how many time it runs/sec, or am I wrong ?

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
On Windows you can call Sleep and on Unix I think it's sleep or usleep; Sleep and sleep take time in ms while usleep takes micro seconds.

And you're right about speed - because you don't see frame limit, bombs move very fast. You should however use time for movement. See this SDL tutorial for it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
okey =) thanks, will check it out, on SDL I checked and some method: SDL_Delay() also takes the time in ms, is this the same thing as sleep?

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Yes.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
Well about the number generator, is it anything wrong since all bombs are created at the same time or it's just placed in the wrong position in the code?

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I assume your generator for loop is executed before main loop. That's why you have all (20?) bombs generated instantly.

But what you probably want is the create bombs as the game is running. After a certain amount of time has passed (use a timer to measure this), create new bomb (since you're using std::vector you can simply push it back and thus you don't need to modify code for Update method) with random x coordinate.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users