rubyioterminalroguelike

More sophisticated terminal I/O in Ruby


I'm trying to make a rouguelike game that runs inside a terminal using Ruby but I'm not exactly sure how to go about doing that. I want to be able to address and update each cell in the standard 80*24 terminal window individually. Can I do this with the standard library or alternately are there any good gems I could do this with?


Solution

  • Curses is probably the easiest to implement and it is widely available across platforms. Ruby bindings used to come as part of the standard library, but it's now a gem: gem install curses. Here's an example from the docs:

    require "curses"
    
    def show_message(message)
      height = 5
      width  = message.length + 6
      top    = (Curses.lines - height) / 2
      left   = (Curses.cols - width) / 2
      win = Curses::Window.new(height, width, top, left)
      win.box("|", "-")
      win.setpos(2, 3)
      win.addstr(message)
      win.refresh
      win.getch
      win.close
    end
    
    Curses.init_screen
    begin
      Curses.crmode
      Curses.setpos((Curses.lines - 1) / 2, (Curses.cols - 11) / 2)
      Curses.addstr("Hit any key")
      Curses.refresh
      Curses.getch
      show_message("Hello, World!")
    ensure
      Curses.close_screen
    end