Class: RubyTerminalGames::Snake::Game

Inherits:
Game
  • Object
show all
Defined in:
lib/ruby_terminal_games/snake/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_terminal_games/snake/game.rb', line 11

def initialize
  @direction = RIGHT
  @board = Board.new

  @apple = Apple.new(
    width: @board.width,
    height: @board.height
  )

  @snake = Snake.new(
    width: @board.width,
    height: @board.height
  )

  @points = 0
  @speed = 0
  @counter = 0
end

Instance Attribute Details

#appleObject (readonly)

Returns the value of attribute apple.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def apple
  @apple
end

#boardObject (readonly)

Returns the value of attribute board.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def board
  @board
end

#counterObject (readonly)

Returns the value of attribute counter.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def counter
  @counter
end

#directionObject (readonly)

Returns the value of attribute direction.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def direction
  @direction
end

#pointsObject (readonly)

Returns the value of attribute points.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def points
  @points
end

#snakeObject (readonly)

Returns the value of attribute snake.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def snake
  @snake
end

#speedObject (readonly)

Returns the value of attribute speed.



8
9
10
# File 'lib/ruby_terminal_games/snake/game.rb', line 8

def speed
  @speed
end

Instance Method Details

#aboutObject



48
49
50
51
52
53
# File 'lib/ruby_terminal_games/snake/game.rb', line 48

def about
  [
    "Remember the good old times. Use the keyboard ",
    "arrows to control the snake."
  ].join
end

#play!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_terminal_games/snake/game.rb', line 30

def play!
  @playing = true

  Keyboard.capture(detect_direction: true) do |key|
    exit if key =~ /q/i
    next unless direction_allowed?(key)
    @direction = key
  end

  while @playing
    eat?
    snake.move!(direction)
    snake.died? and @playing = false
    game_interval!
    board.print_world!(self)
  end
end