Class: RubySnake::UI::Two

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

Class Method Summary collapse

Methods inherited from Base

clear_tail, display, draw_dialog, draw_food

Class Method Details

.clear_remote_tailObject



128
129
130
# File 'lib/ruby_snake/ui/two.rb', line 128

def clear_remote_tail
  remote_display remote_snake_tail, ' '
end

.clear_windowsObject



36
37
38
39
40
41
# File 'lib/ruby_snake/ui/two.rb', line 36

def clear_windows
  local_window.clear
  local_window.noutrefresh
  remote_window.clear
  remote_window.noutrefresh
end

.drawObject



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
# File 'lib/ruby_snake/ui/two.rb', line 155

def draw
  init_windows
  Ncurses.nodelay window, true
  begin 
    loop do
      draw_map
      operation = Game.listen_operation

      if Game.win?
        draw_win
        sleep 1
        window.getch
        break
      elsif Game.lose?
        draw_lose
        sleep 1
        window.getch
        break
      end
      if !snake.move(operation)
        Game.lose!
        break
      elsif score == 0
        Game.win!
        break
      end
    end
  ensure
    Ncurses.endwin
  end
end

.draw_localObject



78
79
80
81
82
83
84
85
# File 'lib/ruby_snake/ui/two.rb', line 78

def draw_local
  draw_snake
  draw_food
  draw_score
  window.noutrefresh
  Ncurses.doupdate
  clear_tail
end

.draw_loseObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_snake/ui/two.rb', line 43

def draw_lose
  clear_windows
  @local_window = UI.window
  window.color_set 1, nil
  Ncurses.nodelay window, false
  x = window.getmaxx / 2 
  y = window.getmaxy / 2 - 10
  draw_dialog y, x, "Sorry, you lose the game."
  window.refresh
end

.draw_mapObject



146
147
148
149
150
151
152
153
# File 'lib/ruby_snake/ui/two.rb', line 146

def draw_map
  draw_local
  draw_remote
  sleep Game.speed
  window.noutrefresh
  remote_window.noutrefresh
  Ncurses.doupdate
end

.draw_remoteObject



137
138
139
140
141
142
143
144
# File 'lib/ruby_snake/ui/two.rb', line 137

def draw_remote
  draw_remote_snake
  draw_remote_food
  draw_remote_score
  remote_window.noutrefresh
  Ncurses.doupdate
  clear_remote_tail
end

.draw_remote_foodObject



132
133
134
135
# File 'lib/ruby_snake/ui/two.rb', line 132

def draw_remote_food
  remote_window.color_set 1, nil
  remote_display remote_food, '$'
end

.draw_remote_scoreObject



97
98
99
# File 'lib/ruby_snake/ui/two.rb', line 97

def draw_remote_score
  remote_window.mvprintw 0, 0, score.to_s
end

.draw_remote_snakeObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_snake/ui/two.rb', line 101

def draw_remote_snake
  remote_window.color_set 2, nil
  remote_snake_body.each do |body|
    if body == remote_snake_head
      remote_display body, '@'
    else
      remote_display body, 'o'
    end
  end
end

.draw_scoreObject



74
75
76
# File 'lib/ruby_snake/ui/two.rb', line 74

def draw_score
  window.mvprintw 0, 0, score.to_s
end

.draw_snakeObject



64
65
66
67
68
# File 'lib/ruby_snake/ui/two.rb', line 64

def draw_snake
  window.color_set 2, nil

  super
end

.draw_winObject



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

def draw_win
  clear_windows
  @local_window = UI.window
  window.color_set 4, nil
  x = window.getmaxx / 2 
  y = window.getmaxy / 2 - 10
  draw_dialog y, x, "Congratulations, you win the game."
  window.refresh
end

.init_windowsObject



5
6
7
8
9
10
# File 'lib/ruby_snake/ui/two.rb', line 5

def init_windows
  local_window.border *([0]*8) 
  remote_window.border *([0]*8) 
  local_window.noutrefresh
  remote_window.noutrefresh
end

.local_windowObject



12
13
14
15
16
17
# File 'lib/ruby_snake/ui/two.rb', line 12

def local_window
  x = (Ncurses.stdscr.getmaxy - 30) / 2
  y = (Ncurses.stdscr.getmaxx - 100) / 2

  @local_window ||= Ncurses::WINDOW.new 30, 50, x, y
end

.remote_display(stuff, symbol) ⇒ Object



87
88
89
90
91
# File 'lib/ruby_snake/ui/two.rb', line 87

def remote_display stuff, symbol
  x = stuff.last + 1
  y = stuff.first + 1
  remote_window.mvprintw x, y, symbol
end

.remote_foodObject



124
125
126
# File 'lib/ruby_snake/ui/two.rb', line 124

def remote_food
  Game.role_class.food
end

.remote_scoreObject



93
94
95
# File 'lib/ruby_snake/ui/two.rb', line 93

def remote_score
  50 - remote_snake_body.length + 2
end

.remote_snake_bodyObject



112
113
114
# File 'lib/ruby_snake/ui/two.rb', line 112

def remote_snake_body
  Game.role_class.snake
end

.remote_snake_headObject



116
117
118
# File 'lib/ruby_snake/ui/two.rb', line 116

def remote_snake_head
  Game.role_class.snake.first
end

.remote_snake_tailObject



120
121
122
# File 'lib/ruby_snake/ui/two.rb', line 120

def remote_snake_tail
  Game.role_class.snake.last
end

.remote_windowObject



19
20
21
22
23
24
25
26
# File 'lib/ruby_snake/ui/two.rb', line 19

def remote_window
  x = (Ncurses.stdscr.getmaxy - 30) / 2
  y = Ncurses.stdscr.getmaxx / 2
  @remote_window ||= Ncurses::WINDOW.new 30, 
                                         50,
                                         x,
                                         y
end

.scoreObject



70
71
72
# File 'lib/ruby_snake/ui/two.rb', line 70

def score
  50 - snake.body.length + 2
end

.snakeObject



28
29
30
# File 'lib/ruby_snake/ui/two.rb', line 28

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

.windowObject



32
33
34
# File 'lib/ruby_snake/ui/two.rb', line 32

def window
  @local_window
end