class Tatty::Game

Attributes

config[RW]
framerate[RW]

Public Class Methods

new() click to toggle source
# File lib/tatty/game.rb, line 11
def initialize
  @config = TTY::Config.new
  @cursor = TTY::Cursor
  @reader = TTY::Reader.new
  @framerate = 0.1
  @buffer = ""

  @reader.on(:keypress) do |event|
    self.keypress(event)
  end
end

Public Instance Methods

draw() click to toggle source
# File lib/tatty/game.rb, line 46
def draw
  render "not implemented"
end
keypress(event) click to toggle source
# File lib/tatty/game.rb, line 43
def keypress(event)
end
move_to(x, y) click to toggle source
# File lib/tatty/game.rb, line 27
def move_to(x, y)
  render @cursor.move_to(x, y)
end
render(out) click to toggle source
# File lib/tatty/game.rb, line 31
def render(out)
  @buffer += out
end
render_at(x, y, out) click to toggle source
# File lib/tatty/game.rb, line 35
def render_at(x, y, out)
  out.to_s.each_line do |line|
    render @cursor.move_to(x, y)
    render line
    y += 1
  end
end
render_box(*content, **kwargs, &block) click to toggle source
# File lib/tatty/game.rb, line 73
def render_box(*content, **kwargs, &block)
  render @cursor.save
  move_to(0, 0)
  render TTY::Box.frame(*content, **kwargs, &block)
  render @cursor.restore
end
run() click to toggle source
# File lib/tatty/game.rb, line 50
def run
  begin
    @cursor.invisible do
      while true
        @reader.read_keypress(nonblock: true)
        last_buffer = @buffer
        @buffer = ""
        render @cursor.clear_screen
        move_to(0, 0)
        self.draw
        if last_buffer != @buffer
          print @buffer
        end
        sleep(@framerate)
      end
    end
  rescue Interrupt => e
  ensure
    print @cursor.clear_screen
    print @cursor.move_to(0, 0)
  end
end
screen_size() click to toggle source
# File lib/tatty/game.rb, line 23
def screen_size
  TTY::Screen.size
end