ruby_snake 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ruby_snake/client.rb +3 -7
- data/lib/ruby_snake/server.rb +2 -6
- data/lib/ruby_snake/ui/two.rb +26 -54
- data/lib/ruby_snake/version.rb +1 -1
- metadata +1 -1
data/lib/ruby_snake/client.rb
CHANGED
@@ -3,13 +3,13 @@ module RubySnake
|
|
3
3
|
|
4
4
|
# The following code is a shit
|
5
5
|
class << self
|
6
|
-
attr_accessor :message, :
|
6
|
+
attr_accessor :message, :receive
|
7
7
|
|
8
8
|
def connect(host='127.0.0.1')
|
9
9
|
@message = '0'
|
10
10
|
|
11
11
|
loop do
|
12
|
-
sleep
|
12
|
+
sleep 0.1
|
13
13
|
|
14
14
|
begin
|
15
15
|
server = TCPSocket.new host, 2000
|
@@ -34,11 +34,7 @@ module RubySnake
|
|
34
34
|
break
|
35
35
|
else
|
36
36
|
@message = ([Game.snake.food] + Game.snake.body).join(',').to_s if Game.snake
|
37
|
-
|
38
|
-
if array
|
39
|
-
@food = array.shift
|
40
|
-
@snake = array
|
41
|
-
end
|
37
|
+
@receive = server_message.split(',').map(&:to_i).each_slice(2).to_a if server_message
|
42
38
|
end
|
43
39
|
end
|
44
40
|
end
|
data/lib/ruby_snake/server.rb
CHANGED
@@ -3,7 +3,7 @@ module RubySnake
|
|
3
3
|
|
4
4
|
# The following code is a shit
|
5
5
|
class << self
|
6
|
-
attr_accessor :message, :
|
6
|
+
attr_accessor :message, :receive
|
7
7
|
|
8
8
|
def start
|
9
9
|
@message = '0'
|
@@ -28,11 +28,7 @@ module RubySnake
|
|
28
28
|
break
|
29
29
|
else
|
30
30
|
@message = ([Game.snake.food] + Game.snake.body).join(',').to_s if Game.snake
|
31
|
-
|
32
|
-
if array
|
33
|
-
@food = array.shift
|
34
|
-
@snake = array
|
35
|
-
end
|
31
|
+
@receive = client_message.split(',').map(&:to_i).each_slice(2).to_a if client_message
|
36
32
|
end
|
37
33
|
end
|
38
34
|
end
|
data/lib/ruby_snake/ui/two.rb
CHANGED
@@ -11,16 +11,16 @@ module RubySnake
|
|
11
11
|
|
12
12
|
def local_window
|
13
13
|
x = (Ncurses.stdscr.getmaxy - 30) / 2
|
14
|
-
y = (Ncurses.stdscr.getmaxx -
|
14
|
+
y = (Ncurses.stdscr.getmaxx - 102) / 2
|
15
15
|
|
16
|
-
@local_window ||= Ncurses::WINDOW.new 30,
|
16
|
+
@local_window ||= Ncurses::WINDOW.new 30, 51, x, y
|
17
17
|
end
|
18
18
|
|
19
19
|
def remote_window
|
20
20
|
x = (Ncurses.stdscr.getmaxy - 30) / 2
|
21
21
|
y = Ncurses.stdscr.getmaxx / 2
|
22
22
|
@remote_window ||= Ncurses::WINDOW.new 30,
|
23
|
-
|
23
|
+
51,
|
24
24
|
x,
|
25
25
|
y
|
26
26
|
end
|
@@ -80,7 +80,6 @@ module RubySnake
|
|
80
80
|
draw_food
|
81
81
|
draw_score
|
82
82
|
window.noutrefresh
|
83
|
-
Ncurses.doupdate
|
84
83
|
clear_tail
|
85
84
|
end
|
86
85
|
|
@@ -90,66 +89,39 @@ module RubySnake
|
|
90
89
|
remote_window.mvprintw x, y, symbol
|
91
90
|
end
|
92
91
|
|
93
|
-
def
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
92
|
+
def draw_remote
|
93
|
+
begin
|
94
|
+
receive = Game.role_class.receive
|
95
|
+
food = receive.shift
|
96
|
+
snake = receive
|
97
|
+
|
98
|
+
remote_window.color_set 1, nil
|
99
|
+
remote_display food, '$'
|
100
|
+
remote_window.mvprintw 0, 0, (50 - snake.length + 2).to_s
|
101
|
+
|
102
|
+
remote_window.color_set 2, nil
|
103
|
+
head = snake.shift
|
104
|
+
remote_display head, '@'
|
105
|
+
snake.each do |body|
|
107
106
|
remote_display body, 'o'
|
108
107
|
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
def remote_snake_body
|
113
|
-
Game.role_class.snake
|
114
|
-
end
|
115
108
|
|
116
|
-
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
def remote_snake_tail
|
121
|
-
Game.role_class.snake.last
|
122
|
-
end
|
109
|
+
remote_window.noutrefresh
|
110
|
+
remote_display head, ' '
|
123
111
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
def draw_remote_food
|
133
|
-
remote_window.color_set 1, nil
|
134
|
-
remote_display remote_food, '$'
|
135
|
-
end
|
136
|
-
|
137
|
-
def draw_remote
|
138
|
-
draw_remote_snake
|
139
|
-
draw_remote_food
|
140
|
-
draw_remote_score
|
141
|
-
remote_window.noutrefresh
|
142
|
-
Ncurses.doupdate
|
143
|
-
clear_remote_tail
|
112
|
+
snake.each do |body|
|
113
|
+
remote_display body, ' '
|
114
|
+
end
|
115
|
+
rescue
|
116
|
+
nil
|
117
|
+
end
|
144
118
|
end
|
145
119
|
|
146
120
|
def draw_map
|
147
121
|
draw_local
|
148
122
|
draw_remote
|
149
|
-
sleep Game.speed
|
150
|
-
window.noutrefresh
|
151
|
-
remote_window.noutrefresh
|
152
123
|
Ncurses.doupdate
|
124
|
+
sleep 0.1
|
153
125
|
end
|
154
126
|
|
155
127
|
def draw
|
data/lib/ruby_snake/version.rb
CHANGED