pixelfaucet/examples/noise_1d.cr

50 lines
1.2 KiB
Crystal
Raw Normal View History

2022-01-28 05:04:24 +01:00
require "../src/game"
require "../src/controller"
require "../src/noise"
module PF
class Noise1d < Game
@noise : Noise = Noise.new
@noise_scale : Float64
@noise_zoom : Float64
def initialize(*args, **kwargs)
super
@noise_scale = height / 4
@noise_zoom = width / 4
@xpos = 0.0
@controller = PF::Controller(Keys).new({
Keys::UP => "scale up",
Keys::DOWN => "scale down",
Keys::RIGHT => "zoom up",
Keys::LEFT => "zoom down",
2022-01-28 05:04:24 +01:00
})
plug_in @controller
2022-01-28 05:04:24 +01:00
end
def update(dt)
2022-01-28 05:04:24 +01:00
@noise_scale += (@noise_scale * 0.8) * dt if @controller.held?("scale up")
@noise_scale -= (@noise_scale * 0.8) * dt if @controller.held?("scale down")
@noise_zoom += (@noise_zoom * 0.8) * dt if @controller.held?("zoom up")
@noise_zoom -= (@noise_zoom * 0.8) * dt if @controller.held?("zoom down")
@xpos += 20.0 * dt
end
def draw
clear(50, 127, 200)
step = width // 15
mid = height // 2
0.upto(width) do |x|
y = mid + (@noise.get((x + @xpos) / @noise_zoom) * @noise_scale).to_i
2022-03-30 04:54:31 +02:00
draw_point(x, y, Pixel::Yellow)
2022-01-28 05:04:24 +01:00
end
end
end
end
game = PF::Noise1d.new(300, 200, 2)
game.run!