Class: Rb2048::GameRender
- Inherits:
-
Object
- Object
- Rb2048::GameRender
- Defined in:
- lib/rb2048/game.rb
Instance Attribute Summary collapse
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
- #draw(payload) ⇒ Object
- #init_screen ⇒ Object
-
#initialize ⇒ GameRender
constructor
A new instance of GameRender.
Constructor Details
#initialize ⇒ GameRender
Returns a new instance of GameRender.
108 109 110 111 112 113 114 |
# File 'lib/rb2048/game.rb', line 108 def initialize @window = nil @window_height = nil @window_width = nil init_screen end |
Instance Attribute Details
#window ⇒ Object (readonly)
Returns the value of attribute window.
107 108 109 |
# File 'lib/rb2048/game.rb', line 107 def window @window end |
Instance Method Details
#draw(payload) ⇒ Object
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rb2048/game.rb', line 130 def draw(payload) data = payload[:data] size = payload[:size] level = payload[:level] score = payload[:score] last_action = payload[:last_action] status = payload[:status] = payload[:start_timestamp] = payload[:end_timestamp] tun_new_value = payload[:tun_new_value] @window.clear # display board content buffer buffer = [] number_width = score.to_s.length * 2 for row_index in (0..size-1) row_text = "|" for col_index in (0..size-1) n = data[row_index][col_index] n_text = " "*(number_width - n.to_s.length) + n.to_s row_text +=" "*2+"#{n_text}"+" "*2+"|" end buffer.push(row_text) end # x,y offset counter row,col = [0,1] # title content_width = buffer.first.length title = "-- Ruby 2048 --" left_offset = (content_width - title.length) / 2 row = row + 1 @window.setpos(row, left_offset) @window.addstr("-- Ruby 2048 --") # board row = row + 2 start_row, start_col = [row,1] @window.setpos(start_row, start_col) @window.addstr("-"*buffer.first.length) buffer.each_with_index do |line,index| @window.setpos(start_row+index*2 + 2, start_col) @window.addstr("-" * line.length) @window.setpos(start_row+index*2 + 1, start_col) @window.addstr(line) end # score row = row + buffer.length * 2 + 2 @window.setpos(row, col) @window.addstr("Score:\t#{score}\t\t#{"You:"+last_action.to_s if last_action}") # status row = row + 2 @window.setpos(row, col) result_text = nil if status == 1 result_text = "Congratulation! You have got 2048!!! :D" elsif status == -1 result_text = "YOU LOST :(" end if result_text left_offset = (buffer.first.length - result_text.length )/2 @window.setpos(row, left_offset) @window.addstr(result_text) end # control row = row + 2 @window.setpos(row, col) @window.addstr("Control: W(↑) A(←) S(↓) D(→) Q(quit) R(Restart)") @window.refresh end |
#init_screen ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rb2048/game.rb', line 116 def init_screen Curses.init_screen Curses.cbreak Curses.noecho Curses.curs_set(0) # Invisible cursor Curses.stdscr.keypad = true @window_height = Curses.lines @window_width = Curses.cols @window = Curses::Window.new(@window_height, @window_width, 0, 0) @window.refresh end |