Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WIDTH = 640
- HEIGHT = 480
- if Graphics.respond_to?(:width)
- WIDTH = Graphics.width
- HEIGHT = Graphics.height
- end
- class Snow
- class Flake < Struct.new(:x, :y)
- def initialize(x = rand(WIDTH))
- self.x = x
- self.y = 0
- end
- def move(arr)
- self.y += 1
- return !is_ok?(arr)
- end
- def is_ok?(arr)
- return self.y < arr[self.x]
- end
- end
- def self.run
- snow = Snow.new
- self.integrate_graphics(snow)
- end
- def self.integrate_graphics(snow)
- Graphics.snow = snow
- end
- def initialize
- @bitmap = Bitmap.new(WIDTH,HEIGHT)
- @sprite = Sprite.new
- @color = Color.new(255,255,255)
- @clear = Color.new(0,0,0,0)
- @table = Table.new(WIDTH,HEIGHT)
- @list = []
- @bottom = Array.new(WIDTH,HEIGHT-1)
- @sprite.bitmap = @bitmap
- @sprite.z = 9999
- end
- def update
- removed = []
- for flake in @list
- @bitmap.set_pixel(flake.x,flake.y,@clear)
- removed << flake if flake.move(@bottom)
- @bitmap.set_pixel(flake.x,flake.y,get_color)
- end
- for flake in removed
- @list.delete(flake)
- @bottom[flake.x] -= 1
- end
- for num in all_diff(1)
- flake = Flake.new
- if (flake.is_ok?(@bottom))
- @list << flake
- @bitmap.set_pixel(flake.x,flake.y,get_color)
- end
- end
- end
- def get_color
- return @color
- end
- def all_diff(num)
- arr = [rand(WIDTH)]
- while arr.size < num
- v = rand(WIDTH)
- arr << v unless arr.include?(v)
- end
- arr
- end
- end
- module Graphics
- @snow = nil
- class << self
- attr_accessor :snow
- alias_method(:update_snow, :update)
- def update(*args)
- snow.update if snow
- update_snow(*args)
- end
- end
- end
- Snow.run
Add Comment
Please, Sign In to add comment