Leser: 2
3 Einträge, 1 Seite |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#!/usr/bin/perl -w use strict; use Tk; my $moveX = 1; my $moveY = 1; my ($stop, $speed); my $mw = tkinit(); my $field = $mw->Canvas(-background => 'white') ->pack(-side => 'top', -fill => 'both', -expand => 1 ); my $rect = $field -> createRectangle(0, 0, 10, 10, -fill => 'blue'); my $b1 = $mw->Button(-text => 'Move it!', -command => sub{ $stop = 0; &moveSquare(); }) ->pack(-side => 'left'); $mw->Scale(-from => 1, -to => 10, -label => 'Speed', -orient => 'horizontal', -variable => \$speed) ->pack(-side => 'left'); my $b2 = $mw->Button(-text => 'Stop it!', -command => sub{ $stop = 1 }) ->pack(-side => 'left'); MainLoop; sub moveSquare { my @colors = qw/red yellow blue orange pink green gray white/; for (;;) { last if $stop; my $height = $field->height; my $width = $field->width; my @coords = $field->coords($rect); print join ';', @coords; print $/; #generate random colors my $rect_color = $colors[rand(scalar @colors)]; my $bg_color; do {$bg_color = $colors[rand(scalar @colors )]} while ($rect_color eq $bg_color); #prevent rectangle from going out at left side if ($coords[0] < 1) { $moveX = 1; conf_field($field, $bg_color, $rect_color); } #prevent rectangle from going out at top if ($coords[1] < 1) { $moveY = 1; conf_field($field, $bg_color, $rect_color); } #prevent rectangle from going out at right side if ($coords[2]+1 > $width-1) { $moveX = -1; conf_field($field, $bg_color, $rect_color); } #prevent rectangle from going out at bottom if ($coords[3]+1 > $height-1) { $moveY = -1; conf_field($field, $bg_color, $rect_color); } $field->move($rect, $moveX, $moveY); $mw->after(10-$speed); $mw->update; } } sub conf_field { my ($field, $bg_color, $rect_color) = @_; $field->configure(-background => $bg_color); $field->itemconfigure($rect, -fill => $rect_color); }
1
2
3
4
5
6
7
8
9
10
11
12
13
.
.
.
332;104;342;114
331;103;341;113
330;102;340;112
329;101;339;111
328;99.9999999999999;338;110
327;98.9999999999999;337;109
326;97.9999999999999;336;108
.
.
.
QuoteAnsonsten würde ich auf die üblichen Float-Ungenauigkeiten verweisen --- gab es da nicht kürzlich einen Thread in diesem Forum?
3 Einträge, 1 Seite |