bounce.rb

Ruby code posted by Jimmy Schementi
created at 11 Jun 21:39

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Bouncer
  def initialize xvelocity, yvelocity, canvas
    @xvelocity = xvelocity
    @yvelocity = yvelocity
    @canvas = canvas
  end

  def update target
    if (Canvas.get_left(target) + @xvelocity) >= (@canvas.actual_width - target.width)  or (Canvas.get_left(target) + @xvelocity) <= 0
      @xvelocity = -@xvelocity
    end
    if (Canvas.get_top(target)  + @yvelocity) >= (@canvas.actual_height - target.height) or (Canvas.get_top(target)  + @yvelocity) <= 0
      @yvelocity = -@yvelocity
    end
    Canvas.set_top  target, Canvas.get_top(target)  + @yvelocity
    Canvas.set_left target, Canvas.get_left(target) + @xvelocity
  end
end

def each_object target
  Bouncer.new rand(10) - 5, rand(10) - 5, canvas
end
748 Bytes in 2 ms with coderay