-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
363 lines (301 loc) · 6.68 KB
/
game.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#encoding: utf-8
require "bundler/setup"
require "gaminator"
class Array
def x
self.first
end
def y
self.last
end
end
class Fixnum
def sign
if self > 0
1
elsif self < 0
-1
else
0
end
end
end
class BindingGame
MOVE = {
:up => [0, -1],
:down => [0, 1],
:left => [-2, 0],
:right => [2, 0]
}
module Coordinates
def coordinates
[x, y]
end
def desired_coordinates(x, y)
[self.x + x, self.y + y]
end
def move(x, y)
self.x += x
self.y += y
end
end
class Wall < Struct.new(:x, :y)
include Coordinates
def char
'#'
end
end
class Bullet < Struct.new(:x, :y)
include Coordinates
attr_accessor :type, :vector
def initialize(type, vec, x, y)
self.type = type
self.vector = vec
super(x, y)
end
def char
'*'
end
def color
player? ? Curses::COLOR_GREEN : Curses::COLOR_RED
end
def player?
type == :player
end
def move
self.x += vector.x
self.y += vector.y
end
end
class Ascii < Struct.new(:x, :y)
include Coordinates
def char
'@'
end
def color
Curses::COLOR_BLUE
end
end
class Enemy < Struct.new(:x, :y)
include Coordinates
attr_accessor :tick, :last_shot
def initialize(x, y, tick = 0)
self.tick = tick
self.last_shot = rand(10)
super(x, y)
end
def tick_me
self.tick += 1
self.last_shot += 1
end
def char
'&'
end
def color
Curses::COLOR_YELLOW
end
def shoot
self.last_shot = rand(10)
end
end
class ChasingEnemy < Struct.new(:x, :y)
include Coordinates
attr_accessor :tick, :speed
def initialize(x, y)
self.tick = 0
self.speed = rand(8) + 5
super(x, y)
end
def char
"h"
end
def tick_me
self.tick += 1
end
def color
Curses::COLOR_MAGENTA
end
end
def initialize(width, height)
@bullets = []
@width = width
@height = height
@ascii = Ascii.new(rand((@width-2)/2+1)*2, rand(@height-3)+1)
@tick = 0
@killed = 0
@walls = []
@cwalls = []
@enemies = []
@chasing = []
initialize_walls
end
MOVE.each do |dir, vec|
define_method "move_#{dir}" do
unless @cwalls.include? @ascii.desired_coordinates(vec.x, vec.y)
@ascii.move(vec.x, vec.y)
end
end
define_method "shoot_#{dir}" do
@bullets << Bullet.new(:player, vec, @ascii.x, @ascii.y)
end
end
def initialize_walls
wp = @width%2 == 0 ? 2 : 1
(@width - wp + 1).times do |x|
@walls << Wall.new(x, 0)
@walls << Wall.new(x, @height-1)
end
@height.times do |y|
@walls << Wall.new(0, y)
@walls << Wall.new(@width-wp, y)
end
40.times do |i|
@walls << Wall.new(10+i,7)
end
40.times do |i|
@walls << Wall.new(30+i,14)
end
7.times do |i|
@walls << Wall.new(26,10+i)
end
10.times do |i|
@walls << Wall.new(56,3+i)
end
end
def check_bullet_collisions
check_wall_collisions
check_enemies_collisions
check_player_collisions
end
def check_wall_collisions
to_remove =[]
@bullets.each_with_index do |bullet, i|
to_remove << i if @cwalls.include? bullet.coordinates
end
to_remove.reverse.each { |i| @bullets.delete_at(i) }
end
def check_enemies_collisions
initial = @enemies.count + @chasing.count
@bullets.delete_if do |bullet|
[email protected]!{ |enemy| enemy.coordinates == bullet.coordinates && bullet.player? }
end
@bullets.delete_if do |bullet|
[email protected]!{ |enemy| enemy.coordinates == bullet.coordinates && bullet.player? }
end
@killed += initial - @enemies.count - @chasing.count
end
def check_player_collisions
@bullets.each do |bullet|
if @ascii.coordinates == bullet.coordinates
time = (@tick * sleep_time).round(2)
@status = "You lasted #{time} seconds and killed #{@killed} enemies. Your score: #{score}"
exit
end
end
end
def check_chasing_collisions
(@chasing + @enemies).each do |enemy|
if enemy.coordinates == @ascii.coordinates
time = (@tick * sleep_time).round(2)
@status = "You lasted #{time} seconds and killed #{@killed} enemies. Your score: #{score}"
exit
end
end
end
def score
((@tick * sleep_time).round(2) * 100 + @killed * 200).to_i
end
def move_enemies
move_shooters
move_chasers
end
def move_shooters
@enemies.each do |enemy|
try_to_shoot(enemy)
enemy.tick_me
if enemy.tick%7 == 0
vec = MOVE[MOVE.keys.sample]
enemy.move(vec.x, vec.y) unless @cwalls.include? enemy.desired_coordinates(vec.x, vec.y)
end
try_to_shoot(enemy)
end
end
def move_chasers
@chasing.each do |chaser|
chaser.tick_me
if chaser.tick%chaser.speed == 0
vec = [(@ascii.x - chaser.x).sign, (@ascii.y - chaser.y).sign]
vec[0] *= 2
chaser.move(vec.x, vec.y)
end
end
end
def try_to_shoot(enemy)
if enemy.last_shot > 20
if enemy.x == @ascii.x
@bullets << Bullet.new(:enemy, MOVE[ enemy.y - @ascii.y > 0 ? :up : :down ], enemy.x, enemy.y)
enemy.shoot
elsif enemy.y == @ascii.y
@bullets << Bullet.new(:enemy, MOVE[ enemy.x - @ascii.x > 0 ? :left : :right ], enemy.x, enemy.y)
enemy.shoot
end
end
end
def spawn_new_enemies
if @tick%30 == 0
how_many = @tick/30
chasers = rand(how_many+1)
(how_many - chasers).times do
@enemies << Enemy.new(rand((@width-2)/2+1)*2, rand(@height-3)+1)
end
chasers.times do
@chasing << ChasingEnemy.new(rand((@width-2)/2+1)*2, rand(@height-3)+1)
end
end
end
def tick
@cwalls = @walls.map(&:coordinates)
move_bullets
check_bullet_collisions
move_enemies
check_chasing_collisions
spawn_new_enemies
@tick += 1
end
def exit
@status ||= "Bye bye! See ya later!"
Kernel.exit
end
def exit_message
@status
end
def objects
@walls + @bullets + @enemies + @chasing + [@ascii]
end
def input_map
{
"a" => :move_left,
"d" => :move_right,
"w" => :move_up,
"s" => :move_down,
"k" => :shoot_up,
"j" => :shoot_down,
"h" => :shoot_left,
"l" => :shoot_right,
"q" => :exit
}
end
def move_bullets
@bullets.each(&:move)
end
def textbox_content
"w, s, a, d for move. h, j, k ,l for shoot. Fight if you can!"
end
def wait?
false
end
def sleep_time
0.05
end
end
Gaminator::Runner.new(BindingGame).run