-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick.rb
121 lines (99 loc) · 1.75 KB
/
brick.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
require 'ruby2d'
require './lib/ball'
require './lib/paddle'
require './lib/match'
##
# Window & FPS
##
set title: 'AlleyWay',
background: 'black',
with: 640,
height: 480,
resizable: false
fps_display = Text.new(get(:fps).to_i, x: 315, y: 463, size: 12)
##
# Pad
##
pad = Paddle.new(
x: get(:width) / 2 - 60 / 2,
y: get(:height) - 15,
width: 60,
height: 10,
speed: 7,
constraints: { x: { min: 0, max: get(:width) - 50 } }
)
##
# Bricks
##
bricks = []
4.times do |row|
10.times do |column|
bricks << Paddle.new(
x: get(:width) / 10 * column,
y: 15 * row,
width: 60,
height: 10,
speed: 0,
constraints: {}
)
end
end
##
# Ball
##
ball = Ball.new(
x: pad.x + (pad.width / 2) - 5,
y: 450,
size: 10,
speed: 5
)
##
# Pause
##
pause_display = Text.new(
'PRESS SPACE',
x: get(:width) / 2 - 110,
y: get(:height) / 2 - 20,
font: 'assets/PressStart2P.ttf',
color: 'gray',
size: 20,
opacity: 1
)
##
# Main
##
match = Match.new
# Paddle movement
on :key_held do |event|
pad.move(event, left: 'a', right: 'd')
if match.paused?
ball.x = pad.x + (pad.width / 2) - (ball.width / 2)
end
end
# Game pause
on :key_down do |event|
if event.key == 'space'
match.paused = false
pause_display.opacity = -1
end
end
update do
fps_display.text = get(:fps).to_i
if match.paused?
next
else
ball.move(window: get(:window), pad: pad, bricks: bricks)
if ball.scored?
ball.scored.each do |brick|
index = bricks.index(brick)
bricks[index].width = 0
bricks.delete(brick)
end
elsif ball.failed?
match.paused = !match.paused
pause_display.opacity = 1
match.restart!(get(:window), ball, pad)
end
end
end
show