Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'sfml/system'
- require 'sfml/window'
- require 'sfml/graphics'
- include SFML
- HOTSPOTS = 10
- WIDTH = 128
- HEIGHT = 64
- class Game
- def initialize
- @screen = RenderWindow.new(VideoMode.new(WIDTH, HEIGHT, 32), "Flame")
- @buffer = Image.new
- @buffer.create(WIDTH, HEIGHT)
- @sprite = Sprite.new
- @sprite.image = @buffer
- init_palette
- end
- def run
- @temp = Array.new(WIDTH, 0)
- @hotspot = Array.new(HOTSPOTS) {rand(WIDTH)}
- loop do
- while event = @screen.get_event
- @screen.close if event.type == SFML::Event::Closed
- end
- break if update == false || !@screen.open?
- end
- quit
- end
- def update
- return false if @screen.input.isKeyDown(Key::Escape)
- draw_fire
- update_fire
- @screen.draw(@sprite)
- @screen.display
- end
- def quit
- end
- def init_palette
- @palette = Array.new(256)
- @palette.fill(0, 64) {|c| Color.new(c * 4, 0, 0)}
- @palette.fill(64, 64) {|c| Color.new(255, (c - 64) * 4, 0)}
- @palette.fill(128, 64) {|c| Color.new(255, 255, (c-128) * 4)}
- @palette.fill(Color.new(255, 255, 255), 192, 255)
- end
- def draw_fire
- @temp.fill(0)
- 0.upto(HOTSPOTS-1) do |c|
- (@hotspot[c]-20).upto(@hotspot[c]+20) do |x|
- if x >= 0 and x < @buffer.width
- @temp[x] = [@temp[x] + 20 - (@hotspot[c] - x).abs, 192].min
- end
- end
- @hotspot[c] = (@hotspot[c] + rand(8) - 3) % @buffer.width
- end
- @temp.each_index do |i|
- @buffer.setPixel(i, HEIGHT - 1, @palette[@temp[i]])
- end
- end
- def update_fire
- 0.upto(@buffer.height-2) do |y|
- 0.upto(@buffer.width-1) do |x|
- c = @palette.index(@buffer.getPixel(x, y + 1))
- color = @buffer.getPixel(x, y+1)
- c -= 1 if c > 0
- @buffer.setPixel(x, y, @palette[c])
- end
- end
- end
- end
- Game.new.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement