Class: RubySnake::Game
- Inherits:
-
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_mode ⇒ Object
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_role ⇒ Object
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.
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_host ⇒ Object
214
215
216
217
218
|
# File 'lib/ruby_snake/game.rb', line 214
def connected_host
system 'clear'
print "Input host to connect: "
gets.chomp
end
|
.continue ⇒ Object
167
168
169
170
|
# File 'lib/ruby_snake/game.rb', line 167
def continue
@time_flag = Time.now.to_f
@pause_flag = false
end
|
.direction_opposite?(o) ⇒ 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
|
.get_host ⇒ Object
248
249
250
251
252
253
254
|
# File 'lib/ruby_snake/game.rb', line 248
def get_host
system 'clear'
@host = connected_host
puts '=' * 40
puts "Connect to #{host}..."
puts '=' * 40
end
|
.host ⇒ Object
244
245
246
|
# File 'lib/ruby_snake/game.rb', line 244
def host
@host
end
|
.level ⇒ Object
123
124
125
|
# File 'lib/ruby_snake/game.rb', line 123
def level
score / 10 + 1
end
|
.listen_operation ⇒ Object
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
|
.lose ⇒ Object
199
200
201
|
# File 'lib/ruby_snake/game.rb', line 199
def lose
@lose = true
end
|
.lose! ⇒ Object
207
208
209
210
211
212
|
# File 'lib/ruby_snake/game.rb', line 207
def lose!
Ncurses.nodelay window, false
UI::Two.draw_lose
sleep 1
window.getch
end
|
.lose? ⇒ Boolean
203
204
205
|
# File 'lib/ruby_snake/game.rb', line 203
def lose?
@lose
end
|
.operation ⇒ Object
147
148
149
|
# File 'lib/ruby_snake/game.rb', line 147
def operation
@operation ||= RIGHT
end
|
.over? ⇒ 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
|
.pause ⇒ Object
162
163
164
165
|
# File 'lib/ruby_snake/game.rb', line 162
def pause
@used_time = time
@pause_flag = true
end
|
.pause? ⇒ Boolean
158
159
160
|
# File 'lib/ruby_snake/game.rb', line 158
def pause?
@pause_flag
end
|
.pause_or_continue ⇒ Object
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
|
.restart ⇒ Object
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
|
.role ⇒ Object
224
225
226
|
# File 'lib/ruby_snake/game.rb', line 224
def role
@role
end
|
.role=(role) ⇒ Object
228
229
230
|
# File 'lib/ruby_snake/game.rb', line 228
def role= role
@role = role
end
|
.role_class ⇒ Object
232
233
234
235
236
237
238
|
# File 'lib/ruby_snake/game.rb', line 232
def role_class
if role == 'server'
Server
else
Client
end
end
|
.score ⇒ Object
119
120
121
|
# File 'lib/ruby_snake/game.rb', line 119
def score
snake.body.length - 2
end
|
.snake ⇒ Object
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
|
.speed ⇒ Object
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
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/ruby_snake/game.rb', line 7
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
|
.stop ⇒ Object
220
221
222
|
# File 'lib/ruby_snake/game.rb', line 220
def stop
@operation = QUIT
end
|
.time ⇒ Object
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
240
241
242
|
# File 'lib/ruby_snake/game.rb', line 240
def vs?
!!self.role
end
|
.welcome ⇒ Object
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
|
.win ⇒ Object
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 = '1'
Ncurses.nodelay window, false
UI::Two.draw_win
sleep 1
window.getch
end
|
.win? ⇒ Boolean
187
188
189
|
# File 'lib/ruby_snake/game.rb', line 187
def win?
@win
end
|
.window ⇒ Object
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
|