mynameisG has asked for the wisdom of the Perl Monks concerning the following question:

I have a game that I am working on. It is a math game, every time the math problem is correct the user plants a crop, but if the math problem is wrong the plant dies. I want to make it so that every time the user gets the math problem right, a picture of corn appears on the screen (i.e. for 3 right math problems 3 pictures of corn appear) here is my if statement that gives the user +1 corn or +1 dead corn...

if($game eq "Plant") { $userResponse=param('userResponse'); $realtotal=$number1 + $number2; $crops=param('crops'); $deadcrops=param('deadcrops'); if ($userResponse == $realtotal) { $crops=$crops+1; } if ($userResponse != $realtotal) { $deadcrops=$deadcrops+1; }

How would I add some kind of img src to the if statement to make the pictures appear? or would it not be in the if statement? Thanks

Replies are listed 'Best First'.
Re: use perl to print multiple images
by JavaFan (Canon) on Dec 02, 2010 at 14:53 UTC
    From your use of param, I'm guessing you're using a CGI program.

    Assuming your program is generating HTML, your if statement should probably generate the HTML code that includes images.

    print '<img src = "http://www.example.com/images/crop.jpg" alt = " +Crop">' x $crops;
Re: use perl to print multiple images
by moritz (Cardinal) on Dec 02, 2010 at 14:44 UTC

    What kind of output are device are you using?

    If you simply print to the standard output, you can only display text (except through ASCII art).

    For displaying images you either need an UI toolkit (ie open a window), or a web frontend.

Re: use perl to print multiple images
by PeterPeiGuo (Hermit) on Dec 02, 2010 at 14:58 UTC

    I remember that a related question was asked several days ago... if this is the same initiative and is a web page. What you need is html img tag. You can simply print something similar to this:

    <img src="crop.jpg" alt="crop" />

    Obviously crop.jpg must exist on the server under the correct path.

    Peter (Guo) Pei

Re: use perl to print multiple images
by tospo (Hermit) on Dec 02, 2010 at 15:48 UTC
    This is obviously a web app, so it all depends on the use of your web framework (probably just a simple CGI script, I guess?) and also on how exactly you want those pictures to appear.
    You probably want to display some sort of matrix of images on the page to represent the field of plants, starting with "empty slot" images and then replacing them with alive or dead plants one by one, right? For that you need to create the matrix with unique IDs for each image source in your html output. Each time a question is answered you need to figure out the ID of the image that will be affected and change it's image source to point to an image of "empty slot", "dead plant" or "alive plant" accordingly.
    If this is handled with a web form then you also have to think about preserving the state of the field, i.e. the currently displayed images in each slot. You could do this with hidden form fields.
    A better and more modern solution would employ AJAX techniques to update pictures on the page without reloading it and some sort of web programing framework like CGI::Application or Catalyst.