#!/usr/bin/perl -w use strict; use Term::ReadKey; my (@top, @bottom, @board, $x, $y, $barrier); my ($hieght) = 20; #hieght of the cave my ($width) = 70; #width of the cave my ($slope) = 80; #how often the standard wall hieght increases by 1 my ($count) = 0; #for waiting till $slope iterations for increasing wall hieght my ($wall_hieght) = 1; my ($score) = 0; my ($ship_char) = '>'; my ($ship_y) = int(.5 * $hieght); #where the ship is located on the y axis my ($ship_x) = 6;#where the ship is located on the x axis my ($wall_char) = ':'; my ($clear) = `clear`; #clear the screen and cache the response for (1 .. $width) {push @top, $wall_hieght;} #initilize the top wall with the initial wall hieght for (1 .. $width) {push @bottom, $wall_hieght;}#initilize the bottom wall with the initial wall hieght #main loop while (1) { generate_board(\@top, \@bottom, \@board, \$hieght); #generate board check_input(\$ship_y); #check for input and move ship if necessary print $clear; #clear screen #go through board looking for 1s (represent wall) for $y (0 .. $#board) { for $x (0 .. $#{$board[$y]}) { if ($board[$y][$x] == 1) { #on crash if ($y == $ship_y &amp;amp;&amp;amp; $x == $ship_x) { crash(\$score); } else { print $wall_char; } } else { #if ship here if ($y == $ship_y &amp;amp;&amp;amp; $x == $ship_x) { print $ship_char; } else { print " "; } } } print "\n"; } print "Score: " . $score; $barrier = int(rand($hieght-$wall_hieght))/2+int(.4 * $hieght); #odd way to calculate new barriers, made from approximations and altered after running like 9 bizillion times shift @top; shift @bottom; #only add barriers some times if (int(rand(5)) == 3) { #add barrier to top if (int(rand(2)) == 1) { push @top, $wall_hieght + $barrier; push @bottom, $wall_hieght; } else { #add barrier to bottom push @top, $wall_hieght; push @bottom, $wall_hieght + $barrier; } } else { #no barrier push @top, $wall_hieght; push @bottom, $wall_hieght; } $count++; $score++; #check to make wall hieght greater if ($count == $slope and $wall_hieght < (int(.5 * $hieght)-1)) { $wall_hieght++; $count = 0; } } sub generate_board { my ($top, $bottom, $board, $hieght) = @_; my (@tmp_top) = @{$top}; my (@tmp_bottom) = @{$bottom}; my ($check) = 0; my ($y) = 0; @{$board} = (); while ($check < ($#tmp_top+1)) { $check = 0; for (0 .. $#tmp_top) { if ($tmp_top[$_] > 0) { $$board[$y][$_] = 1; $tmp_top[$_]--; } else { $$board[$y][$_] = 0; $check++; } } $y++; } $check = 0; $y = $$hieght-1; while ($check < ($#tmp_bottom+1)) { $check = 0; for (0 .. $#tmp_bottom) { if ($tmp_bottom[$_] > 0) { $$board[$y][$_] = 1; $tmp_bottom[$_]--; } else { $$board[$y][$_] = 0; $check++; } } $y--; } for $y (0 .. ($$hieght-1)) { for $x (0 .. ($#tmp_top)) { $$board[$y][$x] = 0 unless defined($$board[$y][$x]); } } } sub check_input { my ($ship_y) = @_; my ($char); ReadMode ('cbreak'); if (defined ($char = ReadKey(-1)) ) { if ($char =~ /[Uu]/) { $$ship_y--; } elsif ($char =~ /[Jj]/) { $$ship_y++; } } ReadMode ('normal'); } sub crash { my ($$score) = @_; print "\ncrash\nScore: $$score\n"; exit(); }