How to stop input in Perl?

Posted by user1472747 on Stack Overflow See other posts from Stack Overflow or by user1472747
Published on 2012-06-21T16:48:28Z Indexed on 2012/06/26 9:16 UTC
Read the original article Hit count: 198

Filed under:
|
|

First time poster and part time perl noobie.

I'm making a reflex game. Here's the output -

__________________________________________________________________________
Reflex game initiated. Press ENTER to begin the game, and then press ENTER 
after the asterisks are printed to measure your reflexes!.


*************************

Your result: 0.285606 seconds.
logout

[Process completed]
__________________________________________________________________________

There's one small problem though - There's 0-10 seconds (based on a random variable) after you press enter to start the game and before the stars are printed. During that time, if the player presses ENTER, it's logged as their reflex time. So I need a way to stop my code from reading their ENTER button before the stars are printed. The code -

#!/usr/bin/perl

use Time::HiRes qw(sleep);
use Time::HiRes qw(gettimeofday);

#random delay variable
$random_number = rand();

print "Reflex game initiated. Press ENTER to begin the game, and then press ENTER after         the asterisks are printed to measure your reflexes!.\n";

#begin button
$begin = <>;

#waits x milliseconds
sleep(10*$random_number);

#pre-game
$start = [ Time::HiRes::gettimeofday() ];

print "\n****************************\n";

#user presses enter
$stop = <>;

#post game
$elapsed = Time::HiRes::tv_interval($start);

#delay time print
print "Your result: ".$elapsed." seconds.\n";

© Stack Overflow or respective owner

Related posts about perl

Related posts about game-development