forked from ongaeshi/ofruby-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_bound.rb
69 lines (61 loc) · 1.02 KB
/
random_bound.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
X = 50
Y = 50
SX = 220
SY = 380
R = 10
def setup
# srand 0
@balls = []
num = rand(100) + 1
# num = 5
1.step(num, 1) do
@balls.push Ball.new rand(300), rand(300), rand(20), rand(20)
end
end
def update
@balls.each { |ball| ball.update }
end
def draw
set_color_hex 0xecf1f6
set_fill
set_line_width 2
rect X, Y, SX, SY
@balls.each { |ball| ball.draw }
set_color 0, 0, 0
text "fps: #{frame_rate}", 10, 25
text "balls: #{@balls.length}", 10, 40
end
class Ball
def initialize(x, y, sx, sy)
@x = x
@sx = sx
@y = y
@sy = sy
@color = Color.new rand(255), rand(255), rand(255)
end
def update
@x += @sx
if @x > X + SX - R
@sx *= -1
@x = X + SX - R
end
if @x < X + R
@sx *= -1
@x = X + R
end
@y += @sy
if @y > Y + SY - R
@sy *= -1
@y = Y + SY - R
end
if @y < Y + R
@sy *= -1
@y = Y + R
end
end
def draw
set_color @color
set_fill
ellipse @x, @y, 20, 20
end
end