Sign in to follow this  

Any way to make random movement for demo?

This topic is 3149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an assignment that has me making my human car character move randomly for the demo mode. The way its controlled now is using the MoveCarRight() or MoveCarLeft() and using GetAsyncKeyState(VK_RIGHT) in the functions. I tried making them equal to something for random movement, but it doesn't like the GetAsyncKeyState(VK_LEFT)= 1 or something of the sort. the function i use to make them move by human means is

	void MoveCarRight()
	{
		// Move the car based upon left/right key presses
    POINT ptVelocity = g_pCarSprite->GetVelocity();
		//if (GetAsyncKeyState(VK_RIGHT) < 0)
		//{
 // Move right
      ptVelocity.x = min(ptVelocity.x + 2, 6);
      g_pCarSprite->SetVelocity(ptVelocity);
		//}
	  
	}


and call it by

if (GetAsyncKeyState(VK_RIGHT) < 0)
{
MoveCarRight();
}


Is it possible to make the movement random for a demo mode, without me pressing the buttons?

Share this post


Link to post
Share on other sites
Couldn't you just use a timer each frame and randomly change directions every so often?


//global or something
DWORD timer = GetTickCount();
srand(GetTickCount()); //seed random number generator with tick count

//each frame
if (timer + 1000 < GetTickCount() ) //if more than a second has passed since the last movement
{
int direction = ( rand() % 2 ) //this may not be right but I didn't look it up get a random number between 1 and 0
if (direction==0)
MoveCarRight()
else if (direction==1)
MoveCarLeft()


}







[Edited by - yewbie on May 2, 2009 3:29:41 AM]

Share this post


Link to post
Share on other sites
Have a demo boolean, and if the game is currently in demo mode, ignore the check for input and move. This would probably rely on some sort randomness to determine which direction the car should move.

if (demoMode)
{
// generate number 0-1
// 0 = left, 1 = right
static int direction = rand() % 2;
// hold the direction for anywhere between 0-1000 milliseconds
// replace timeGetTime with a timer function of sorts relevant to your use
static int holdTime = timeGetTime() + (rand() % 1001);

// if the key-hold time expired...
if (timeGetTime() > holdTime)
{
// generate another direction
direction = rand() % 2;
// generate another time
holdTime = timeGetTime() + (rand() % 1001);
}

if (direction == 0)
{
MoveCarLeft();
}
else if (direction == 1)
{
MoveCarRight();
}
}
else
{
// check for human input here
}


Share this post


Link to post
Share on other sites
Make your own func to replace the GetKeyState function. Inside of it, you can randomly set the "state" of the "key" and a duration. Everytime you call the function, you can check to see if the duration has expired, then randomly set it again.

Share this post


Link to post
Share on other sites
Essentially, yes. However, you can make a function that calls GetAsyncKeyState(...) if key mode isn't emulated, and artificially set key states if it is.

Share this post


Link to post
Share on other sites

This topic is 3149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this