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



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

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



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

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



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

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

.countdownObject



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

def countdown
  3.times do |i|
    puts 3 - i
    sleep 1
  end
end

.direction_opposite?(o) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.levelObject



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

def level
  score / 10 + 1
end

.listen_operationObject



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

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



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

def lose
  @lose = true
end

.lose!Object



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

def lose!
  role_class.message = '2'
  Ncurses.nodelay window, false
  UI::Two.draw_lose
  sleep 1
  window.getch
end

.lose?Boolean

Returns:

  • (Boolean)


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

def lose?
  @lose
end

.operationObject



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

def operation
  @operation
end

.over?Boolean

Returns:

  • (Boolean)


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

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

.pauseObject



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

def pause
  @used_time = time
  @pause_flag = true
end

.pause?Boolean

Returns:

  • (Boolean)


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

def pause?
  @pause_flag
end

.pause_or_continueObject



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

def pause_or_continue
  if pause?
    continue
  else
    pause
  end
end

.restartObject



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

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



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

def score
  snake.body.length - 2
end

.snakeObject



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

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

.speedObject



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

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
# File 'lib/ruby_snake/game.rb', line 8

def start(role=nil)
  self.role = role unless role.nil?
  @time_flag = Time.now.to_f
  if role == 'server'
    start_server_thread
  elsif role == 'client'
    start_client_thread
  end

  UI.draw
end

.start_client_threadObject



267
268
269
270
271
272
273
274
275
276
# File 'lib/ruby_snake/game.rb', line 267

def start_client_thread
  host = connected_host
  Thread.new { Client.connect host }
  sleep 0.35
  raise 'Connect failed' if Client.message == '10'
  puts '=' * 40
  puts "Connect to #{host}..." 
  puts '=' * 40
  countdown
end

.start_server_threadObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/ruby_snake/game.rb', line 252

def start_server_thread
  system 'clear'
  puts '=' * 40
  puts 'Waiting for a connection...' 
  puts '=' * 40
  Thread.new { Server.start }
  loop do
    sleep 0.2
    if Server.message != '0'
      countdown
      break
    end
  end
end

.stopObject



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

def stop
  @operation = QUIT
end

.timeObject

one player



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

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



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

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

.winObject

two players



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

def win
  @win = true
end

.win!Object



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

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

.win?Boolean

Returns:

  • (Boolean)


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

def win?
  @win
end

.windowObject



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

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