Guest User

Untitled

a guest
May 16th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.80 KB | None | 0 0
  1. WIDTH  = 640
  2. HEIGHT = 480
  3.  
  4. if Graphics.respond_to?(:width)
  5.   WIDTH  = Graphics.width
  6.   HEIGHT = Graphics.height
  7. end
  8.  
  9.  
  10. class Snow
  11.   class Flake < Struct.new(:x, :y)
  12.     def initialize(x = rand(WIDTH))
  13.       self.x = x
  14.       self.y = 0
  15.     end
  16.     def move(arr)
  17.       self.y += 1
  18.       return !is_ok?(arr)
  19.     end
  20.     def is_ok?(arr)
  21.       return self.y < arr[self.x]
  22.     end
  23.   end
  24.  
  25.   def self.run
  26.     snow = Snow.new
  27.     self.integrate_graphics(snow)
  28.   end
  29.  
  30.   def self.integrate_graphics(snow)
  31.     Graphics.snow = snow
  32.   end
  33.  
  34.   def initialize
  35.     @bitmap = Bitmap.new(WIDTH,HEIGHT)
  36.     @sprite = Sprite.new
  37.     @color  = Color.new(255,255,255)
  38.     @clear  = Color.new(0,0,0,0)
  39.     @table  = Table.new(WIDTH,HEIGHT)
  40.     @list   = []
  41.     @bottom = Array.new(WIDTH,HEIGHT-1)
  42.    
  43.     @sprite.bitmap = @bitmap
  44.     @sprite.z      = 9999
  45.   end
  46.  
  47.   def update
  48.     removed = []
  49.     for flake in @list
  50.       @bitmap.set_pixel(flake.x,flake.y,@clear)
  51.       removed << flake if flake.move(@bottom)
  52.       @bitmap.set_pixel(flake.x,flake.y,get_color)
  53.     end
  54.    
  55.     for flake in removed
  56.       @list.delete(flake)
  57.       @bottom[flake.x] -= 1
  58.     end
  59.    
  60.     for num in all_diff(1)
  61.       flake = Flake.new
  62.       if (flake.is_ok?(@bottom))
  63.         @list << flake
  64.         @bitmap.set_pixel(flake.x,flake.y,get_color)
  65.       end
  66.     end
  67.   end
  68.  
  69.   def get_color
  70.     return @color
  71.   end
  72.  
  73.   def all_diff(num)
  74.     arr = [rand(WIDTH)]
  75.     while arr.size < num
  76.       v = rand(WIDTH)
  77.       arr << v unless arr.include?(v)
  78.     end
  79.     arr
  80.   end
  81. end
  82.  
  83. module Graphics
  84.   @snow = nil
  85.   class << self
  86.     attr_accessor :snow
  87.     alias_method(:update_snow, :update)
  88.    
  89.     def update(*args)
  90.       snow.update if snow
  91.       update_snow(*args)
  92.     end
  93.   end
  94. end
  95.  
  96. Snow.run
Add Comment
Please, Sign In to add comment