Sha256: f6f8df3b25876a48996fbb78ec1698ef3d1470acfd6c3ce8b45c36ae12a6b5d1

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

#!/usr/local/bin/ruby -w

require "graphics"

class Ball < Graphics::Body
  COUNT = 50

  G = V[0, -18 / 60.0]

  attr_accessor :g

  def initialize w
    super

    self.a = random_angle / 2
    self.m = rand 25
    self.g = G
  end

  def draw
    w.angle x, y, a, 10+3*m, :red
    w.circle x, y, 5, :white, :filled
  end

  def update
    fall
    move
    bounce
  end

  def fall
    self.velocity += g
  end
end

class BounceSimulation < Graphics::Simulation
  attr_accessor :bs

  def initialize
    super 640, 640, 16, "Bounce"

    self.bs = populate Ball
  end

  def initialize_keys
    super
    add_key_handler :SPACE, &:randomize
    add_key_handler :R,     &:reverse
  end

  def update n
    bs.each(&:update)
  end

  def draw n
    clear
    bs.each(&:draw)
    fps n
  end

  def randomize
    bs.each do |b|
      b.m = rand(25)
      b.a = b.random_angle / 2
    end
  end

  def reverse
    return if @guard
    @guard = true
    bs.each do |b|
      b.g *= -1
    end
    @guard = false
  end
end

BounceSimulation.new.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphics-1.0.0b4 examples/bounce.rb