Jump to content
  • Advertisement
Sign in to follow this  
ksintath

Question in C++

This topic is 4861 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

void wait ( float seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

I want to ask a question.For example there is a for loop.In each loop user can enter one variable(Ex: cout>>x ) or wait for one seconds using function like above wait(Ex: wait(1)).[[Function is not mine.I found it in internet and using in my codes.]].How can i define it in C++?. I know "wait(1); || cout>>x;" is not working:(

Share this post


Link to post
Share on other sites
Advertisement
cout<<x; will print the value of x.
to accept user input, you would need to use cin>>x;

other than that i don't understand what you are asking.

Share this post


Link to post
Share on other sites
I'm not completely sure i understand what you're asking. It seems like you want to wait for a user input for x seconds and if no key is pressed do something else. If that is the case the easiest way to do it would be something like this


#include <iostream>

using namespace std;

void main()
{
int keyPressed;
do
{
keyPressed=getchar(); //get the input

//do whatever you want with the input

putchar(keyPressed); //for example output it back to the screen

}while(currentTime < maxTime); //dont wait past the max allowed time
}




if i'm way off about what you're asking, could you be a little more specific about what you're trying to do? i'd love to help more, but thats the best i can make of your problem :-\

Share this post


Link to post
Share on other sites
That function definition should compile just fine in C++. The only things to be wary of are that you include <ctime> instead of <time.h>, and you should change CLK_TCK to CLOCKS_PER_SEC (these are equivalent, but CLK_TCK is considered obsolete according to MSDN).

Share this post


Link to post
Share on other sites
Quote:
Original post by EricmBrown
I'm not completely sure i understand what you're asking. It seems like you want to wait for a user input for x seconds and if no key is pressed do something else. If that is the case the easiest way to do it would be something like this

*** Source Snippet Removed ***

if i'm way off about what you're asking, could you be a little more specific about what you're trying to do? i'd love to help more, but thats the best i can make of your problem :-\


First thanks for your help,
I just wanted to create a simple spaceship game in text mode.The world is designed as double array.The ship only goes up or down.There is random generator which creates X(think as asteroids)in 1 seconds for example.And there is a function which shitfts each X to left and deletes old X each turn.The problem is ,i coded all functions in a for loop and which loops until a X crashes to ship('O').But if i put "cin>>" in loop it asks user to enter a value each turn and stops the movement.So i asked the question:( If user doesn' t enter something in 1 second for example the loop will continue,and if user enters an input the loop will continue also.I hope i can explain it clearly:(

Share this post


Link to post
Share on other sites
So essentially you want a way to take the input without stopping the game? I dont know any efficient way to do that in a console application. The easiest way to solve the problem is to use kbhit() and getch() from the conio.h library. similar to this:



char keyPressed = 'o'; //set equal to something not used so you dont crash the app!
while(quit != true) //main game loop
{
//do stuff

if(kbhit())
{
keyPressed = getch();

if(keyPressed == 'a' || keyPressed == 'A')
{
//do something when A is pressed
}
else if(keyPressed == 'w' || keyPressed == 'W')
{
//do something else
}
}
}




this will allow you to check for input without stopping the game loop. kbhit() is false unless any key on the keyboard is pressed. getch() takes input without requiring you to press enter. so basically you are checking
IF(a key is pressed)
THEN(get the key pressed and respond to the input);

I hope that helps. if i said something unclear or you dont understand the code just let me know and i'll help as much as i can. GL w/ that game, i'd love to see it when you're done! :)

Share this post


Link to post
Share on other sites
cin and cout arent suitable to realtime games.
The next step you need is allegro or if you feel ready SDL or directX.

For your game use the kbhit/getchar approach EricmBrown suggested.
Use cin when you want to input a string and the game stops, such as maybe for the top-10 names list.
note that arrows and f-keys return two asci codes zero and then a number.
also note your wait function uses 100% CPU for idle time. It will be better if you find a sleep function that makes the OS give the time to other processes.
The good thing about your wait function is that you can stop in the middle of the wait if a key is hit, but i wont use that in case you find a sleep function to replace it.

Eric's code neglected delaying the game, here would be my fix.



char keyPressed = 'o'; //set equal to something not used so you dont crash the app!
while(quit != true) //main game loop
{
// do stuff
updateMeteors();
updateMissles();
updateExplosions();

// wait only a small amount, we dont want keyboard press to effect the
// game a second after they are hit, even 0.1 second is abit too long.
// if no key is hit this loop loops around 10 times per second
wait(0.1);

if(kbhit())
{
keyPressed = getch();
quit = handleKeys(keyPressed);
}

drawScreen();
}


bool handleKeys(char keyPressed)
{
if(keyPressed == 'a' || keyPressed == 'A')
{
//do something when A is pressed
}
else if(keyPressed == 'w' || keyPressed == 'W')
{
//do something else
}
if (keyPressed == 0)
{
keyPressed = getchar();
if (keyPressed == __) // i forget code for f1
help();
else if (keyPressed == __) // i forget code for arrow key down
moveDown();
...
}
}

// now this function is being called 10 times a second (because we are waiting
// 0.1 seconds). So if you want to move the meteors only once per second you
// can count times it looped here.
// if you want to make the meteors go faster you can decrease the "slowness"
// when "slowness" reaches 1 the meteors will move 10 times per second
void updateMeteors()
{
static int called = 0;
static int slowness = 10;

called++;
if (called >= slowness)
{
called = 0;

// move the meteors
}
}



Iftah.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!