starx 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/starx +162 -174
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1766004e6d683bafb2bd91cda59db189d97a615
4
- data.tar.gz: 090ce82321ae8f293d5b22b30c21aee653a2d6bc
3
+ metadata.gz: 8938a1480f84d937813dc5f5dd87e7a9ea525bfe
4
+ data.tar.gz: 4146bf0e98d355b9609e1c5ee2a4df9b5a8ccd00
5
5
  SHA512:
6
- metadata.gz: 4045128452861c9d176bdecb100934cebb599ce4999fe2af3265bfd4630b1b4ef831077a71a253a353e8ede19b6e97e9a24b97ac43567552966f7c4483a60b7f
7
- data.tar.gz: 869a6a660d3087085ead46be40f4bf818ddce4f4780409391dda8ab674d272715db8b0a50c7717feb4bdee09e88cb3c125d6ae43ff0d92b9ff0178b0fd9f7b6d
6
+ metadata.gz: fbc9e1d8716759caa0f7c8ee084be3834ff0a898b5067723a87c43190022d917a9111da7384074df57484613c9f3379070da57f32e2aa51625bcff09496c869a
7
+ data.tar.gz: da32342dccb4df913c1d46b705bb8db88c9c18c88f0c5eba520202173e54948f721c5f6e7ce7ffd2182b84b830305f98f8adab268e69f75f9baa0deafa023412
data/bin/starx CHANGED
@@ -1,65 +1,75 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'io/wait'
3
3
 
4
- WIDTH = %x{tput cols}.strip.to_i
5
- HEIGHT = %x{tput lines}.strip.to_i - 3
4
+ WIDTH = `tput cols`.to_i
5
+ HEIGHT = `tput lines`.to_i - 3
6
6
 
7
7
  class Starx
8
8
  def initialize
9
- system 'tput civis'
10
9
  @cells = {}
11
- for y in 1..HEIGHT
12
- y = "0#{y}" if y < 10
13
- for x in 1..WIDTH
14
- x = "0#{x}" if x < 10
15
- key = "#{y}.#{x}"
16
- @cells[key] = :empty
17
- end
18
- end
19
-
20
- player_key = "#{ HEIGHT / 2 }.05"
21
- @cells[player_key] = :player
22
-
23
- display
24
- puts " "
25
-
10
+ (1..HEIGHT).each do |y|
11
+ (1..WIDTH).each do |x|
12
+ y = add_zero_if_needed(y)
13
+ x = add_zero_if_needed(x)
14
+ key = "#{y}.#{x}"
15
+ if key == "#{ HEIGHT / 2 }.05"
16
+ @cells[key] = :player
17
+ else
18
+ @cells[key] = :empty
19
+ end
20
+ end
21
+ end
26
22
  @tickcount = 0
27
23
  @score = 0
24
+ display
28
25
  end
29
26
 
27
+ def add_zero_if_needed(value)
28
+ value = value.to_i
29
+ if value < 10
30
+ value = "0#{value}"
31
+ else
32
+ value.to_s
33
+ end
34
+ end
35
+
30
36
  def display
31
- # sort the hash so it prints out correctly
32
- @cells = @cells.sort_by { |key,_| key.to_f }
37
+ sort_cells
38
+ print_score
39
+ @cells.each do |_, value|
40
+ case value
41
+ when :full
42
+ print '*'
43
+ when :empty
44
+ print " "
45
+ when :player
46
+ print "\e[31m>\e[0m"
47
+ end
48
+ end
49
+ print_asterisks
50
+ end
33
51
 
52
+ def print_score
34
53
  score_length = "SCORE: #{@score} ".length
35
54
  print "\e[32mSCORE:\e[0m #{@score} "
36
55
  (WIDTH - score_length).times {print '*'}
37
- @cells.each do |position, value|
38
- position_array = position.split('.')
39
- y = position_array[0]
40
- x = position_array[1]
41
- case value
42
- when :full
43
- print '*'
44
- when :empty
45
- print " "
46
- when :player
47
- print "\e[31m>\e[0m"
48
- end
49
- end
50
- WIDTH.times {print '*'}
56
+ end
51
57
 
52
- end
58
+ def print_asterisks
59
+ WIDTH.times {print '*'}
60
+ end
61
+
62
+ def sort_cells
63
+ @cells = @cells.sort_by { |key,_| key.to_f }
64
+ end
53
65
 
54
66
  def tick
55
67
  @tickcount += 1
56
68
  if @tickcount >= WIDTH && @tickcount % 20 == 0
57
69
  @score += 1
58
70
  end
59
-
60
71
  new_cells
61
72
  keys
62
-
63
73
  if collision?
64
74
  try_again_screen
65
75
  else
@@ -73,108 +83,98 @@ class Starx
73
83
  position_array = position.split('.')
74
84
  y = position_array[0].to_i
75
85
  x = position_array[1].to_i - 1
76
- x = "0#{x}" if x < 10
77
- y = "0#{y}" if y < 10
86
+ x = add_zero_if_needed(x)
87
+ y = add_zero_if_needed(y)
78
88
  unless x.to_i == 0
79
89
  new_key = "#{y}.#{x}"
80
90
  new_cells[new_key] = value
81
91
  end
82
92
  end
83
93
 
84
- # switch player cell with empty cell to the right
85
- player_before_switch = new_cells.select {|_,v| v == :player}.keys[0].split('.')
86
- x_player_before_switch = player_before_switch[1].to_i
87
- y_player_before_switch = player_before_switch[0]
88
-
89
- x_cell_after_player = x_player_before_switch + 1
90
-
91
- x_player_before_switch = "0#{x_player_before_switch}" if x_player_before_switch < 10
92
- x_cell_after_player = "0#{x_cell_after_player}" if x_cell_after_player < 10
93
-
94
- cell_after_player = "#{y_player_before_switch}.#{x_cell_after_player}"
95
- player_before_switch = player_before_switch.join('.')
96
-
97
- new_cells.delete(cell_after_player)
98
- new_cells.delete(player_before_switch)
99
- new_cells[cell_after_player] = :player
100
- new_cells[player_before_switch] = :empty
94
+ @cells = new_cells
95
+ switch_player_with(0,1)
96
+ new_column
97
+ end
101
98
 
102
- # new column
99
+ def new_column
103
100
  if @tickcount % 20 == 0 || @tickcount == 1
101
+ new_full_column
102
+ else
104
103
  x = WIDTH
105
- space = HEIGHT / 5
106
- random = rand(HEIGHT - space)
107
- for y in 1..random
108
- y = "0#{y}" if y < 10
104
+ (1..HEIGHT).each do |y|
105
+ y = add_zero_if_needed(y)
109
106
  key = "#{y}.#{x}"
110
- new_cells[key] = :full
107
+ @cells[key] = :empty
111
108
  end
112
- for y in random..(random + space)
113
- y = "0#{y}" if y < 10
109
+ end
110
+ end
111
+
112
+ def new_full_column
113
+ x = WIDTH
114
+ space = HEIGHT / 5
115
+ random = rand(HEIGHT - space)
116
+ (1..random).each do |y|
117
+ y = add_zero_if_needed(y)
114
118
  key = "#{y}.#{x}"
115
- new_cells[key] = :empty
119
+ @cells[key] = :full
116
120
  end
117
- for y in (random + space)..HEIGHT
121
+ (random..(random + space)).each do |y|
122
+ y = add_zero_if_needed(y)
118
123
  key = "#{y}.#{x}"
119
- new_cells[key] = :full
124
+ @cells[key] = :empty
120
125
  end
121
- else
122
- x = WIDTH
123
- for y in 1..HEIGHT
126
+ ((random + space)..HEIGHT).each do |y|
127
+ y = add_zero_if_needed(y)
124
128
  key = "#{y}.#{x}"
125
- new_cells[key] = :empty
129
+ @cells[key] = :full
126
130
  end
127
- end
128
-
129
- @cells = new_cells
130
- end
131
+ end
131
132
 
132
133
  def move_up!
133
- switch_player_with(1,0)
134
+ switch_player_with(-1,0)
134
135
  end
135
136
 
136
137
  def move_down!
137
- switch_player_with(-1,0)
138
+ switch_player_with(1,0)
138
139
  end
139
140
 
140
- def move_forward!
141
- switch_player_with(0,-1)
142
- end
141
+ def move_forward!
142
+ switch_player_with(0,1)
143
+ end
143
144
 
144
- def move_backward!
145
- switch_player_with(0,1)
146
- end
147
-
148
- def switch_player_with(y,x)
149
- player_x = find_player[1].to_i
150
- player_y = find_player[0].to_i
151
- cell_x = player_x - x
152
- cell_y = player_y - y
153
-
154
- player_x = "0#{player_x}" if player_x < 10
155
- player_y = "0#{player_y}" if player_y < 10
156
- cell_x = "0#{cell_x}" if cell_x < 10
157
- cell_y = "0#{cell_y}" if cell_y < 10
158
-
159
- cell_key = "#{cell_y}.#{cell_x}"
160
- player_key = "#{player_y}.#{player_x}"
161
-
162
- @cells.delete(cell_key)
163
- @cells.delete(player_key)
164
- @cells[player_key] = :empty
165
- @cells[cell_key] = :player
166
- end
167
-
168
- def find_player
169
- @cells.select {|_,v| v == :player}.keys[0].split('.')
170
- end
145
+ def move_backward!
146
+ switch_player_with(0,-1)
147
+ end
148
+
149
+ def switch_player_with(y,x)
150
+ player_x = find_player[1].to_i
151
+ player_y = find_player[0].to_i
152
+ cell_x = player_x + x
153
+ cell_y = player_y + y
154
+
155
+ player_x = add_zero_if_needed(player_x)
156
+ player_y = add_zero_if_needed(player_y)
157
+ cell_x = add_zero_if_needed(cell_x)
158
+ cell_y = add_zero_if_needed(cell_y)
159
+
160
+ cell_key = "#{cell_y}.#{cell_x}"
161
+ player_key = "#{player_y}.#{player_x}"
162
+
163
+ @cells.delete(cell_key)
164
+ @cells.delete(player_key)
165
+ @cells[player_key] = :empty
166
+ @cells[cell_key] = :player
167
+ end
168
+
169
+ def find_player
170
+ @cells.select {|_,v| v == :player}.keys[0].split('.')
171
+ end
171
172
 
172
173
  def collision?
173
174
  collision = false
174
175
  player_x = find_player[1].to_i
175
176
  player_y = find_player[0]
176
- cell_in_front_x = player_x + 1
177
- cell_in_front_x = "0#{cell_in_front_x}" if cell_in_front_x < 10
177
+ cell_in_front_x = add_zero_if_needed(player_x + 1)
178
178
  cell_in_front = "#{player_y}.#{cell_in_front_x}"
179
179
 
180
180
  value_of_cell_in_front = @cells.select {|k, v| k == cell_in_front}.values[0]
@@ -186,48 +186,49 @@ class Starx
186
186
 
187
187
  def keys
188
188
  c = read_char
189
- case c
190
- when 'q'
191
- system 'tput cnorm'
192
- Process.exit! true
193
- when "\e[A"
194
- move_up!
195
- when "\e[B"
196
- move_down!
197
- when "\e[C"
198
- move_forward!
199
- when "\e[D"
200
- move_backward!
201
- end
189
+ case c
190
+ when 'q'
191
+ quit_nicely
192
+ when "\e[A"
193
+ move_up!
194
+ when "\e[B"
195
+ move_down!
196
+ when "\e[C"
197
+ move_forward!
198
+ when "\e[D"
199
+ move_backward!
200
+ when "\u0003"
201
+ quit_nicely
202
+ end
202
203
  end
203
204
 
205
+ def quit_nicely
206
+ system 'stty -raw echo'
207
+ system 'tput cnorm'
208
+ system 'clear'
209
+ Process.exit! true
210
+ end
211
+
204
212
  def read_char
205
- begin
206
- system 'stty raw -echo'
207
- if $stdin.ready?
208
- c = $stdin.getc.chr
209
- if c == "\e"
210
- extra_thread = Thread.new do
211
- c += $stdin.getc.chr
212
- c += $stdin.getc.chr
213
- end
214
- extra_thread.join 0.00001
215
- extra_thread.kill
216
- end
217
- end
218
- ensure
219
- system 'stty -raw echo'
220
- end
221
- c
213
+ system 'tput civis'
214
+ system 'stty raw -echo'
215
+ if $stdin.ready?
216
+ c = $stdin.getc.chr
217
+ if c == "\e"
218
+ extra_thread = Thread.new do
219
+ c += $stdin.getc.chr
220
+ c += $stdin.getc.chr
222
221
  end
222
+ extra_thread.join 0.00001
223
+ extra_thread.kill
224
+ end
225
+ end
226
+ c
227
+ end
223
228
 
224
229
  def read_char_menu
225
- begin
226
- system 'stty raw -echo'
227
- c = $stdin.getc.chr
228
- ensure
229
- system 'stty -raw echo'
230
- end
230
+ system 'stty raw -echo'
231
+ c = $stdin.getc.chr
231
232
  c
232
233
  end
233
234
 
@@ -237,48 +238,35 @@ class Starx
237
238
  when 'a'
238
239
  Starx.new.play
239
240
  when 'q'
240
- system 'tput cnorm'
241
- system 'clear'
242
- Process.exit! true
241
+ quit_nicely
242
+ when "\u0003"
243
+ quit_nicely
243
244
  else
244
245
  try_again_screen
245
246
  end
246
247
  end
247
248
 
248
249
  def try_again_screen
250
+ system 'stty -raw'
251
+ (HEIGHT / 2).times{ WIDTH.times { print '*' } }
252
+ (WIDTH / 2 - 10).times { print ' ' }
253
+ puts "\e[31m You died. Haha!\e[0m"
254
+ (WIDTH / 2 - 10).times { print ' ' }
255
+ puts "\e[32m Your score: #{@score}\e[0m"
256
+ (WIDTH / 2 - 10).times { print ' ' }
257
+ puts "To try again hit 'a'"
258
+ (WIDTH / 2 - 10).times { print ' ' }
259
+ puts "To quit hit 'q'"
249
260
  (HEIGHT / 2).times{ WIDTH.times { print '*' } }
250
-
251
- num = WIDTH / 2
252
- (WIDTH / 2 - 10).times{ print ' '}
253
- print "\e[31m You died. Haha!\e[0m\n"
254
- puts ''
255
- (WIDTH / 2 - 10).times{ print ' '}
256
- print "\e[32m Your score: #{@score}\e[0m\n"
257
- puts ''
258
- puts ''
259
- (WIDTH / 2 - 10).times{ print ' '}
260
- print "To try again hit 'a'\n"
261
- (WIDTH / 2 - 10).times{ print ' '}
262
- print "To quit hit 'q'\n"
263
-
264
- (HEIGHT / 2 - 1).times{ WIDTH.times { print '*' } }
265
- puts ''
266
261
  keys_menu
267
-
268
262
  end
269
263
 
270
264
  def play
271
265
  loop do
272
- begin
273
- tick
266
+ tick
274
267
  sleep 0.04
275
- system 'clear'
276
- rescue Interrupt
277
- system 'tput cnorm'
278
- system 'clear'
279
- Process.exit! true
280
- end
281
- end
268
+ system 'clear'
269
+ end
282
270
  end
283
271
  end
284
272
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justyna Rachowicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-04 00:00:00.000000000 Z
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: justynarachowicz@gmail.com
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 2.4.6
41
+ rubygems_version: 2.4.3
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Simple, terminal-based game written in ruby