Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Lunar Lander (Re: Games in Perl)

by rje (Deacon)
on Jan 10, 2002 at 23:17 UTC ( [id://137823]=CUFP: print w/replies, xml ) Need Help??

This is a silly script, but I love it because it's perl, and perl is cool. Remember how you coded games before you had graphics? So what if the screen resolution is based on your tty? So what if the graphics set is ASCII? Why should that stop you? Development time sure is faster than working on a GUI.

Of course, scripts like this might have a limited audience...

The Lunar Lander is actually a suite of 4 perl scripts, written to run in a UNIX environment. It takes unfair advantage of the UNIX system process ps() to provide control to the lunar module.

To play: place all four scripts into a directory.

Open up one terminal window for the lunar module and one window for the user input.

Run lander.pl in the first tty. If your tty is not 25 rows by 80 columns, specify your view like so:

lander.pl rows columns
To fire one of the lander's rockets, run one of the other commands:
h to move left
k to move up
l to move right
What these tiny scripts do is just pause for a fraction of a second -- enough time for the lander script to detect their presence via ps() and fire the appropriate rocket.

I know: it's an absurd, kludgey misuse (abuse?) of a perfectly good UNIX command. But it was fun to write.

rje

P.S. There's quite a bit of room for performance improvement and expansion... for instance, there are no obstacles...

h:

#!/usr/bin/perl print "fire right retros...\n"; select undef, undef, undef, 0.1;
k:
#!/usr/bin/perl print "fire main thrusters...\n"; select undef, undef, undef, 0.1;
l:
#!/usr/bin/perl print "fire left retros...\n"; select undef, undef, undef, 0.1;
lander.pl:
#!/usr/bin/perl my $theight = shift || 25; my $twidth = shift || 80; my @lm = ( ' o ', '/ \\' ); my $play = 'y'; while( $play =~ /y/i ) { my $x = 20; my $y = 5; my $dx = 0; my $dy = -0.01; # forever! { &draw( $x, $dx, $y, $dy ); $dy += 0.005; $dx *= 0.98; $x += $dx unless $x > $twidth - 5; $y += $dy; $dy = -0.1 if $dy < -0.1; $y = 1 if $y < 1; $pids = `ps -e`; $dx -= 0.05 if $pids =~ /\bh\b/; $dx += 0.05 if $pids =~ /\bl\b/; $dy -= 0.05 if $pids =~ /\bk\b/; redo unless $theight - $y < 4; } print "\nVelocity: ", $dy * 100, " m/s\n"; if ( $dy > 0.1 ) { print "I'm sorry, you have crashed!\n"; } else { print "Congratulations, you are an ace Lunar pilot!\n"; } print "Play Again?"; $play = <STDIN>; } sub draw { my ($x, $dx, $y, $dy ) = @_; `clear`; print "x: ", int($x*100), " dx: ", int($dx*100), " y: ", int(($theight-$y-4)*100), " m dy: ", int($dy*100), " m/s\n"; if ( $y > 1 ) { print "\n" x $y; print " " x $x; print $lm[0], "\n"; print " " x $x; print $lm[1]; print "\n" x ($theight - $y - 2); } else { print "\n" x ($theight-2); } }

Replies are listed 'Best First'.
ANSI based game graphics ...
by johanvdb (Beadle) on Jan 15, 2002 at 07:31 UTC
    Hi, I love ANSI games and *demos* ... if you have roots in the demoscene ( I'm an ex amiga/c64 demo coder, sort of ... ) you must love those block like characters ... remembers me of those chunky graphics. I know this is off-topic, but nostalgia strikes me! Anyone in the mood in developing an ANSI 3d demo written in Perl? J.
      I have coded b4 the Game of Life (Cellular Automata game) in Assembly for 386 machine and am thinking of redoing that in Perl! it would be interesting but I have to review the game logic first! G.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://137823]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found