Dear All,
I wanted to create some educational games for my child. I created this pair matching game. I would like to share it with you all. The rules of the game are very simple
1) Store this code in one folder. Create a folder 'images' and store 35 gif images of approx 40x40 dimensions. (For 70 squares you need 35 images)
2) start it using command 'perl pairs.pl', which will display 70 buttons.
3) Click each button and try to find it match.
4) if you are able to find match then the picture on the button will remain constant or else it will vanish.
Please enjoy the game, since it was done as a passtime excersize the code is Not properly commented. Please forgive me for this. Do let me know your views.
code for 'pairs.pl'
######################
use strict; use Tk; use Tk::Button; use Tk::Canvas; use Tk::Photo; use Tk::Splashscreen; #use fileoprn; require Tk::Splash; ##################### my @filearray; opendir( DIR, 'images' ) or die "Can't open images DIR $!"; while ( my $entry = readdir( DIR ) ) { my $type = ( -d "images\\$entry" ) ? "dir" : "file"; push @filearray, "images\\$entry" if (($type eq "file") && ($e +ntry =~ /\.gif$/)); #print "$type\t$entry\t@filearray\n"; } closedir( DIR ); @filearray = (@filearray,@filearray); #################### fisher_yates_shuffle(\@filearray); unshift @filearray, "NIL"; #foreach (@filearray) {print $_,"\n";}; my @arranged_gifs; my $top = new MainWindow(); my $loop_cntr=0; my $state_of_button=-1; my $prev_x = undef; my $prev_y = undef; my $number_of_clicks=0; my $prev_clicked_button=undef; my $button_width= 10; my $button_height= 3; foreach my $x (1..10) { for my $y (1..7) { $loop_cntr++; $arranged_gifs[$x][$y]= $filearray[$loop_cntr]; #print "$loop_cntr:$x:$y:-".$arranged_gifs[$x][$y]."\t".$filea +rray[$loop_cntr]."\n"; my $button = $top->Button(-width => $button_width,-background= +>'yellow', -height =>$button_height,-command=>sub{click_button($x,$y, +\$top->gridSlaves(-row=>$x,-column=>$y), \$prev_clicked_button)}); $button->configure(-text=>'Hey'); $button->grid(-row =>$x , -column =>$y); } } sub click_button($$$){ my ($myx,$myy,$clicked_button,$prev_clicked_button)=@_; if (!${$clicked_button}->cget(-image)) { $number_of_clicks++; my $mywait; my $image = $top->Photo(-format => 'gif' , -file=>$arranged_gi +fs[$myx][$myy]); ${$clicked_button}->configure(-width => 50, -height =>40, -ima +ge=>$image); if ($number_of_clicks == 1) { print "Hello\n"; ${$prev_clicked_button} = ${$clicked_button}; } if ($number_of_clicks == 2) { print "pair\n"; #sleep(2); $number_of_clicks=0; print "#".$arranged_gifs[$myx][$myy]."#". $arranged_gifs[$ +prev_x][$prev_y]."\n"; if ($arranged_gifs[$myx][$myy] ne $arranged_gifs[$prev_x][ +$prev_y]) { ${$prev_clicked_button} -> after (250, [\&hide, ${$pre +v_clicked_button} ]); ${$clicked_button} -> after (250, [\&hide, ${$cli +cked_button}]); } else { print "Got it\n"; } } $prev_x = $myx; $prev_y = $myy; } } sub hide { shift -> configure (-width => $button_width, -height =>$button_hei +ght, -state=>'normal',-image => undef); } sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } MainLoop;
Regards,
SanPerl

In reply to Tk: pair matching game by sanPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.