class RubyTerminalGames::Snake::Board
Constants
- APPLE
- SNAKE_BODY
- SNAKE_HEAD_DOWN
- SNAKE_HEAD_LEFT
- SNAKE_HEAD_RIGHT
- SNAKE_HEAD_UP
Public Class Methods
new(width: nil, height: nil)
click to toggle source
Calls superclass method
RubyTerminalGames::Board::new
# File lib/ruby_terminal_games/snake/board.rb, line 11 def initialize(width: nil, height: nil) super @width = cols @height = rows - 1 end
Public Instance Methods
print_world!(game)
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 17 def print_world!(game) clear! draw_border! draw_exit_instructions! draw_apple!(game.apple) draw_stats!(game.counter, game.points) draw_snake!(game.snake.state, game.direction) end
Private Instance Methods
draw_apple!(apple)
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 40 def draw_apple!(apple) row, col = apple.position write(APPLE.red, row: row, col: col) end
draw_exit_instructions!()
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 35 def draw_exit_instructions! text = "Press Q to exit" write(text, row: rows, col: cols - text.length) end
draw_snake!(snake_state, direction)
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 45 def draw_snake!(snake_state, direction) snake_state.each do |state| index, pos = state head = (index == snake_state.length - 1) draw_snake_body(direction, pos, head: head) end end
draw_snake_body(direction, position, head: false)
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 62 def draw_snake_body(direction, position, head: false) row, col = position text = head ? snake_head(direction) : SNAKE_BODY write(text, row: row, col: col) end
draw_stats!(speed, points)
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 28 def draw_stats!(speed, points) stats = [ "Speed:", speed + 1, "Points:", points ].join(' ') write(stats, row: rows, col: 0) end
snake_head(position)
click to toggle source
# File lib/ruby_terminal_games/snake/board.rb, line 53 def snake_head(position) case position when UP then SNAKE_HEAD_UP when RIGHT then SNAKE_HEAD_RIGHT when DOWN then SNAKE_HEAD_DOWN when LEFT then SNAKE_HEAD_LEFT end end