DXRubyExtensionの簡単な使い方の例

見た目が賑やかで複雑なサンプルよりも、こういうもののほうがよかったかもしれない。

require 'dxruby'
require 'dxrubyex'

class Toufu
  attr_reader :collision, :x, :y

  @@image = Image.new(50,50,[255,255,255])

  def initialize
    @x = rand(590)
    @y = rand(430)
    @dx = rand(2) * 2 - 1
    @dy = rand(2) * 2 - 1

    # collisionオブジェクトに指定する値はキャラの左上を(0,0)とした範囲
    @collision = CollisionBox.new(self, 0, 0, 49, 49)
  end

  def update
    @x += @dx
    @y += @dy
    @dx = -@dx if @x < 0 or @x > 590
    @dy = -@dy if @y < 0 or @y > 430
  end

  def draw
    Window.draw(@x, @y, @@image)
  end

  # 衝突検出時に呼ばれるメソッド
  def hit(obj)
    if (@x - obj.x).abs < (@y - obj.y).abs
      @dy = -@dy
    else
      @dx = -@dx
    end
  end
end

Objects = Array.new(10) { Toufu.new }

Window.loop do
  Objects.each { |obj| obj.update }

  # Array#mapを使ってToufuの配列からcollisionの配列を生成する
  objs = Objects.map { |obj|
    # 基準座標を移動。setの戻り値はCollisionオブジェクトなのでそのまま配列へ。
    obj.collision.set(obj.x, obj.y)
  }

  # collision配列同士の衝突判定
  Collision.check(objs, objs)

  Objects.each { |obj| obj.draw }
end