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

    Your changes worked! Blessed are you among monks. At work I use Linux but use c shell for all my scripting. Virtually all of my peers use perl so thought I should see what everybody likes so much. I wanted to introduce my 9 year old to programming and thought this would be a simple program that he could play with. Thus I'm creating it with windows since my son is in a windows world. When I wrote this in basic many moons ago I used peek to return a value that would tell me if the V had hit a location with a T thus a collision. Any thoughts on how I could accomplish this with perl? The goal of the game is to move the V and avoid hitting the Ts as the scrolling slowly speeds up. I need a way to tell if a location has a character in it before the V is put in the location. If no way to do this I'll need to create a list of x,y locations that have a character. Prefer a peek type solution if anybody has one.

      The skiers view: notice the V (skier) is placed 50 lines up the screen ($skiery = $y - 50;) while the Ts show up at the bottom and scroll upward so the skier can see whats coming with time to avoid. Until other obstacles show up like "YELLOW SNOW" which is a wall of characters to avoid or "ROCK" which is only 4 characters to avoid. This to come after I work out how to tell if the V hits an existing character. :P

      I think my 9 year old and his henchmen will be amused. (For about 5 minutes)