1 class Particle
2 def initialize(width, height)
3 @x, @y, @xvel, @yvel = rand(width), rand(height), rand(1)*10-5, rand(1)*10-5
4 @r, @g, @b = rand(255).to_f, rand(255).to_f, rand(255).to_f
5 @a = 0.0
6 end
7
8 def update
9 $app.animate 10 do
10 break if @a > (2 * 3.14)
11 px, py = @x, @y
12 @x += @xvel
13 @y += @yvel
14 @yvel += 0.1
15 @xvel *= -1 if (@x < 0 || @x > WIDTH)
16 @yvel *= -1 if (@y < 0 || @y > HEIGHT)
17 $app.stroke rgb(@r, @g, @b, Math.sin(@a += 0.01))
18 $app.line px, py, @x, @y
19 end
20 end
21 end
22
23 WIDTH = 400.0
24 HEIGHT = 400.0
25 Shoes.app :width => WIDTH, :height => HEIGHT do
26 $app = self
27
28 particles = Array.new(5) { Particle.new(WIDTH, HEIGHT) }
29 background white
30
31 particles.each do |p|
32 p.update
33 end
34 end
No comments:
Post a Comment