Results 1 to 4 of 4

Thread: Learning Perl; Trying to code a text adventure game

  1. #1
    Join Date
    Mar 2005
    Beans
    188
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Learning Perl; Trying to code a text adventure game

    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.

  2. #2
    Join Date
    Mar 2006
    Location
    Michigan State University
    Beans
    17
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Learning Perl; Trying to code a text adventure game

    Quote Originally Posted by Rhemat View Post
    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.
    you have to chomp that CR.
    Vostro 1400 - t7200 2.5gb ram geforce 8400gs

  3. #3
    Join Date
    Mar 2006
    Location
    Michigan State University
    Beans
    17
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Learning Perl; Trying to code a text adventure game

    You have your hashes and arrays mixed up as well...

    chomp:
    chomp(my $act = <STDIN>);

    http://perldoc.perl.org/functions/chomp.html

    Might want to take a look at this book:
    http://www.learning-perl.com/

    It's a great beginner's resource and is quite inexpensive in its ebook form. DRM-free ebook available in multiple formats.
    Vostro 1400 - t7200 2.5gb ram geforce 8400gs

  4. #4
    Join Date
    Mar 2005
    Beans
    188
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Learning Perl; Trying to code a text adventure game

    Quote Originally Posted by nutrapi View Post
    You have your hashes and arrays mixed up as well...

    chomp:
    chomp(my $act = <STDIN>);

    http://perldoc.perl.org/functions/chomp.html

    Might want to take a look at this book:
    http://www.learning-perl.com/

    It's a great beginner's resource and is quite inexpensive in its ebook form. DRM-free ebook available in multiple formats.

    Thank you, I had forgotten all about chomp. I am also taking a look at the website, and seeing some interesting articles.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •