Sha256: a56260f96707f4cae0db9bfb4fb2fb0c6d5731d37d7b47571e930e6f767481da

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 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 = rand 180
    self.m = rand 100
    self.g = G
  end

  def update
    fall
    move
    bounce
  end

  def fall
    self.velocity += g
  end

  def label
    l = "%.1f %.1f" % dx_dy
    w.text l, x-10, y-40, :white
  end

  def draw
    # w.angle x, y, a, 3*m, :red
    w.angle x, y, a, 50, :red
    w.circle x, y, 5, :white, :filled
    # label
  end
end

class BounceSimulation < Graphics::Simulation
  attr_accessor :bs

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

    self.bs = populate Ball
  end

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

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

  def handle_keys
    super
    randomize if SDL::Key.press? SDL::Key::SPACE
    reverse if SDL::Key.press? SDL::Key::R
  end

  def randomize
    bs.each do |b|
      b.m = rand(100)
      b.a = rand(180)
    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.0b1 examples/bounce.rb