-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math-Bugs.rb
75 lines (63 loc) · 1.6 KB
/
Math-Bugs.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
Shoes.app do
# Worms!
@worms = []
@worms << [oval(200, 100, 300, 40, fill: lightcoral), 2,2, para("")]
@worms << [oval(200, 200, 300, 40, fill: magenta), 3, 3, para("")]
@worms << [oval(200, 300, 300, 40, fill: navy), 4, 4, para("")]
@worms.each_with_index do |worm, index|
p = worm.last
p.text = "#{worm[-2]}+#{worm[-3]}="
p.left = 300
p.top = (100 * (index + 1) + 10)
p.stroke = white
p.hide
end
# Finish Line!
3.times do |i|
rect 0, 90 * (2 * i), 90, 90, fill: red
rect 0, 90 * (2 * i + 1), 90, 90 , fill: darkblue
end
# Helpers
def reset
@worms.each do |worm|
worm.last.text = worm.last.text[0..-2]
end
set_current_worm(0)
end
def reset_problem(index)
i = rand(9)
j = rand(10-i)
worm = @worms[index]
worm[-2] = i
worm[-3] = j
worm.last.text = "#{worm[-2]}+#{worm[-3]}="
end
def set_current_worm(index)
index = index % @worms.length
@worms.each do |worm|
worm.last.hide
end
if worm = @worms[index]
worm.last.toggle
end
@current_worm = index
end
set_current_worm(0)
keypress do |key|
worm = @worms[@current_worm]
if worm
if key.is_a?(String)
worm.last.text = worm.last.text.gsub(/=.*/, "=#{key}")
end
if key == (worm[-2] + worm[-3]).to_s
worm.each do |element|
element.left -= 20 unless element.is_a?(Integer)
end
else
Thread.new { `afplay ~/.hacketyhack/beep.mp3` }
end
reset_problem(@current_worm)
set_current_worm(@current_worm + 1)
end
end
end