http://www.perlmonks.org?node_id=199915
Category: Fun Stuff
Author/Contact Info legolas
Description: This is the first code I have done in a while. I wanted to make a game that would be easy to modify so people could change the game to fit their liking. I want to change it so you can be other things besides wizards and I want to add more spells monsters and areas. I think the code seems sluggish so if someone could help please respond, thank you. Tell me what you think and have fun with it.
#!usr/bin/perl -w
$wizard=50;
$wizardl=1;
$gold=5;
$damage=2;
print"Welcome to Wizard!  Type your wizard's name...";
$name=<STDIN>;
print "Welcome $name to Wizard.  You are now in the Merlins castle.  Y
+ou are surrounded  by other mages and disscussion flows back\n";
print "and forth in the room.   Suddenly Merlin stands up in front of 
+all and says, I need a powerful man to go out and slay all the monste
+rs that have been plauging my land.\n";
print "Which one of you can raise to the challenge.  Slowly all those 
+in the room rise, including yourself.\n\n";
print "Good, good merlin utters.  Then be off.\n\n\n";
sub die{
if($wizard==0){
print "Thanks for playing";
die;
}
sub toad{
my $toad=int (rand 4)+1;
if($toad<=$wizardl){
print "The goblins have been zapped into toads!\n";
$gold=$gold+3;
print "good job you got 3 gold!\n";
&freedale
}elsif($toad>$wizardl){
print "your spell has failed! -2 health\n";
$wizard=$wizard-2;
&battle;
}
    }
sub battle{
my $goblin=12;
print "What do you want to do...press 1 for spells or 2 for combat\n";
$choice=<STDIN>;
if($choice==1){
    &toad;
}elsif($choice==2){
do{
$goblin=$goblin-$damage;
$wizard=$wizard-2;
print "wizard health: $wizard\t";
print "goblin health: $goblin\n";
}until($goblin==0 || $wizard==0);
if($wizard>0){
print "good job you got 3 gold!\n";
$gold=$gold+3;
&freedale;
}elsif($wizard<=0){
 &die;
}
    }
        }

sub forest {
    print "The forest is dark and smells of old mold and rotton fish. 
+ The foul waters have turned this once peaceful place in to a\n";    
+ print "dark evil place.\n";
    my $gobbo=int (rand 6)+1;
            if ($gobbo>=5){
        print "goblins attack\n";
        &battle;
        }elsif($gobbo!=5 || $gobbo!=6){
        print "There are no monsters here now.\n";
        &freedale
        }
    }

sub mountain {
    print "Its pretty cold up here, but from this altitude you can see
+ the whole town below you.\n"; 
    print "Although it is clear that you are not the first one here!\n
+";
    my $gobbo=int (rand 6)+1;
            if ($gobbo>=4){
        print "goblins attack\n";
        &battle;
        }elsif($gobbo!=4 || $gobbo!=5 || $gobbo!=6){
        print "There are no monsters here now.\n";
        &freedale
        }
    }

sub dungeon {
    print "There is a heavy feeling in the air, it's very dark and mus
+ty down here.\n"; 
    print "You can see clear signs that goblins have been here, and ma
+ybe worse things!\n";
    my $gobbo=int (rand 6)+1;
            if ($gobbo>=3){
        print "goblins attack\n";
        &battle;
        }elsif($gobbo!=3 || $gobbo!=4 || $gobbo!=5 || $gobbo!=6){
        print "There are no monsters here now.\n";
        &freedale
        }
    }
sub shop {
    print "Hello welcome to freedale market may I help you?\n";
    print "type 1 for healing.........................................
+.....10 gold\n";
    print "type 2 for daggers.........................................
+....15 gold\n";
    print "type 3 for magic wands.....................................
+30 gold\n";
    $shop=<STDIN>;
    if($shop==1){
        if($gold<10){
        print "you don't have enough gold\n";
        &freedale;
        }
    print "Thank you\n";
    $gold=$gold-10;
    $wizard=50;
    &freedale;
        
    }elsif($shop==2){
        if($gold<15){
        print "you don't have enough gold\n";
        &freedale;
        }
    print "thank you\n";
    $gold=$gold-15;
    $damage=3;
    &freedale;
    }elsif($shop==3){
        if($gold<15){
        print "you don't have enough gold\n";
        &freedale;
        }
    print "thank you\n";
    $gold=$gold-30;
    $wizardl=$wizardl+1;
    }
}
sub stat{
    print "health:\t$wizard\n";
    print "magic level:\t$wizardl\n";
    print "gold:\t$gold\n";
    &freedale
}
sub freedale{
print "you are in freedale\n";
print "where do you want to go\n";
print "type 1 for the forest, 2 for the mountain, or 3  for the dungeo
+n\n 4 for the shop or 5 to view stats.\n ";
$room=<STDIN>;
if($room==1){
&forest;
}elsif($room==2){
&mountain;
}elsif($room==3){
&dungeon;
}elsif($room==4){
&shop
}elsif($room==5){
&stat
}
}
}
&freedale
Replies are listed 'Best First'.
Re: Wizard v1.0
by Limbic~Region (Chancellor) on Sep 22, 2002 at 17:12 UTC
    I am not sure if you had issues when you cut and pasted the code, but some things are missing:

    print "The goblins have been zapped into toads!\n"; $gold=$gold+3; "good job you got 3 gold!\n"; &freedale

    There is no print statement in front of the "good job you got 3 gold!\n"; You do not use warnings or strict, and you didn't chomp $name.

    If you do not mind, I would like to use your code as a base, and then build it up myself. I am not an RPG type of guy, but I think this would be a great excersise in developing my Perl skills because it is a large scope project. A couple of ideas that came to mind such as storing the information into a database if someone wanted to save their game. /msg me if it is ok.

    UPDATE

    I am not condeming you for the mistakes. The critism is/was meant to be contructive. I really like the game and think it is a very worthy project.

    Limbic~Region

      Hey its fine by me if you use this code I don't mind although I would like to see what you do because I'd like to see the ways you modify it. I was actually thinking of posting the code on the web so all perl programmers can see it and modify it.
        I don't want to fix all the problems in my reply - I was hoping my pointers would lead you to find your own problems. Here are some more pointers to get you started.

      • Do not create a sub called stat - use uppercase or change the name because of the Perl built in stat
      • You include subs within subs, which is not what I believe you intended to do. You have the correct number of curly braces, just not where they should be - consistent indentation is the key.
      • Your sluggishness you mentioned I am betting only occurs after playing the game a while - that is because you never let a sub properly exit, you just keep calling subs within subs

        I should have something for you to look at by tonight, but if not - /msg me (you do have a real account right?)

        Limbic~Region

Re: Wizard v1.0
by barrd (Canon) on Sep 22, 2002 at 16:53 UTC
    Just a quick note, you need an error trapping routine in the shop sub to ensure people don't type anything other than 1..3, and maybe add a "leave shop, don't buy anything" option. Oh, and the ubiquitous "use strict;" & warnings should be added ;)

    Update: minor modified code: see my scratchpad, not meaning to tread on Limbic~Region's toes but I've fixed a couple of immediate problems and given you some clues as to furthering your code with a couple of minor error traps in the same style that you wrote the original programme. I had a tad of free time so had some fun as you suggested. I would have fixed other areas but time is fleeting... Thanks for posting it, I did have fun :-)

    P.S. Limbic~Region will do a much better job I'm sure. :-)

Re: Wizard v1.0
by katgirl (Hermit) on Sep 23, 2002 at 10:40 UTC
    Your first line: #!usr/bin/perl -w

    should be: #!/usr/bin/perl -w

    I was trying to convert this thing so that it can print to a web page, and it took me nearly an hour to work out where the 500 error was coming from :) I'll paste the code when I've finished... I've always wanted a text adventure on my site, didn't know where to start!

    UPDATE:
    http://www.queenfans.com/games/ogre_battle/wizard.cgi

    I think it needs a lot more work though!

      I LOVE IT!!!!!!! Could I please take a look at the source. I think that is awsome, I'd like to post it on my site if it is okay with you /msg me next time we are both on.
Re: Wizard v1.0
by Mr. Muskrat (Canon) on Sep 23, 2002 at 17:05 UTC

    Perhaps change Merlin to merlyn. ;) With his permission of course.

    Also you should run the text to be displayed through a spellchecker.

Re: Wizard v1.0
by xCodexWarriorx (Acolyte) on Sep 04, 2004 at 05:12 UTC
    I've written a few Text-Based RPGs, but they were in C...haven't tried such a thing in Perl yet, so take my advice with a grain of salt.

    One thing you may want to look into is some OO stuff. It's really nice to take out the abstractions of rooms, players, and enemies...that kind of thing can help a lot.

    You also may want to look into using something like the d20 Open Source game engine to base your combat on...this would obviously be way over the scope of this game, but maybe just take some elements from it to base things off of. One of the hardest things about making games is balance. Making sure it's not too hard or easy is difficult (how ironic!), and basing your work off of an established system may help.
Re: Wizard v1.0
by void_Anthony() (Acolyte) on May 05, 2005 at 20:00 UTC
    I have complete revised some of the additions to my code and the game itself. I have added a new area called the "Shadow Plane", which contains a different kind of monster called the demon. They are more numerous than goblins and are 6 hp stronger, but they give 6 gold when defeat them. You must have a magic level of 3 in order to travel there. I like the idea because it gives a player a quick 5-minute goal to reach :). To see the final version, refer to my scratchpad. I have added a catch all to make sure the program doesn't just die when someone enters something besides a specified number (at least in "freedale" anyway), but I can't figure out how to account for someone pushing enter without entering anything. Any suggestions would be helpful. P.S. - I have also added a cheat sub (so I could quickly access Shadow Plane and do some testing) but I like the idea so I'm keeping it there. please forgive the code errors that you find in the modified code. im only an initiate here at the forums and a novice at perl
Re: Wizard v1.0
by void_Anthony() (Acolyte) on May 05, 2005 at 23:13 UTC
    With your permission Legolas, I would like to make these modifications (and maybe a few more debugging ones) and release Wizard v1.1. The new level, enemy, and error handling provides more variety to the game. I could possibly continue to add to (including enemies, spells(one that would only hurt the enemy but have a higher success rate), and areas) the game. Get back to me when you have time.
      legolas hasn't been logged in for 28 weeks (as of this post). It may be awhile before he gets back to you.
        Because of this minor setback, Wizardv1.1 has already been released giving legolas credit for the original code.