Sha256: 9ff88df9232385992d74f19b775d700ca98e661a06e9689c8edc9f64ca82d406

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

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

require "graphics"

class Sprite < Graphics::Body
  COUNT = 8

  attr_accessor :image

  def initialize w
    super w

    self.a = random_angle
    self.m = 5
  end

  def update
    move
    bounce
  end

  def collide
    self.a = (a + 180).degrees
  end

  def draw
    w.blit image, x, y, a
  end

  def collide_with? other
    w.cmap.collision_check(x, y, w.cmap, other.x, other.y) != nil
  end
end

class Collision < Graphics::Simulation
  attr_accessor :sprites, :cmap, :image

  def initialize
    super 850, 850, 16, "Collision"

    self.image = SDL::Surface.load "resources/images/body.png"
    self.cmap = image.make_collision_map

    self.sprites = populate Sprite do |s|
      s.image = image
    end
  end

  def inspect
    "<Screen ...>"
  end

  def detect_collisions(sprites)
    collisions = []
    sprites.combination(2).each do |a, b|
      collisions << a << b if a.collide_with? b
    end
    collisions.uniq
  end

  def update n
    sprites.each(&:update)
    detect_collisions(sprites).each(&:collide)
  end

  def draw n
    clear

    sprites.each(&:draw)
    fps n
  end
end

Collision.new.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphics-1.0.0b1 examples/collision.rb