starx 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/starx +148 -150
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a1da7c8823872de2af59e9ae6e2f330127a85fd
|
4
|
+
data.tar.gz: 9a5f80211863a95bbb87dccb04be837c3723f6e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def initialize
|
9
|
+
@cells = {}
|
10
|
+
(1..HEIGHT).each do |y|
|
11
11
|
(1..WIDTH).each do |x|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
36
|
+
def display
|
37
37
|
sort_cells
|
38
38
|
print_score
|
39
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
48
|
+
end
|
49
|
+
print_asterisks
|
50
|
+
end
|
51
51
|
|
52
52
|
def print_score
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
63
|
+
@cells = @cells.sort_by { |key,_| key.to_f }
|
64
64
|
end
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
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
|
-
|
96
|
+
end
|
98
97
|
|
99
98
|
def new_column
|
100
|
-
|
99
|
+
if @tickcount % 20 == 0 || @tickcount == 1
|
101
100
|
new_full_column
|
102
|
-
|
103
|
-
|
101
|
+
else
|
102
|
+
x = WIDTH
|
104
103
|
(1..HEIGHT).each do |y|
|
105
104
|
y = add_zero_if_needed(y)
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
105
|
+
key = "#{y}.#{x}"
|
106
|
+
@cells[key] = :empty
|
107
|
+
end
|
108
|
+
end
|
110
109
|
end
|
111
110
|
|
112
111
|
def new_full_column
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
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
|
-
|
131
|
+
|
132
|
+
def move_up!
|
134
133
|
switch_player_with(-1,0)
|
135
|
-
|
134
|
+
end
|
136
135
|
|
137
|
-
|
136
|
+
def move_down!
|
138
137
|
switch_player_with(1,0)
|
139
|
-
|
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
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|
-
|
192
|
-
when "\e[A"
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
when "\e[C"
|
197
|
-
|
198
|
-
when "\e[D"
|
199
|
-
|
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
|
-
|
199
|
+
quit_nicely
|
202
200
|
end
|
203
|
-
|
201
|
+
end
|
204
202
|
|
205
203
|
def quit_nicely
|
206
204
|
system 'stty -raw echo'
|
207
|
-
|
205
|
+
system 'tput cnorm'
|
208
206
|
system 'clear'
|
209
|
-
|
207
|
+
Process.exit! true
|
210
208
|
end
|
211
209
|
|
212
|
-
|
213
|
-
|
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
|
-
|
222
|
-
|
223
|
-
|
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
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
227
|
+
def read_char_menu
|
228
|
+
system 'stty raw -echo'
|
229
|
+
c = $stdin.getc.chr
|
230
|
+
c
|
231
|
+
end
|
234
232
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
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
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
241
|
+
quit_nicely
|
242
|
+
else
|
243
|
+
try_again_screen
|
244
|
+
end
|
245
|
+
end
|
248
246
|
|
249
|
-
|
247
|
+
def try_again_screen
|
250
248
|
system 'stty -raw'
|
251
|
-
|
249
|
+
(HEIGHT / 2).times{ WIDTH.times { print '*' } }
|
252
250
|
(WIDTH / 2 - 10).times { print ' ' }
|
253
|
-
|
251
|
+
puts "\e[31m You died. Haha!\e[0m"
|
254
252
|
(WIDTH / 2 - 10).times { print ' ' }
|
255
|
-
|
253
|
+
puts "\e[32m Your score: #{@score}\e[0m"
|
256
254
|
(WIDTH / 2 - 10).times { print ' ' }
|
257
|
-
|
255
|
+
puts "To try again hit 'a'"
|
258
256
|
(WIDTH / 2 - 10).times { print ' ' }
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
257
|
+
puts "To quit hit 'q'"
|
258
|
+
(HEIGHT / 2).times{ WIDTH.times { print '*' } }
|
259
|
+
keys_menu
|
260
|
+
end
|
263
261
|
|
264
|
-
|
265
|
-
|
262
|
+
def play
|
263
|
+
loop do
|
266
264
|
tick
|
267
|
-
|
268
|
-
|
265
|
+
sleep 0.04
|
266
|
+
system 'clear'
|
269
267
|
end
|
270
|
-
|
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.
|
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-
|
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.
|
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:
|