Ruby/SDL でランダム・ウォーク

20181111000203
Ruby/SDL参照)を使ってランダム・ウォークしてみました。Linux Mint 19、Ruby 2.5.1 で確認しました。

コード。
sdl_random_walk.rb

require_relative 'sdl_draw'

WindowWidth = 300
FieldWidth = 50.0
N = 30

class Agent
  def initialize(ob)
    @x = @y = 0
    @slot = ob
  end
  
  def next_step
    @x += rand(-0.5..0.5)
    @y += rand(-0.5..0.5)
  end
  
  def paint
    ratio = WindowWidth / FieldWidth
    x = WindowWidth / 2 + @x * ratio
    y = WindowWidth / 2 - @y * ratio
    @slot.draw_circle(x, y, 5, @slot.color(255, 0, 0), true, true)
  end
end

draw(WindowWidth, WindowWidth) do
  agents = (1..N).map { Agent.new(self) }
  
  loop do
    fill_rect(0, 0, WindowWidth, WindowWidth, color(0, 0, 0))
    agents.each(&:next_step)
    agents.each(&:paint)
    flip
    sleep(0.05)
  end
end

sdl_draw.rb に関してはここを参照して下さい。簡単なライブラリみたいなものです。