Class: RubySnake::Game

Inherits:
Object
  • Object
show all
Extended by:
Config
Defined in:
lib/ruby_snake/game.rb

Constant Summary

Constants included from Config

Config::CONNECT_TO_HOST, Config::CREATE_HOST, Config::DOWN, Config::LEFT, Config::NO, Config::OPERATIONS, Config::PAUSE, Config::QUIT, Config::RIGHT, Config::SINGLE_PLAYER, Config::TWO_PLAYERS, Config::UP, Config::YES

Class Method Summary collapse

Class Method Details

.choise_modeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_snake/game.rb', line 39

def choise_mode
  loop do
    case window.getch
    when SINGLE_PLAYER
      start
      break
    when TWO_PLAYERS
      choise_role
      break
    when QUIT
      break
    end
  end
end

.choise_roleObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_snake/game.rb', line 21

def choise_role
  UI.draw_role_menu
  loop do
    case window.getch
    when CREATE_HOST
      Ncurses.endwin
      start 'server'
      break
    when CONNECT_TO_HOST
      Ncurses.endwin
      start 'client'
      break
    when QUIT
      break
    end
  end
end

.connected_hostObject



215
216
217
218
219
# File 'lib/ruby_snake/game.rb', line 215

def connected_host
  system 'clear'
  print "Input host to connect: "
  gets.chomp
end

.continueObject



168
169
170
171
# File 'lib/ruby_snake/game.rb', line 168

def continue
  @time_flag = Time.now.to_f
  @pause_flag = false
end

.direction_opposite?(o) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
# File 'lib/ruby_snake/game.rb', line 152

def direction_opposite? o 
  return true if operation == UP && o == DOWN
  return true if operation == DOWN  && o == UP
  return true if operation == LEFT && o == RIGHT
  return true if operation == RIGHT && o == LEFT
end

.get_hostObject



249
250
251
252
253
254
255
# File 'lib/ruby_snake/game.rb', line 249

def get_host
  system 'clear'
  @host = connected_host
  puts '=' * 40
  puts "Connect to #{host}..." 
  puts '=' * 40
end

.hostObject



245
246
247
# File 'lib/ruby_snake/game.rb', line 245

def host
  @host
end

.levelObject



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

def level
  score / 10 + 1
end

.listen_operationObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_snake/game.rb', line 80

def listen_operation
  operation = window.getch

  if OPERATIONS.include?(operation) && !direction_opposite?(operation)
    case operation
    when PAUSE
      pause_or_continue unless Game.vs?
    when QUIT
      stop unless Game.vs?
    else
      @operation = operation unless Game.pause?
    end
  end

  @operation
end

.loseObject



200
201
202
# File 'lib/ruby_snake/game.rb', line 200

def lose
  @lose = true
end

.lose!Object



208
209
210
211
212
213
# File 'lib/ruby_snake/game.rb', line 208

def lose!
  Ncurses.nodelay window, false
  UI::Two.draw_lose
  sleep 1
  window.getch
end

.lose?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/ruby_snake/game.rb', line 204

def lose?
  @lose
end

.operationObject



148
149
150
# File 'lib/ruby_snake/game.rb', line 148

def operation
  @operation ||= RIGHT
end

.over?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_snake/game.rb', line 108

def over?
  UI::One.draw_over
  loop do
    case window.getch
    when YES
      return false
    when NO
      return true
    end
  end 
end

.pauseObject



163
164
165
166
# File 'lib/ruby_snake/game.rb', line 163

def pause
  @used_time = time
  @pause_flag = true
end

.pause?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/ruby_snake/game.rb', line 159

def pause?
  @pause_flag
end

.pause_or_continueObject



140
141
142
143
144
145
146
# File 'lib/ruby_snake/game.rb', line 140

def pause_or_continue
  if pause?
    continue
  else
    pause
  end
end

.restartObject



173
174
175
176
177
178
179
180
# File 'lib/ruby_snake/game.rb', line 173

def restart
  Ncurses.nodelay window, true
  UI::One.window.clear
  @operation = RIGHT
  @used_time = 0.0
  @time_flag = Time.now.to_f
  snake.init
end

.roleObject



225
226
227
# File 'lib/ruby_snake/game.rb', line 225

def role
  @role
end

.role=(role) ⇒ Object



229
230
231
# File 'lib/ruby_snake/game.rb', line 229

def role= role
  @role = role
end

.role_classObject



233
234
235
236
237
238
239
# File 'lib/ruby_snake/game.rb', line 233

def role_class
  if role == 'server'
    Server
  else
    Client
  end
end

.scoreObject



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

def score
  snake.body.length - 2
end

.snakeObject



72
73
74
75
76
77
78
# File 'lib/ruby_snake/game.rb', line 72

def snake
  if vs?
    UI::Two.snake
  else
    UI::One.snake
  end
end

.speedObject



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_snake/game.rb', line 128

def speed
  if score < 10
    0.2
  elsif score >= 10 && score < 20 
    0.1
  elsif score >= 20 && score < 30
    0.05
  elsif score >= 30
    0.03
  end
end

.start(role = nil) ⇒ Object

common



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_snake/game.rb', line 8

def start(role=nil)
  self.role = role
  @time_flag = Time.now.to_f
  if role.nil?
    UI.draw
  elsif role == 'server'
    Server.start
  elsif role == 'client'
    get_host
    UI::Two.draw
  end
end

.stopObject



221
222
223
# File 'lib/ruby_snake/game.rb', line 221

def stop
  @operation = QUIT
end

.timeObject

one player



100
101
102
103
104
105
106
# File 'lib/ruby_snake/game.rb', line 100

def time
  if pause?
    @used_time
  else
    ((Time.now.to_f - @time_flag + @used_time.to_f) * 10).round / 10.0
  end
end

.vs?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/ruby_snake/game.rb', line 241

def vs?
  !!self.role
end

.welcomeObject



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

def welcome
  begin 
    UI.init
    UI.draw_welcome
    choise_mode
  ensure
    Ncurses.endwin
  end
end

.winObject

two players



184
185
186
# File 'lib/ruby_snake/game.rb', line 184

def win
  @win = true
end

.win!Object



192
193
194
195
196
197
198
# File 'lib/ruby_snake/game.rb', line 192

def win!
  role_class.message = '1'
  Ncurses.nodelay window, false
  UI::Two.draw_win
  sleep 1
  window.getch
end

.win?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/ruby_snake/game.rb', line 188

def win?
  @win
end

.windowObject



64
65
66
67
68
69
70
# File 'lib/ruby_snake/game.rb', line 64

def window
  if vs?
    UI::Two.window
  else
    UI::One.window
  end
end