Heya all,
I'm trying to expand my knowledge of Perl, and I figured that coding a text adventure game would be the easiest way to do it. The first thing I am tackling is being able to "look" at the environment. I do know how to call up values of both arrays, and hashes, but I am still puzzled on how to keep the location of the player current, rather than statically coded in. Here are the two ways I've tried it so far:
With hashes:
Code:
#!/usr/bin/perl -w
#Define world HASH
@world = (
[ "", "", "" ],
[ "town", "Town", "You see several run-down buildings, and a few citizens meandering about.\n" ],
[ "forest", "Ancient Forest", "You are surrounded by tall, ancient trees.\n" ],
);
action();
#sub for getting player action
sub action {
print "What would you like to do? ";
$act = <STDIN>;
if ($act eq "look") {
look();
print $look;
print "\n";
};
};
#sub for getting look info
#take contents of %world, put into $look
#print look
sub look {
$look = $world[1][2];
print $look;
};
With Arrays:
Code:
#!/usr/bin/perl -w
#Define world HASH
%world = (
"town" => "Town\nYou see several run-down buildings, and a few citizens meandering about.\n",
"forest" => "Ancient Forest\n You are surrounded by tall, ancient trees.\n",
);
$look = "";
action();
#sub for getting player action
sub action {
print "What would you like to do? ";
$act = <STDIN>;
print "\n";
if ($act eq "look") {
look();
print $look;
print "\n";
};
};
#sub for getting look info
#take contents of %world, put into $look
#print look
sub look {
$look = $world{"town"};
};
Also, this code does not print the contents of $look for some reason, so I'm obviously doing something wrong there. Any hints?
Thanks in advance.
Bookmarks