Module: SpaceDozer::Console

Extended by:
Curses, Console
Included in:
Console
Defined in:
lib/spacedozer/console.rb

Constant Summary collapse

Color =
{
  empty: 0,
  dirt:  8,
  rock:  7,
  wall:  8,
  alien: 1,
  dozer: 2
}
Key =
{
  ?k => :north,
  ?j => :south,
  ?h => :west,
  ?l => :east
}

Instance Method Summary collapse

Instance Method Details

#closeObject

Turn line buffering on and close the Curses session.


52
53
54
55
56
57
# File 'lib/spacedozer/console.rb', line 52

def close
  unless closed?
    nocbreak
    close_screen
  end
end

#draw(color, x, y) ⇒ Object


46
47
48
49
# File 'lib/spacedozer/console.rb', line 46

def draw(color, x, y)
  setpos x, y
  attron(color_pair color) { addstr '  ' }
end

#heightInteger

Returns height of console.

Returns:

  • (Integer)

    height of console


36
37
38
# File 'lib/spacedozer/console.rb', line 36

def height
  lines
end

#keypressObject


23
24
25
# File 'lib/spacedozer/console.rb', line 23

def keypress
  command = Key[getch] and yield(command)
end

#openObject

Initialize the Curses environment, configure it and then clean up when the caller is done.

  • don't echo keypresses

  • hide the cursor

  • turn off line buffering

  • don't wait for input, just listen for it

9 different bg/fg pairs are initialized for the colored SpaceDozer::Tile squares (0-8): (black, red, green, yellow, blue, magenta, cyan, white, grey)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/spacedozer/console.rb', line 68

def open
  init_screen

  noecho
  curs_set 0
  cbreak
  stdscr.nodelay = -1

  use_default_colors
  start_color
  9.times { |i| init_pair i, i, i }

  yield

  close
end

#update(tiles) ⇒ Object


40
41
42
43
44
# File 'lib/spacedozer/console.rb', line 40

def update(tiles)
  tiles.each(&:update)
  refresh
  sleep 0.01 # this seems to keep the laptop under 750°
end

#widthInteger

Returns width of console.

Returns:

  • (Integer)

    width of console


31
32
33
# File 'lib/spacedozer/console.rb', line 31

def width
  cols
end