Advertisement
Guest User

Flame (Trickster)

a guest
Dec 1st, 2010
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.88 KB | None | 0 0
  1. require 'sfml/system'
  2.  
  3. require 'sfml/window'
  4.  
  5. require 'sfml/graphics'
  6.  
  7. include SFML
  8.  
  9.  
  10.  
  11. HOTSPOTS = 10
  12.  
  13. WIDTH = 128
  14.  
  15. HEIGHT = 64
  16.  
  17.  
  18.  
  19. class Game
  20.  
  21.     def initialize
  22.  
  23.         @screen = RenderWindow.new(VideoMode.new(WIDTH, HEIGHT, 32), "Flame")
  24.  
  25.         @buffer = Image.new
  26.  
  27.         @buffer.create(WIDTH, HEIGHT)
  28.  
  29.         @sprite = Sprite.new
  30.  
  31.         @sprite.image = @buffer
  32.  
  33.         init_palette
  34.  
  35.     end
  36.  
  37.  
  38.  
  39.     def run
  40.  
  41.         @temp = Array.new(WIDTH, 0)
  42.  
  43.         @hotspot = Array.new(HOTSPOTS) {rand(WIDTH)}
  44.  
  45.         loop do
  46.  
  47.             while event = @screen.get_event
  48.  
  49.                 @screen.close if event.type == SFML::Event::Closed
  50.  
  51.             end
  52.  
  53.             break if update == false || !@screen.open?
  54.  
  55.         end
  56.  
  57.         quit
  58.  
  59.     end
  60.  
  61.  
  62.  
  63.     def update
  64.  
  65.         return false if @screen.input.isKeyDown(Key::Escape)
  66.  
  67.         draw_fire
  68.  
  69.         update_fire
  70.  
  71.         @screen.draw(@sprite)
  72.  
  73.         @screen.display
  74.  
  75.     end
  76.  
  77.  
  78.  
  79.     def quit
  80.  
  81.     end
  82.  
  83.  
  84.  
  85.     def init_palette
  86.  
  87.         @palette = Array.new(256)
  88.  
  89.         @palette.fill(0, 64) {|c| Color.new(c * 4, 0, 0)}
  90.  
  91.         @palette.fill(64, 64) {|c| Color.new(255, (c - 64) * 4, 0)}
  92.  
  93.         @palette.fill(128, 64) {|c| Color.new(255, 255, (c-128) * 4)}
  94.  
  95.         @palette.fill(Color.new(255, 255, 255), 192, 255)
  96.  
  97.     end
  98.  
  99.  
  100.  
  101.     def draw_fire
  102.  
  103.         @temp.fill(0)
  104.  
  105.         0.upto(HOTSPOTS-1) do |c|
  106.  
  107.             (@hotspot[c]-20).upto(@hotspot[c]+20) do |x|
  108.  
  109.                 if x >= 0 and x < @buffer.width
  110.  
  111.                     @temp[x] = [@temp[x] + 20 - (@hotspot[c] - x).abs, 192].min
  112.  
  113.                 end
  114.  
  115.             end
  116.  
  117.             @hotspot[c] = (@hotspot[c] + rand(8) - 3) % @buffer.width
  118.  
  119.         end
  120.  
  121.         @temp.each_index do |i|
  122.  
  123.             @buffer.setPixel(i, HEIGHT - 1, @palette[@temp[i]])
  124.  
  125.         end
  126.  
  127.     end
  128.  
  129.  
  130.  
  131.     def update_fire
  132.  
  133.         0.upto(@buffer.height-2) do |y|
  134.  
  135.             0.upto(@buffer.width-1) do |x|
  136.  
  137.                 c = @palette.index(@buffer.getPixel(x, y + 1))
  138.  
  139.                 color = @buffer.getPixel(x, y+1)
  140.  
  141.                 c -= 1 if c > 0
  142.  
  143.                 @buffer.setPixel(x, y, @palette[c])
  144.  
  145.             end
  146.  
  147.         end
  148.  
  149.     end
  150.  
  151. end
  152.  
  153.  
  154.  
  155. Game.new.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement