-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyan_rush.rb
290 lines (231 loc) · 5.26 KB
/
nyan_rush.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# encoding: utf-8
require "bundler/setup"
require "gaminator"
class NyanRush
class Primitive < Struct.new(:x, :y, :char, :color); end
###########################################################
class Matrix
def initialize(strings, x, y)
@data = []
if strings.respond_to? :length and strings[0].respond_to? :length
@height = strings.length
@width = strings[0].length
@x = x
@y = y
for i in 0..(@height - 1)
j = x
strings[i].each_char do |char|
primitive = Primitive.new(j, i + y, char, Curses::COLOR_WHITE)
@data.push primitive
j += 1
end
end
end
end
def translate(x, y)
@data.each do |primitive|
primitive.x += x
primitive.y += y
end
clear
end
def clear
@data = @data.select { |primitive| primitive.x >= 0}
end
def append(chars, colors, x, y)
for i in 0..(chars.length - 1)
primitive = Primitive.new(x, y + i, chars[i], colors[i])
@data.push primitive
end
end
attr_accessor :data
attr_reader :height
end
###########################################################
class Nyan
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class Head
def initialize(x, y, height)
@x = x - 6
@y = y - 2
@height = height
# http://evilzone.org/creative-arts/nyan-cat-ascii/
@matrix = Matrix.new([
',-----',
'| /\_/\\',
'|__( ^ .^)',
'"" ""'
], @x, @y
)
end
def moveUp
if @y > 0 then
@y -= 1
@matrix.translate(0, -1)
end
end
def moveDown
if @y + @matrix.height < @height then
@y += 1
@matrix.translate(0, 1)
end
end
def primitives
@matrix.data
end
attr_reader :x, :y
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class Tail
def initialize(head)
@head = head
@pattern = [
'o',
'o',
'o',
'o'
]
@colors = [
Curses::COLOR_RED,
Curses::COLOR_YELLOW,
Curses::COLOR_GREEN,
Curses::COLOR_BLUE,
]
@matrix = Matrix.new([''], @head.x - 1, @head.y)
end
def generate
@matrix.translate(-1, 0)
@matrix.clear
@matrix.append(@pattern, @colors, @head.x - 1, @head.y)
end
def primitives
@matrix.data
end
end
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def initialize(x, y, height)
@head = Head.new(x, y, height)
@tail = Tail.new(@head)
end
def update
@tail.generate
end
def primitives
@head.primitives + @tail.primitives
end
def moveUp
@head.moveUp
end
def moveDown
@head.moveDown
end
attr_reader :head
end
###########################################################
class Wall
def initialize(width, height)
@scr_width = width
@scr_height = height
@top_pattern = []
@bottom_pattern = []
generate
@top_matrix = Matrix.new(@top_pattern, @scr_width-1, 0)
@bottom_matrix = Matrix.new(@bottom_pattern, @scr_width-1, @doors+4)
end
def generate
@doors = Random.new.rand(0..(@scr_height-4))
for i in 0...@doors
@top_pattern.push("#")
end
for i in (@doors+4)...(@scr_height)
@bottom_pattern.push("#")
end
end
def update
@top_matrix.translate(-1, 0)
@bottom_matrix.translate(-1, 0)
end
def primitives
@top_matrix.data + @bottom_matrix.data
end
end
###########################################################
def initialize(width, height)
@interval = 0.05
@wall_counter = 0
@score = 0
@width = width
@height = height
@nyan = Nyan.new((width / 2).floor, (height / 2).floor, height)
@wall = Wall.new(width, height)
end
def wait?
false
end
def input_map
{
?w => :moveUp,
?s => :moveDown,
?k => :moveUp,
?j => :moveDown,
?q => :exit,
}
end
def sleep_time
@interval
end
def objects
@nyan.primitives + @wall.primitives
end
def check_collision
if collision?
exit
end
end
def collision?
@wall.primitives.each do |brick|
@nyan.head.primitives.each do |primitive|
if brick.y == primitive.y and brick.x == primitive.x
return true
end
end
end
false
end
def tick
@nyan.update
@wall.update
if @wall_counter > @width
@wall_counter = 0
@score += 1
@interval -= 0.01/@score
@wall = Wall.new(@width, @height)
end
check_collision
@wall_counter += 1
end
def moveUp
@nyan.moveUp
end
def moveDown
@nyan.moveDown
end
def textbox_content
if @score != 1
"Nyanyanyanyan!!! %d walls." % @score
else
"Nyanyanyanyan!!! 1 wall."
end
end
def exit
Kernel.exit
end
def exit_message
if @score != 1
puts "Nyan's dead... %d walls." % @score
else
"Nyan's dead... 1 wall."
end
end
end
Gaminator::Runner.new(NyanRush).run