Particle fountain: Difference between revisions

no edit summary
m (→‎{{header|Raku}}: Use an enumerated event rather than a magic number)
No edit summary
Line 15:
[https://github.com/thundergnat/rc/blob/master/img/fountain-raku.mp4?raw=true Off-site link to a demo video]
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Particle_fountain
use warnings;
use Tk;
 
my $size = 900;
my @particles;
my $maxparticles = 500;
my @colors = qw( red green blue yellow cyan magenta orange white );
 
my $mw = MainWindow->new;
my $c = $mw->Canvas( -width => $size, -height => $size, -bg => 'black',
)->pack;
$mw->Button(-text => 'Exit', -command => sub {$mw->destroy},
)->pack(-fill => 'x');
 
step();
MainLoop;
-M $0 < 0 and exec $0;
 
sub step
{
$c->delete('all');
$c->createLine($size / 2 - 10, $size, $size / 2, $size - 10,
$size / 2 + 10, $size, -fill => 'white' );
for ( @particles )
{
my ($ox, $oy, $vx, $vy, $color) = @$_;
my $x = $ox + $vx;
my $y = $oy + $vy;
$c->createRectangle($ox, $oy, $x, $y, -fill => $color, -outline => $color);
if( $y < $size )
{
$_->[0] = $x;
$_->[1] = $y;
$_->[3] += 0.006; # gravity :)
}
else { $_ = undef }
}
@particles = grep defined, @particles;
if( @particles < $maxparticles and --$| )
{
push @particles, [ $size >> 1, $size - 10,
(1 - rand 2) / 2.5 , -3 - rand 0.05, $colors[rand @colors] ];
}
$mw->after(1 => \&step);
}</lang>
 
=={{header|Raku}}==
Anonymous user