starx 0.1.5 → 0.1.6

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/starx +148 -150
  3. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8938a1480f84d937813dc5f5dd87e7a9ea525bfe
4
- data.tar.gz: 4146bf0e98d355b9609e1c5ee2a4df9b5a8ccd00
3
+ metadata.gz: 5a1da7c8823872de2af59e9ae6e2f330127a85fd
4
+ data.tar.gz: 9a5f80211863a95bbb87dccb04be837c3723f6e9
5
5
  SHA512:
6
- metadata.gz: fbc9e1d8716759caa0f7c8ee084be3834ff0a898b5067723a87c43190022d917a9111da7384074df57484613c9f3379070da57f32e2aa51625bcff09496c869a
7
- data.tar.gz: da32342dccb4df913c1d46b705bb8db88c9c18c88f0c5eba520202173e54948f721c5f6e7ce7ffd2182b84b830305f98f8adab268e69f75f9baa0deafa023412
6
+ metadata.gz: 53b4246c592d31818c505d4501fedbb68661cfb775327720e30cf22acca291510cb08a6dc4c48e196a39b58331700ffea4e6f8060b8163652d4a27fb48203d32
7
+ data.tar.gz: fcecf017b57ad2bf7a48261dce2d8d8934958b502e1eeb7773e22010c0d60433f8de47f3f556a7fc0ec5102bcd6f23606dc558657d85a476b7708fc3e2b07bbf
data/bin/starx CHANGED
@@ -5,24 +5,24 @@ WIDTH = `tput cols`.to_i
5
5
  HEIGHT = `tput lines`.to_i - 3
6
6
 
7
7
  class Starx
8
- def initialize
9
- @cells = {}
10
- (1..HEIGHT).each do |y|
8
+ def initialize
9
+ @cells = {}
10
+ (1..HEIGHT).each do |y|
11
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
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
20
  end
21
21
  end
22
- @tickcount = 0
23
- @score = 0
24
- display
25
- end
22
+ @tickcount = 0
23
+ @score = 0
24
+ display
25
+ end
26
26
 
27
27
  def add_zero_if_needed(value)
28
28
  value = value.to_i
@@ -33,10 +33,10 @@ class Starx
33
33
  end
34
34
  end
35
35
 
36
- def display
36
+ def display
37
37
  sort_cells
38
38
  print_score
39
- @cells.each do |_, value|
39
+ @cells.each do |_, value|
40
40
  case value
41
41
  when :full
42
42
  print '*'
@@ -45,14 +45,14 @@ class Starx
45
45
  when :player
46
46
  print "\e[31m>\e[0m"
47
47
  end
48
- end
49
- print_asterisks
50
- end
48
+ end
49
+ print_asterisks
50
+ end
51
51
 
52
52
  def print_score
53
- score_length = "SCORE: #{@score} ".length
54
- print "\e[32mSCORE:\e[0m #{@score} "
55
- (WIDTH - score_length).times {print '*'}
53
+ score_length = "SCORE: #{@score} ".length
54
+ print "\e[32mSCORE:\e[0m #{@score} "
55
+ (WIDTH - score_length).times {print '*'}
56
56
  end
57
57
 
58
58
  def print_asterisks
@@ -60,88 +60,87 @@ class Starx
60
60
  end
61
61
 
62
62
  def sort_cells
63
- @cells = @cells.sort_by { |key,_| key.to_f }
63
+ @cells = @cells.sort_by { |key,_| key.to_f }
64
64
  end
65
65
 
66
- def tick
67
- @tickcount += 1
68
- if @tickcount >= WIDTH && @tickcount % 20 == 0
69
- @score += 1
70
- end
71
- new_cells
72
- keys
73
- if collision?
74
- try_again_screen
75
- else
76
- display
77
- end
78
- end
79
-
80
- def new_cells
81
- new_cells = {}
82
- @cells.each do |position, value|
83
- position_array = position.split('.')
84
- y = position_array[0].to_i
85
- x = position_array[1].to_i - 1
86
- x = add_zero_if_needed(x)
87
- y = add_zero_if_needed(y)
88
- unless x.to_i == 0
89
- new_key = "#{y}.#{x}"
90
- new_cells[new_key] = value
91
- end
92
- end
66
+ def tick
67
+ @tickcount += 1
68
+ if @tickcount >= WIDTH && @tickcount % 20 == 0
69
+ @score += 1
70
+ end
71
+ new_cells
72
+ keys
73
+ if collision?
74
+ try_again_screen
75
+ else
76
+ display
77
+ end
78
+ end
93
79
 
94
- @cells = new_cells
80
+ def new_cells
81
+ new_cells = {}
82
+ @cells.each do |position, value|
83
+ position_array = position.split('.')
84
+ y = position_array[0].to_i
85
+ x = position_array[1].to_i - 1
86
+ x = add_zero_if_needed(x)
87
+ y = add_zero_if_needed(y)
88
+ unless x.to_i == 0
89
+ new_key = "#{y}.#{x}"
90
+ new_cells[new_key] = value
91
+ end
92
+ end
93
+ @cells = new_cells
95
94
  switch_player_with(0,1)
96
95
  new_column
97
- end
96
+ end
98
97
 
99
98
  def new_column
100
- if @tickcount % 20 == 0 || @tickcount == 1
99
+ if @tickcount % 20 == 0 || @tickcount == 1
101
100
  new_full_column
102
- else
103
- x = WIDTH
101
+ else
102
+ x = WIDTH
104
103
  (1..HEIGHT).each do |y|
105
104
  y = add_zero_if_needed(y)
106
- key = "#{y}.#{x}"
107
- @cells[key] = :empty
108
- end
109
- end
105
+ key = "#{y}.#{x}"
106
+ @cells[key] = :empty
107
+ end
108
+ end
110
109
  end
111
110
 
112
111
  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)
118
- key = "#{y}.#{x}"
119
- @cells[key] = :full
120
- end
121
- (random..(random + space)).each do |y|
122
- y = add_zero_if_needed(y)
123
- key = "#{y}.#{x}"
124
- @cells[key] = :empty
125
- end
126
- ((random + space)..HEIGHT).each do |y|
127
- y = add_zero_if_needed(y)
128
- key = "#{y}.#{x}"
129
- @cells[key] = :full
130
- end
112
+ x = WIDTH
113
+ space = HEIGHT / 5
114
+ random = rand(HEIGHT - space)
115
+ (1..random).each do |y|
116
+ y = add_zero_if_needed(y)
117
+ key = "#{y}.#{x}"
118
+ @cells[key] = :full
119
+ end
120
+ (random..(random + space)).each do |y|
121
+ y = add_zero_if_needed(y)
122
+ key = "#{y}.#{x}"
123
+ @cells[key] = :empty
124
+ end
125
+ ((random + space)..HEIGHT).each do |y|
126
+ y = add_zero_if_needed(y)
127
+ key = "#{y}.#{x}"
128
+ @cells[key] = :full
129
+ end
131
130
  end
132
-
133
- def move_up!
131
+
132
+ def move_up!
134
133
  switch_player_with(-1,0)
135
- end
134
+ end
136
135
 
137
- def move_down!
136
+ def move_down!
138
137
  switch_player_with(1,0)
139
- end
140
-
138
+ end
139
+
141
140
  def move_forward!
142
141
  switch_player_with(0,1)
143
142
  end
144
-
143
+
145
144
  def move_backward!
146
145
  switch_player_with(0,-1)
147
146
  end
@@ -170,47 +169,46 @@ class Starx
170
169
  @cells.select {|_,v| v == :player}.keys[0].split('.')
171
170
  end
172
171
 
173
- def collision?
174
- collision = false
175
- player_x = find_player[1].to_i
176
- player_y = find_player[0]
177
- cell_in_front_x = add_zero_if_needed(player_x + 1)
178
- cell_in_front = "#{player_y}.#{cell_in_front_x}"
179
-
180
- value_of_cell_in_front = @cells.select {|k, v| k == cell_in_front}.values[0]
181
- if value_of_cell_in_front == :full
182
- collision = true
183
- end
184
- collision
185
- end
186
-
187
- def keys
188
- c = read_char
172
+ def collision?
173
+ collision = false
174
+ player_x = find_player[1].to_i
175
+ player_y = find_player[0]
176
+ cell_in_front_x = add_zero_if_needed(player_x + 1)
177
+ cell_in_front = "#{player_y}.#{cell_in_front_x}"
178
+ value_of_cell_in_front = @cells.select {|k, v| k == cell_in_front}.values[0]
179
+ if value_of_cell_in_front == :full
180
+ collision = true
181
+ end
182
+ collision
183
+ end
184
+
185
+ def keys
186
+ c = read_char
189
187
  case c
190
188
  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!
189
+ quit_nicely
190
+ when "\e[A", 'k'
191
+ move_up!
192
+ when "\e[B", 'j'
193
+ move_down!
194
+ when "\e[C", 'l'
195
+ move_forward!
196
+ when "\e[D", 'h'
197
+ move_backward!
200
198
  when "\u0003"
201
- quit_nicely
199
+ quit_nicely
202
200
  end
203
- end
201
+ end
204
202
 
205
203
  def quit_nicely
206
204
  system 'stty -raw echo'
207
- system 'tput cnorm'
205
+ system 'tput cnorm'
208
206
  system 'clear'
209
- Process.exit! true
207
+ Process.exit! true
210
208
  end
211
209
 
212
- def read_char
213
- system 'tput civis'
210
+ def read_char
211
+ system 'tput civis'
214
212
  system 'stty raw -echo'
215
213
  if $stdin.ready?
216
214
  c = $stdin.getc.chr
@@ -218,56 +216,56 @@ class Starx
218
216
  extra_thread = Thread.new do
219
217
  c += $stdin.getc.chr
220
218
  c += $stdin.getc.chr
221
- end
222
- extra_thread.join 0.00001
223
- extra_thread.kill
219
+ end
220
+ extra_thread.join 0.00001
221
+ extra_thread.kill
224
222
  end
225
223
  end
226
224
  c
227
225
  end
228
226
 
229
- def read_char_menu
230
- system 'stty raw -echo'
231
- c = $stdin.getc.chr
232
- c
233
- end
227
+ def read_char_menu
228
+ system 'stty raw -echo'
229
+ c = $stdin.getc.chr
230
+ c
231
+ end
234
232
 
235
- def keys_menu
236
- c = read_char_menu
237
- case c
238
- when 'a'
239
- Starx.new.play
240
- when 'q'
241
- quit_nicely
233
+ def keys_menu
234
+ c = read_char_menu
235
+ case c
236
+ when 'a'
237
+ Starx.new.play
238
+ when 'q'
239
+ quit_nicely
242
240
  when "\u0003"
243
- quit_nicely
244
- else
245
- try_again_screen
246
- end
247
- end
241
+ quit_nicely
242
+ else
243
+ try_again_screen
244
+ end
245
+ end
248
246
 
249
- def try_again_screen
247
+ def try_again_screen
250
248
  system 'stty -raw'
251
- (HEIGHT / 2).times{ WIDTH.times { print '*' } }
249
+ (HEIGHT / 2).times{ WIDTH.times { print '*' } }
252
250
  (WIDTH / 2 - 10).times { print ' ' }
253
- puts "\e[31m You died. Haha!\e[0m"
251
+ puts "\e[31m You died. Haha!\e[0m"
254
252
  (WIDTH / 2 - 10).times { print ' ' }
255
- puts "\e[32m Your score: #{@score}\e[0m"
253
+ puts "\e[32m Your score: #{@score}\e[0m"
256
254
  (WIDTH / 2 - 10).times { print ' ' }
257
- puts "To try again hit 'a'"
255
+ puts "To try again hit 'a'"
258
256
  (WIDTH / 2 - 10).times { print ' ' }
259
- puts "To quit hit 'q'"
260
- (HEIGHT / 2).times{ WIDTH.times { print '*' } }
261
- keys_menu
262
- end
257
+ puts "To quit hit 'q'"
258
+ (HEIGHT / 2).times{ WIDTH.times { print '*' } }
259
+ keys_menu
260
+ end
263
261
 
264
- def play
265
- loop do
262
+ def play
263
+ loop do
266
264
  tick
267
- sleep 0.04
268
- system 'clear'
265
+ sleep 0.04
266
+ system 'clear'
269
267
  end
270
- end
268
+ end
271
269
  end
272
270
 
273
271
  g = Starx.new
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.5
4
+ version: 0.1.6
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-19 00:00:00.000000000 Z
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: justynarachowicz@gmail.com
@@ -38,8 +38,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 2.4.3
41
+ rubygems_version: 2.4.6
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Simple, terminal-based game written in ruby
45
45
  test_files: []
46
+ has_rdoc: