Class: Gosu::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/bestguigui/pixel_image.rb

Overview

Rajoute Gosu::Image#get_pixel — pratique pour designer un niveau comme une image (une couleur de pixel = un type de tuile/spawn/ennemi) plutôt qu'un format de niveau maison. Charge l'image comme n'importe quelle autre (Gosu::Image.new ou Bestguigui::Assets.image), puis :

level = Bestguigui::Assets.image("levels/level_1.png")
level.height.times do |y|
level.width.times do |x|
  color = level.get_pixel(x, y)
  # ... place une tuile/un ennemi selon la couleur
end
end

nil si (x, y) est hors de l'image.

Instance Method Summary collapse

Instance Method Details

#get_pixel(x, y) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/bestguigui/pixel_image.rb', line 16

def get_pixel(x, y)
  return nil if x < 0 || x >= width || y < 0 || y >= height

  @blob ||= to_blob
  r, g, b, a = @blob[(y * width + x) * 4, 4].unpack("C*")
  Gosu::Color.new(a, r, g, b)
end