mkesling has asked for the wisdom of the Perl Monks concerning the following question:
This is my first perl program. It is a skiing program. I wrote it in basic decades ago. Thought it would be a fun way to get aquainted with perl and introduce my son to programing.
The first part randomly fills the screen with Ts that represent trees. Then it goes into a loop that puts a V on the screen that represents the skier.
This is where I am having a problem. When the j key is pressed I want the V to move to the left. When the k key is pressed I want the V to move to the right. Instead it breaks out of the loop and the text stops scrolling.
How can I fix this so the V moves and the scrolling continues. Code below.
#!/usr/bin/perl use Win32::Console::ANSI; use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; ReadMode('noecho'); cls; #fill the screen with Ts $y = 1; while ($y < 100) { $col = int(rand(80)); printf ("%${col}s\n", T); $y = $y + 1; } $wait = 1; $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 == "j") {$skierx = $skierx - 1;} if ($key == "k") {$skierx = $skierx + 1;} if ($key == "x") {exit;} }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: newbie learning perl
by davido (Cardinal) on Feb 18, 2015 at 22:08 UTC | |
by Anonymous Monk on Feb 18, 2015 at 22:35 UTC | |
by Anonymous Monk on Feb 18, 2015 at 23:24 UTC | |
|
Re: newbie learning perl
by GrandFather (Saint) on Feb 19, 2015 at 02:27 UTC | |
by Anonymous Monk on Feb 19, 2015 at 18:04 UTC | |
by GrandFather (Saint) on Feb 19, 2015 at 20:21 UTC | |
by mkesling (Initiate) on Feb 20, 2015 at 19:03 UTC | |
by GrandFather (Saint) on Feb 20, 2015 at 22:44 UTC | |
by RonW (Parson) on Feb 20, 2015 at 20:58 UTC | |
by mkesling (Initiate) on Feb 20, 2015 at 20:30 UTC |