-
Notifications
You must be signed in to change notification settings - Fork 4
/
pong.rb
139 lines (116 loc) · 1.94 KB
/
pong.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
require 'ruby2d'
require './lib/ball'
require './lib/paddle'
require './lib/match'
##
# Window & FPS
##
set title: 'Pong',
background: 'black',
with: 640,
height: 480,
resizable: false
fps_display = Text.new(get(:fps).to_i, x: 315, y: 463, size: 12)
##
# Table
##
16.times do |i|
Rectangle.new(
width: 5,
height: 15,
x: get(:width) / 2 - 2.5,
y: i * 30,
color: 'gray'
)
end
##
# Ball
##
ball = Ball.new(
x: get(:width) / 2 - 5,
y: get(:height) / 2 - 5 / 2,
size: 10,
speed: 5
)
##
# Paddles
##
pad1 = Paddle.new(
x: 2,
y: get(:height) / 2 - 60 / 2,
width: 10,
height: 60,
speed: 7,
constraints: { y: { min: 0, max: 480 } }
)
pad2 = Paddle.new(
x: 640 - (2 + 10),
y: get(:height) / 2 - 60 / 2,
width: 10,
height: 60,
speed: 7,
constraints: { y: { min: 0, max: 480 } }
)
##
# Score
##
score_display = {
left: Text.new(
0,
x: get(:width) / 2 - 100,
y: 5,
font: 'assets/PressStart2P.ttf',
color: 'gray',
size: 40
),
right: Text.new(
0,
x: get(:width) / 2 + 60,
y: 5,
font: 'assets/PressStart2P.ttf',
color: 'gray', size: 40
)
}
##
# 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|
pad1.move(event, up: 'w', down: 's')
pad2.move(event, up: 'o', down: 'k')
end
# Game pause
on :key_down do |event|
if event.key == 'space'
match.paused = !match.paused
pause_display.opacity *= -1
end
end
update do
fps_display.text = get(:fps).to_i
if match.paused?
next
elsif match.wait_to_start?
match.check_wait!(get(:frames))
else
ball.move(window: get(:window), pads: [pad1, pad2])
if ball.scored?
match.update_score!(ball, score_display)
match.restart!(get(:window), ball)
end
end
end
show