Class: Game2048::Render
- Inherits:
-
Object
- Object
- Game2048::Render
- Defined in:
- lib/game_2048/render.rb
Overview
Render
Constant Summary collapse
- SIZE =
3- HORIZONTAL =
"\u2500"- VERTICAL =
"\u2502"- LEFT_VERTICAL =
"\u251c"- RIGHT_VERTICAL =
"\u2524"- TOP_HORIZONTAL =
"\u252c"- BOTTOM_HORIZONTAL =
"\u2534"- TOP_LEFT =
"\u250c"- TOP_RIGHT =
"\u2510"- BOTTOM_LEFT =
"\u2514"- BOTTOM_RIGHT =
"\u2518"- HORIZONTAL_VERTICAL =
"\u253c"- BORDER_COLOR =
:yellow- TILE_COLOR =
:cyan- STATUSBAR_COLOR =
:blue- STATUSBAR_TEXT_COLOR =
:white- GAMEOVER_COLOR =
:red- WIN_COLOR =
:green- BLANK =
' '
Instance Attribute Summary collapse
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #align ⇒ Object
- #draw ⇒ Object
- #draw_gameover ⇒ Object
- #draw_statusbar ⇒ Object
- #draw_tiles ⇒ Object
-
#initialize(terminal, tiles, **options) ⇒ Render
constructor
A new instance of Render.
- #refresh ⇒ Object
Constructor Details
#initialize(terminal, tiles, **options) ⇒ Render
29 30 31 32 33 34 35 36 |
# File 'lib/game_2048/render.rb', line 29 def initialize(terminal, tiles, **) @tiles = tiles @terminal = terminal @x = @y = 1 @rows = @cols = 0 @width = @height = 0 @size = .fetch(:size, SIZE) end |
Instance Attribute Details
#size ⇒ Object
Returns the value of attribute size.
27 28 29 |
# File 'lib/game_2048/render.rb', line 27 def size @size end |
Instance Method Details
#align ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/game_2048/render.rb', line 168 def align rows, cols = @terminal.display_size return if rows.nil? || cols.nil? @rows = rows @cols = cols n = Math.sqrt(Tiles::SIZE).to_i @width = @size * 2 * n + n + 1 @height = @size * n + n + 1 @x = @cols / 2 - @width / 2 @y = @rows / 2 - @height / 2 end |
#draw ⇒ Object
53 54 55 56 57 |
# File 'lib/game_2048/render.rb', line 53 def draw draw_tiles draw_gameover end |
#draw_gameover ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/game_2048/render.rb', line 79 def draw_gameover y = @y + @height + 1 return if @rows <= y @terminal.move_to(1, y) @terminal.write(BLANK * @cols) @terminal.reset line = String.new if @tiles.win? line << 'Win!' elsif @tiles.game_over? line << 'Game over!' end x = @cols / 2 - line.length / 2 + 1 @terminal.move_to(x, y) @tiles.win? ? @terminal.fg_color(WIN_COLOR) : @terminal.fg_color(GAMEOVER_COLOR) @terminal.bold @terminal.write(line[0..(@cols - 1)]) @terminal.reset end |
#draw_statusbar ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/game_2048/render.rb', line 59 def line = String.new line << "#{@tiles.score} #{VERTICAL} " line << "\u2190\u2193\u2191\u2192 Move #{VERTICAL} " line << "u Undo #{VERTICAL} " line << "r Reset #{VERTICAL} " line << "+- Scale #{VERTICAL} " line << 'q Quit' x = @cols / 2 - line.length / 2 + 1 return if @cols < line.length @terminal.move_to(1, @rows) @terminal.bg_color(STATUSBAR_COLOR) @terminal.write(BLANK * @cols) @terminal.move_to(x, @rows) @terminal.fg_color(STATUSBAR_TEXT_COLOR) @terminal.write(line[0..(@cols - 1)]) @terminal.reset end |
#draw_tiles ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/game_2048/render.rb', line 101 def draw_tiles n = Math.sqrt(Tiles::SIZE).to_i return if @cols <= @width || @rows <= @height @terminal.move_to(@x, @y) @terminal.fg_color(BORDER_COLOR) @terminal.write(TOP_LEFT) n.times do |i| @terminal.write(HORIZONTAL * (@size * 2)) next if i == n - 1 @terminal.write(TOP_HORIZONTAL) end @terminal.write(TOP_RIGHT) n.times do |i| @size.times do |j| @terminal.move_to(@x, @y + i * @size + i + j + 1) @terminal.write(VERTICAL) n.times do |k| @terminal.reset @terminal.write(BLANK * (@size * 2)) @terminal.reset next if k == n - 1 @terminal.fg_color(BORDER_COLOR) @terminal.write(VERTICAL) end @terminal.fg_color(BORDER_COLOR) @terminal.write(VERTICAL) end next if i == n - 1 @terminal.move_to(@x, @y + i * (@size + 1) + @size + 1) @terminal.write(LEFT_VERTICAL) n.times do |j| @terminal.write(HORIZONTAL * (@size * 2)) next if j == n - 1 @terminal.write(HORIZONTAL_VERTICAL) end @terminal.write(RIGHT_VERTICAL) end @terminal.move_to(@x, @y + @size * n + n) @terminal.write(BOTTOM_LEFT) n.times do |i| @terminal.write(HORIZONTAL * (@size * 2)) next if i == n - 1 @terminal.write(BOTTOM_HORIZONTAL) end @terminal.write(BOTTOM_RIGHT) @terminal.reset @terminal.fg_color(TILE_COLOR) @terminal.bold @tiles.items.each.with_index do |tile, i| t = tile.zero? ? '' : tile.to_s x = i % n * (@size * 2 + 1) + @size - t.length / 2 + 1 y = i / n * (@size + 1) + @size / 2 + 1 @terminal.move_to(@x + x, @y + y) @terminal.write(t) end @terminal.reset end |
#refresh ⇒ Object
47 48 49 50 51 |
# File 'lib/game_2048/render.rb', line 47 def refresh @terminal.erase_display align draw end |