Skip to content

Instantly share code, notes, and snippets.

@fallengiants
Created November 14, 2012 23:46
Show Gist options
  • Select an option

  • Save fallengiants/4075669 to your computer and use it in GitHub Desktop.

Select an option

Save fallengiants/4075669 to your computer and use it in GitHub Desktop.
Gosu - change background gradient based on mouse position
require 'gosu'
class FrameKeeper
attr_reader :this, :last, :delta
def initialize
@last = Gosu::milliseconds
end
def update
@this = Gosu::milliseconds
@delta = (@this - @last) / 1000.0
@last = @this
end
end
class MyWindow < Gosu::Window
WIDTH = 640
HEIGHT = 480
def initialize
super(WIDTH, HEIGHT, false)
self.caption = 'Howdy'
@color ||= Gosu::Color.new(0xffebcd1c) # alpha, r, g, b
@colr2 ||= Gosu::Color.new(0x33ebcd1c)
@frames = FrameKeeper.new
end
def update
@frames.update
red = ((mouse_x % WIDTH) / WIDTH.to_f * 255).to_i << 16
green = (@frames.this.to_i / 200 % 255) << 8
blue = ((mouse_y % HEIGHT) / HEIGHT.to_f * 255).to_i
color1 = 0xff000000 + red + green + blue
color2 = 0x33000000 + red + green + blue
@color = Gosu::Color.new(color1)
@colr2 = Gosu::Color.new(color2)
end
def draw
draw_quad(
0, 0, @color,
WIDTH, 0, @color,
0, HEIGHT, @colr2,
WIDTH, HEIGHT, @colr2,
0
)
end
end
window = MyWindow.new
window.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment