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_windowsObject



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

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

.drawObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruby_snake/ui/two.rb', line 121

def draw
  init_windows
  Ncurses.nodelay window, true
  begin 
    loop do
      Client.connect Game.host

      draw_map 

      operation = Game.listen_operation
      if !snake.move(operation) || Game.lose?
        Game.role_class.message = '1'
        Client.connect Game.host
        Game.lose!
        break
      elsif score == 0 || Game.win?
        Game.role_class.message = '2'
        Client.connect Game.host
        Game.win!
        break
      end
    end
  ensure
    Ncurses.endwin
  end
end

.draw_localObject



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

def draw_local
  draw_snake
  draw_food
  draw_score
  window.noutrefresh
  clear_tail
end

.draw_loseObject



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

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



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

def draw_map
  draw_local
  draw_remote
  Ncurses.doupdate
  sleep 0.1
end

.draw_remoteObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruby_snake/ui/two.rb', line 93

def draw_remote
  begin 
    receive = Game.role_class.receive
    food = receive.shift
    snake = receive

    remote_window.color_set 1, nil
    remote_display food, '$'
    remote_window.mvprintw 0, 0, (50 - snake.length + 2).to_s

    remote_window.color_set 2, nil
    head = snake.shift
    remote_display head, '@'

    remote_window.noutrefresh
    remote_display snake.last, ' '
  rescue
    nil
  end
end

.draw_scoreObject



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

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

.draw_snakeObject



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

def draw_snake
  window.color_set 2, nil

  super
end

.draw_winObject



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

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
11
# 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
  @snake ||= Snake.new local_window.getmaxx, local_window.getmaxy
end

.local_windowObject



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

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

  @local_window ||= Ncurses::WINDOW.new 30, 51, 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_windowObject



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

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

.scoreObject



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

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

.snakeObject



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

def snake
  @snake 
end

.windowObject



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

def window
  @local_window
end