in reply to newbie learning perl
Use a ReadMode of 'cbreak', not 'noecho'. The mode you've been using ('noecho') is buffered, which is not what you want. Also, be sure to set the ReadMode to 'restore' prior to terminating, or you'll end up with a wonky terminal! :)
Finally, your $key relational tests need to use eq instead of ==, since it needs to be a stringwise comparison. With these changes it should work for you. ...at least the keyboard input part works. There's also a user-experience issue: The skier has no view of what's ahead, only what is behind or to the side. That would make for a terrible skiing experience! ;)
See below:
use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; ReadMode('cbreak'); cls; #fill the screen with Ts $y = 1; while ($y < 100) { $col = int(rand(80)); printf ("%${col}s\n", T); $y = $y + 1; } $wait = .5; $skierx = 40; $skiery = 0; $y = $y + 5; while (1 < 2) { while (not defined ($key = ReadKey(-1))) { # No key pressed $col = int(rand(79)); $y = $y + 1; locate $y,$col; print "T\n"; $skiery = $y - 50; locate $skiery,$skierx; print "V"; #locate $y,1; print "$skiery $skierx $key"; select(undef, undef, undef, $wait); #pauses here $wait = $wait - .003; } # here when key pressed if ($key eq "j") {$skierx = $skierx - 1;} if ($key eq "k") {$skierx = $skierx + 1;} if ($key eq "x") { exit;} } END{ ReadMode('restore'); # Don't leave the terminal wonky. print "\n"; # Don't leave the prompt in a weird place. }
Also, on my Linux system the use of the Win32 related module was not necessary (and of course, not possible). I don't see anything in the script that actually makes use of that module. Are you sure it's necessary? (It may be; I don't do much with Windows these days, so I'm only speculating that it's not.)
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: newbie learning perl
by Anonymous Monk on Feb 18, 2015 at 22:35 UTC | |
by Anonymous Monk on Feb 18, 2015 at 23:24 UTC |