I've reworked your code somewhat. The most important change is to add strictures (use strict; use warnings; - see The strictures, according to Seuss), but I've also added the start of collision detection (which may have an off by 1 bug in it), allowed the number of display rows to be set and refactored the code somewhat (in part to show off some Perl stuff like join and arrays).

This code is not tested because I can't quickly find a working ANSI Terminal emulation for Windows 8.

#!/usr/bin/perl use strict; use warnings; use Term::ANSIScreen qw/:color :cursor :screen/; use Term::ReadKey; my $rows = 50; ReadMode('noecho'); cls; setscroll (1, $rows); locate 1, 1; my @screenRows; # Create the initial screen push @screenRows, newRow() for 1 .. $rows; print join "\n", @screenRows, ''; my $wait = 1; my $skierx = 40; while (1) { if (defined(my $key = ReadKey(-1))) { if ($key eq "j") { --$skierx; } elsif ($key eq "k") { ++$skierx; } elsif ($key eq "x") { exit; } } # Generate and show a new line shift @screenRows; push @screenRows, newRow(); print $screenRows[-1]; # Collision detection if (' ' ne substr $screenRows[$rows / 2], $skierx - 1, 1) { locate $rows, 1; print "Crash!\n"; exit; } # Show skier's updated position locate $rows / 2, $skierx; print "V"; # wait select(undef, undef, undef, $wait); #pauses here $wait -= 0.003; } sub newRow { my $col = int(rand(80)); return ' ' x $col . 'T' . (' ' x (78 - $col)); }
Perl is the programming world's equivalent of English

In reply to Re: newbie learning perl by GrandFather
in thread newbie learning perl by mkesling

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.