Created
July 13, 2018 04:43
-
-
Save F-3r/53568f26076055e08adc54a1796cb647 to your computer and use it in GitHub Desktop.
mini interactive visualization made in ruby + gosu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gosu' | |
class Window < Gosu::Window | |
attr_accessor :hyperspace | |
def initialize(width, height) | |
super(width, height, false) | |
@hyperspace = HyperSpace.new | |
end | |
def update | |
hyperspace.update | |
end | |
def draw | |
hyperspace.draw | |
end | |
def button_down(id) | |
hyperspace.button_down(id) | |
end | |
def button_up(id) | |
hyperspace.button_up(id) | |
end | |
def needs_cursor? | |
true | |
end | |
end | |
class HyperSpace | |
attr_reader :dots, :moving, :count | |
def initialize | |
@dots = [] | |
@moving = false | |
@count = 0 | |
end | |
def update | |
dots.each(&:update) | |
dots.select!(&:visible?) | |
update_caption | |
end | |
def draw | |
dots.each(&:draw) | |
create_dots(25) if moving | |
end | |
def create_dots(n) | |
n.times { dots << Dot.new(x: $win.mouse_x, y: $win.mouse_y, speed: 5, dir: rand(360).to_i) } | |
end | |
def button_down(id) | |
$win.close if id == Gosu::KbEscape | |
if id == Gosu::MsLeft | |
@moving = true | |
create_dots(25) | |
end | |
end | |
def button_up(id) | |
if id == Gosu::MsLeft | |
@moving = false | |
end | |
end | |
def update_caption | |
now = Gosu.milliseconds | |
if now - (@caption_updated_at || 0) > 200 | |
$win.caption = "[FPS: #{Gosu.fps}. " << "item count: #{dots.count}]" | |
@caption_updated_at = now | |
end | |
end | |
end | |
class Dot | |
RAD = 0.0174533.freeze | |
attr_reader :speed, :x, :y, :dx, :dy, :dir, :scale, :color, :size, :layer | |
def initialize(args = {}) | |
@speed = args.fetch(:speed, 4) | |
@x = args.fetch(:x) | |
@y = args.fetch(:y) | |
@dir = args.fetch(:dir) | |
@scale = 0.1 | |
@size = 10 | |
@color = 0 | |
@layer = 0 | |
end | |
def update | |
cos = Math.cos(dir * RAD) | |
sin = Math.sin(dir * RAD) | |
@x += cos * (speed ** scale) | |
@y += sin * (speed ** scale) | |
@dx = x + cos * size ** scale | |
@dy = y + sin * size ** scale | |
@scale += 0.002 * speed | |
@color += 1 if @color <= 253 | |
@layer += 1 | |
end | |
def draw | |
c = Gosu::Color.new(color, color, color) | |
Gosu.draw_line(x, y, c, dx, dy, c, layer) | |
end | |
def visible? | |
x >= 0 - size && | |
x <= $win.width + size && | |
y >= 0 - size && | |
y <= $win.height + size | |
end | |
end | |
$win = Window.new(1280, 768) | |
$win.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment