in reply to Re^3: newbie learning perl
in thread newbie learning perl

I added use strict and use warnings. Yikes, lots of errors. I tried to clean them all up. Down to one I cant figure out. Global symbol "@xloc" requires explicit package name The array xloc is used in 2 locations so generates this warning twice. Each item in the array represents the x location of a T and the location in the array represents the y location. The line: if ($skierx eq $xloc$skiery) {print "CRASH!!!"; exit;} is how I detect that the skier has hit a tree. A different warning said I should use $ instead of @ infront of xloc. I dont get it. I seek the wisdom of the monks.

#!/usr/bin/perl use strict; use warnings; use Win32::Console::ANSI; use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; ReadMode('noecho'); cls; my $col = 0; my $skierx = 40; my $skiery = 0; my $score = 0; my $key = 0; my $wait = .3; my $y = 1; #fill the screen with Ts while ($y < 100) { $col = int(rand(80)); printf ("%${col}s\n", "T"); $y = $y + 1; } $wait = 1; $skierx = 40; $skiery = 0; $y = $y + 5; locate $y,1; print " ------------------------- Ready Set GO! --------------- +----------"; while (1 < 2) { while (not defined ($key = ReadKey(-1))) { # here when no key pressed # place a T and save X location in array, position in array = Y $col = int(rand(80)); $y = $y + 1; $xloc[$y] = $col; locate $y,$col; print "T\n"; # place V $skiery = $y - 50; locate $skiery,$skierx; print "V"; #crash??? if ($skierx eq $xloc[$skiery]) {print "CRASH!!!"; exit;} #score if ($skiery < 105) {locate $skiery,1; print "PRACTICE";} if ($skiery > 105) {$score = $score + 1;locate $skiery,1; print +"$score";} # keep speeding up, reducing pause time select(undef, undef, undef, $wait); #pauses here #$wait = $wait - .005; if ($score eq 50) {$wait = .25;} if ($score eq 100) {$wait = .2;} if ($score eq 150) {$wait = .15;} if ($score eq 200) {$wait = .1;} } # here when key pressed if ($key eq ",") {$skierx = $skierx - 1;} if ($key eq ".") {$skierx = $skierx + 1;} if ($key eq "x") {exit;} } END{ ReadMode('restore'); }

Replies are listed 'Best First'.
Re^5: newbie learning perl
by GrandFather (Saint) on Feb 20, 2015 at 22:44 UTC

    There are a goodly number of smallish issues in your code and a few outright bugs. Some of the issues clean up if you go back and re-read the scoping reply I wrote above.

    I've reworked your current code to clean up variable scope and to correctly handle lines and collision detection. Look carefully at how @xloc is managed.

    I've also removed the END block code and instead put the main work in a sub wrapped by appropriate ReadMode calls.

    As icing a few blank lines are put at the start of the run to give skiers time to get their eye in and you can tune the number of trees generated per line.

    #!/usr/bin/perl use strict; use warnings; use Win32::Console::ANSI; use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; my $kLines = 50; my $kWidth = 80; my $kMaxTrees = 5; my $kPractise = 5; ReadMode('cbreak'); cls; run(); ReadMode('restore'); sub run { my @xloc; locate 1, 1; my $hLen = ($kWidth - 15) / 2 - 10; print ' ' x 5, '-' x $hLen, " Ready Set GO! ", '-' x $hLen; push @xloc, ' ' x $kWidth for 1 .. 4; push @xloc, newLine($kWidth) for 1 .. $kLines - 2; locate 2, 1; print join "\n", @xloc, ''; my $wait = 1; my $skierx = $kWidth / 2; my $y = $kLines; my $score = 0; while (1 < 2) { my $key = ReadKey(-1); if (defined $key) { # here when key pressed --$skierx if $key eq ","; ++$skierx if $key eq "."; return if $key eq "x"; } push @xloc, newLine($kWidth); # Generate a new line at the end locate ++$y, 1; print "$xloc[-1]\n"; #crash??? if ('T' eq substr $xloc[0], $skierx - 1, 1) { print " CRASH!!! "; return; } # place V shift @xloc; # Remove the previous line locate $y - $kLines + 2, $skierx; print "V"; #score locate $y - $kLines + 1, 1; if ($y < $kPractise + $kLines) { print "PRACTICE"; } else { print ++$score; } # keep speeding up, reducing pause time select (undef, undef, undef, $wait); #pauses here $wait = .25 if $score eq 50; $wait = .2 if $score eq 100; $wait = .15 if $score eq 150; $wait = .1 if $score eq 200; } } sub newLine { my ($width) = @_; my $line = ' ' x $width; substr $line, rand $width, 1, 'T' for 1 .. rand $kMaxTrees; return $line; }

    Please read the Perl docs for anything you can't understand, or ask here if you really can't figure it out. But do work through the code line by line and understand what is going on.

    Oh, and really don't put multiple statements on a line!

    Perl is the programming world's equivalent of English
Re^5: newbie learning perl
by RonW (Parson) on Feb 20, 2015 at 20:58 UTC

    Add a declaration for @xloc

    cls; my @xloc; my $col= 0;
Re^5: newbie learning perl
by mkesling (Initiate) on Feb 20, 2015 at 20:30 UTC

    When I comment out use strict and use warnings and ignore the problems in the prior post the game works as intended. However when the score gets to 144 an extra line is added to the addition of the T obstacles and after a few loops the skier gets moved to the bottom of the page. I have no idea why this happens at score of 144. This repeats every time. I changed this so the comma key moves the skier to the left and the period key moves the skier to the right. This game is working except for problem above on my windows 7 machine. Just for fun going to try and get it to work on my linux machine.