GTK+ で落書き 13(Ruby)


自作の Gem 'oekaki' を使っています。
oekaki | RubyGems.org | your community gem host
GTK+でお絵かきしてみた(Ruby) - Camera Obscura
 

require 'oekaki'

Width, Height = 500, 400
ColorMax = 65536
R = 20

class Star
  def initialize(ob)
    @x, @y = rand(Width), rand(Height)
    @v = Vector[rand - 0.5, rand - 0.5] * (rand + 2) * 4
     = 0
    @f = (rand < 0.5) ? 1 : -1
    @slot = ob
    @color = @slot.color(rand(ColorMax), rand(ColorMax), rand(ColorMax))
  end
  
  def draw
    x1 = @x + R * cos()
    y1 = @y - R * sin()
    @slot.star(true, @x, @y, x1, y1, @color)
    
    @x += @v[0]
    @y += @v[1]
    @x = Width  + R if @x < -R
    @y = Height + R if @y < -R
    @x = -R if @x > Width  + R
    @y = -R if @y > Height + R
    
     += @f * PI * 10 / 180
  end
end


Oekaki.app width: Width, height: Height do
  draw {clear}
  
  stars = []
  timer(50) do
    clear

    stars << Star.new(self) if stars.size <= 40 and rand < 0.1
    stars.each(&:draw)
  end
end

星形を描くメソッド star を使っています。