starx 0.1.0

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