Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

-tk Moving circle

by tamaguchi (Pilgrim)
on Feb 27, 2007 at 09:13 UTC ( [id://602272]=perlquestion: print w/replies, xml ) Need Help??

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

I have a blue canvas with a yellow circle:
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; my $c = $mw->Canvas( -width => 500, -height => 500, -background =>'blue' ); $c->pack(-expand => 1, -fill => 'both'); my $rect=$c->createOval(100, 150, 120, 170, -fill => 'yellow'); MainLoop;
Is there any good way you could think of that would make the circle move over the canvas at moderate speed?
(Not linked to any keypress).
Thank you for any help

Replies are listed 'Best First'.
Re: -tk Moving circle
by bart (Canon) on Feb 27, 2007 at 11:24 UTC
    It's typical for Tk that you can edit the properties of objects on a canvas without recreating a new instance of this object. So, don't create a new oval, but instead, try to move it. Tk will take care of redrawing it for you.

    The docs state:

    These same option-value pairs may be used in itemconfigure methods to change the item's configuration.
Re: -tk Moving circle
by zentara (Archbishop) on Feb 27, 2007 at 12:55 UTC
    You set tags on the items you want to move, then setup a timer to move them. Don't use "sleep" ! Use a timer ( i.e. a repeat).

    P.S. You really don't need the $group, tags by themselves are just as effective.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); # first create a canvas widget my $canvas = $mw->Canvas(width => 300, height => 200)->pack(); my $one = $canvas->createOval(5, 0, 20, 30, -fill => 'blue',-tags => +['fred']); my $two = $canvas->createRectangle(0, 20, 50, 75, -fill => 'red',-tags + => ['fred']); #my $group = $canvas->createGroup([0, 0], -members => [$one, $two]); my $group='fred'; my $dx = 1; my $dy = 1; my $id = Tk::After->new($canvas,10,'repeat', sub {$canvas->move($group, $dx,$dy); my ($x,$y) = $canvas->bbox($group); print "$x $y\n"; if($y >= 100){$dy = -1}; if($x >= 200){$dy = 1; $dx = -1}; if($x < -5){$dx=1;$dy=1}; }); my $ebutton = $mw->Button(-text => 'Exit', -command => 'Tk::exit')->pack(); my $sbutton = $mw->Button(-text => 'Stop', -command => sub{$id->cancel;})->pack(); MainLoop();

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: -tk Moving circle
by Samy_rio (Vicar) on Feb 27, 2007 at 09:42 UTC

    Hi tamaguchi, try like this,

    use strict; use warnings; use Tk; my $top = MainWindow->new(); my $canvas = $top->Canvas(width=>300, height=>245, background=>'blue') +->pack(); my $origin_x=110; my $origin_y=70; my $PI=3.141592635; my $circle_radius=5; my $path_radius=0; for (my $angle=0; $angle<=180; $path_radius+=7, $circle_radius+=3, $an +gle+=10) { my $path_x=$origin_x+$path_radius*cos($angle*$PI/90); my $path_y=$origin_y-$path_radius*sin($angle*$PI/90); $canvas->createOval($path_x-$circle_radius, $path_y-$circle_radius, $path_x+$circle_radius, $path_y+$circle_radius, -fill=>'yellow'); $canvas->update(); sleep(1); } MainLoop();

    I got this code in perlmonks only.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://602272]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-23 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found