我正在做一个反射游戏。这是输出 -
__________________________________________________________________________
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]
__________________________________________________________________________
不过有一个小问题 - 在您按下回车键开始游戏之后和打印星星之前有 0-10 秒(基于随机变量)。在此期间,如果玩家按下 ENTER,则会记录为他们的反射时间。所以我需要一种方法来阻止我的代码在打印星星之前读取他们的 ENTER 按钮。编码 -
#!/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";