Class: RubySnake::UI::One

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_snake/ui/one.rb

Class Method Summary collapse

Methods inherited from Base

clear_tail, display, draw_dialog, draw_food

Class Method Details

.drawObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_snake/ui/one.rb', line 73

def draw
  window.clear
  Ncurses.nodelay window, true
  begin 
    loop do
      draw_map
      operation = Game.listen_operation
      break if operation == Config::QUIT
      if !Game.pause? && !snake.move(operation)
        if Game.over?
          break
        else
          Game.restart
        end
      end
    end
  ensure
    Ncurses.endwin
  end
end

.draw_boderObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_snake/ui/one.rb', line 37

def draw_boder
  if Game.pause?
    window.color_set 3, nil
  elsif snake.near_border?
    window.color_set 1, nil
  else
    window.color_set 4, nil
  end
  window.border *([0] * 8)
end

.draw_mapObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_snake/ui/one.rb', line 62

def draw_map
  draw_boder
  draw_title
  draw_snake
  window.refresh
  draw_food
  clear_tail
  sleep Game.speed
  window.refresh
end

.draw_overObject



54
55
56
57
58
59
60
# File 'lib/ruby_snake/ui/one.rb', line 54

def draw_over
  window.clear
  Ncurses.nodelay window, false
  x = window.getmaxx / 2 
  y = window.getmaxy / 2 - 10
  draw_dialog y, x, "Oops, I am died, restart? (y/n)"
end

.draw_quitObject



48
49
50
51
52
# File 'lib/ruby_snake/ui/one.rb', line 48

def draw_quit
  x = window.getmaxx / 2 
  y = window.getmaxy / 2 - 10
  draw_dialog y, x, "Oops, I am died, restart? (y/n)"
end

.draw_snakeObject



13
14
15
16
17
18
19
20
21
# File 'lib/ruby_snake/ui/one.rb', line 13

def draw_snake
  if Game.pause?
    window.color_set 3, nil
  else
    window.color_set 2, nil
  end

  super
end

.draw_titleObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_snake/ui/one.rb', line 23

def draw_title
  if Game.pause?
    window.color_set 3, nil
  elsif snake.near_border?
    window.color_set 1, nil
  else
    window.color_set 4, nil
  end
  y = snake.width / 2  - 15

  window.mvprintw 0, y, 
    "  Score: #{Game.score}  Level: #{Game.level}  Time: #{Game.time}s  "
end

.snakeObject



9
10
11
# File 'lib/ruby_snake/ui/one.rb', line 9

def snake
  @snake ||= Snake.new window.getmaxx, window.getmaxy
end

.windowObject



5
6
7
# File 'lib/ruby_snake/ui/one.rb', line 5

def window
  UI.window
end