-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-build.rb
342 lines (279 loc) · 5.38 KB
/
main-build.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
require 'ruby2d'
class Shot
attr_reader :body
def initialize(opt = {})
@x = opt[:x]
@y = opt[:y]
@y_speed = 30
@body = draw
end
def draw
width = 10
height = 50
x = @x - (width / 2)
y = @y - height
Rectangle.new(
x: x,
y: y,
width: 10,
height: 50,
color: 'lime'
)
end
def fly
@body.y -= @y_speed
if @body.y > Window.height
@body.remove
end
end
def hit(block)
@body.remove
block.damage
end
end
class Weapon
def initialize; end
def shoot(x, y)
Shot.new(x: x, y: y)
end
end
# The game hero
class Hero
attr_reader :body, :shots
def initialize
@width = 150
@height = 25
@x_speed = 10
@body = draw
@weapon = Weapon.new
@shots = []
end
def draw
x = (Window.width / 2) - (@width / 2)
y = Window.height - @height
Rectangle.new(
x: x, y: y,
width: @width, height: @height,
color: 'teal',
z: 20
)
end
def move(direction)
if direction == 'left' && @body.x > -( @width / 2 )
@body.x -= @x_speed
elsif direction == 'right' && @body.x < Window.width - (@body.width / 2)
@body.x += @x_speed
end
end
def shoot
x = @body.x + (@width / 2)
y = @body.y
@shots << @weapon.shoot(x, y)
end
def moving
@body.x += @x_speed
end
end
# A block object to destroy that can have one of these 3 colors:
# - yellow : 1 shot to destroy
# - orange : 2 shots to destroy
# - red : 3 shots to destroy
class Block
attr_reader :body, :status
COLORS = ['yellow', 'orange', 'red']
def initialize(opt = {})
@x = opt[:x]
@y = opt[:y]
@size = opt[:size]
@status = rand(3)
@body = draw
end
def draw
Square.new(
x: @x, y: @y,
size: @size,
color: COLORS[@status]
)
end
def go_down
@y += 1
@body.y = @y * (@size + 1)
end
def collision_detected?(shot)
@body.contains?(shot.body.x1, shot.body.y1) ||
@body.contains?(shot.body.x2, shot.body.y2)
end
def damage
@status -= 1
if is_detroyed?
@body.remove
else
@body.color = COLORS[@status]
end
end
def is_detroyed?
@status < 0
end
end
# A raw of blocks
class Raw
attr_reader :blocks
N_ITEMS = 10
def initialize(opt = {})
y = opt[:y]
size = (Window.width / N_ITEMS)
@blocks = []
N_ITEMS.times do |x|
@blocks << Block.new(
x: x * (size + 1),
y: y * (size + 1),
size: size - 1,
)
end
end
end
# The limit until blocks can go down before game over.
class Limit
attr_reader :body
def initialize(opt = {})
@body = draw
end
def draw
y = Window.height - 170
Line.new(
x1: 0, y1: y,
x2: Window.width, y2: y,
width: 1,
color: 'silver',
z: 20
)
end
end
class Overlay
def initialize(opt = {})
@body = draw
@text_body = draw_text(opt[:text])
center_text
hide # hidden by defautl
end
def draw
rect = Rectangle.new(
x: 0,
y: 0,
width: Window.width,
height: Window.height,
color: 'silver',
z: 100
)
rect.opacity = 0.5
rect
end
def draw_text(text)
text = text || ''
Text.new(
text,
size: 60,
color: 'red',
z: 100
)
end
def show
@body.add
@text_body.add
end
def hide
@body.remove
@text_body.remove
end
def text=(new_text)
@text_body.text = new_text
center_text
end
private
def center_text
@text_body.x = (Window.width / 2) - (@text_body.width / 2)
@text_body.y = (Window.height / 2) - (@text_body.height / 2)
end
end
# World settings
set title: 'Fifi Breaks Blocks!',
background: 'navy',
width: 640,
height: 960,
resizable: false
# Init
is_game_over = false
is_paused = false
start_time = Time.now
hero = Hero.new
limit = Limit.new
gover_overlay = Overlay.new(text: '- GAME OVER -')
pause_overlay = Overlay.new(text: '- PAUSED -')
score = 0
score_bar = Text.new(
score,
color:
'white',
x: 20, y: Window.height - 25,
z: 20
)
# Add initial blocks
blocks = []
blocks += Raw.new(y: 0).blocks
blocks.each do |block|
block.go_down
end
blocks += Raw.new(y: 0).blocks
# Events
on :key_down do |event|
close if event.key == 'escape'
unless is_game_over
is_paused = ! is_paused if event.key == 'p'
hero.shoot if ! is_paused && event.key == 'space'
end
end
on :key_held do |event|
hero.move(event.key) if event.key == 'left' || event.key == 'right'
end
# Loop
update do
if is_game_over
# gover_overlay.text = "GAME OVER - #{score}"
gover_overlay.show
elsif is_paused
pause_overlay.show
start_time = Time.now
else
pause_overlay.hide
if ((Time.now - start_time) * 1000) > 5000
# New line of blocks
blocks.each do |block|
block.go_down
end
blocks += Raw.new(y: 0).blocks
start_time = Time.now
end
blocks.each do |block|
if block.collision_detected?(limit)
is_game_over = true
hero.body.remove
end
hero.shots.each do |shot|
if block.collision_detected?(shot)
shot.hit(block)
score += 10
score_bar.text = score
hero.shots.delete(shot)
blocks.delete(block) if block.is_detroyed?
break
end
end
end
# Shots
hero.shots.each do |shot|
shot.fly
end
end
end
# Render
show