-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
104 lines (87 loc) · 1.88 KB
/
main.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
require 'ruby2d'
require './lib/hero.rb'
require './lib/raw.rb'
require './lib/limit.rb'
require './lib/overlay.rb'
# 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