ztriangle1


0001  #!/usr/bin/perl
0002  use warnings;
0003  use strict;
0004  use Tk; 
0005  use Tk::Zinc;
0006  
0007  
0008  my $motion_flag = 0;
0009  my $mw = MainWindow->new;
0010  $mw->geometry("700x600");
0011  $mw->resizable(0,0);
0012  
0013  my $zinc = $mw->Zinc(-width => 700, -height => 565,
0014                      -backcolor => 'black',
0015  		    -borderwidth => 3, -relief => 'sunken');
0016  $zinc->pack;
0017  
0018  # Then we create a gray filled rectangle, in which we will display explain text.
0019  $zinc->add('rectangle', 1 , [200, 400, 490, 490],
0020  	   -linewidth => 2,
0021  	   -filled => 1,
0022  	   -fillcolor => 'SkyBlue',
0023  	   );
0024  my $text = $zinc->add('text', 1,
0025  		      -position => [350, 445],
0026  		      -anchor => 'center',
0027  		      );
0028  
0029  $zinc->add('rectangle', 1 , [250,275,450,325], #(xpos1,ypos1,xpos2,ypos2)
0030  	   -linewidth => 2,
0031  	   -filled => 1,
0032  	   -fillcolor => 'Orange',
0033  	   );
0034  
0035  my $wintext = $zinc->add('text', 1,
0036  		      -position => [350, 300],
0037  		      -anchor => 'center',
0038  		      );
0039  
0040  #create triangles
0041  #create first triangle, then clone and translate
0042  my $tr1 = $zinc->add('triangles', 1,
0043                       [0,20,20,20,10,50],
0044                       -fan => 1,
0045                       -colors => 'Orange',
0046                       -visible => 1,
0047                       );
0048  my $tr2 = $zinc->clone($tr1);
0049  my $tr3 = $zinc->clone($tr1);
0050  $zinc->translate($tr1,130,0);
0051  $zinc->translate($tr2,340,0);
0052  $zinc->translate($tr3,550,0);
0053  
0054  
0055  # Display comment
0056  &comment("Strike any key to begin");
0057  &wincomment("READY");
0058  
0059  # Create Tk binding 
0060  $mw->Tk::bind('<Key>', \&start);
0061  
0062  my $closebutton = $mw->Button(text => 'Exit', -command => sub{Tk::exit(0)})
0063                 ->pack; 
0064  MainLoop;
0065  
0066  sub start {
0067     # set binding to stop and exit
0068      $mw->Tk::bind('<Key>', \&stopaction);
0069      $motion_flag = 1;
0070      &comment("Strike any key to exit.");
0071      &startaction();
0072  }
0073  
0074  sub stopaction{
0075      $motion_flag = 0;
0076      &comment("Strike any key to begin");
0077      &wincomment("READY");
0078      $mw->Tk::bind('<Key>', \&start);
0079  
0080  
0081  $zinc->coords($tr1,[100,100,100,60,60,60]); #changes shape of triangle
0082  $zinc->coords($tr2,[100,100,100,60,60,60]);               #not position
0083  $zinc->coords($tr3,[100,100,100,60,60,60]);
0084  
0085  $zinc->translate($tr1,0,-150); #changes position
0086  $zinc->translate($tr2,0,-150);               
0087  $zinc->translate($tr3,0,-150);
0088  
0089  }
0090  
0091  # Callback bound to '<Key>' event when pressed
0092  sub startaction {
0093  # move triangles
0094  
0095  $zinc->translate($tr1,0,1);
0096  $zinc->translate($tr2,0,1);
0097  $zinc->translate($tr3,0,1);
0098  
0099  if($motion_flag == 1){ $zinc->after(1, sub {startaction()})}
0100  else {return}
0101  
0102  }
0103  
0104  # Just display comment 
0105  sub comment {
0106      my $string = shift;
0107      $zinc->itemconfigure($text, -text => $string);
0108  }
0109  
0110  # display winning comment 
0111  sub wincomment {
0112      my $string = shift;
0113      $zinc->itemconfigure($wintext, -text => $string);
0114  }