rubygame 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color/models/base'
21
+
22
+ module Rubygame
23
+ module Color
24
+
25
+ # Represents color in the RGB (Red, Green, Blue) color space.
26
+ class ColorRGB
27
+ include ColorBase
28
+
29
+ attr_reader :r, :g, :b, :a
30
+
31
+ # call-seq:
32
+ # new( [r,g,b,a] ) -> ColorRGB
33
+ # new( [r,g,b] ) -> ColorRGB
34
+ # new( color ) -> ColorRGB
35
+ #
36
+ # Create a new instance from an Array or an existing color
37
+ # (of any type). If the alpha (opacity) component is omitted
38
+ # from the array, full opacity will be used.
39
+ #
40
+ # All color components range from 0.0 to 1.0.
41
+ #
42
+ def initialize( color )
43
+ if color.kind_of?(Array)
44
+ @r, @g, @b, @a = color.collect { |i| i.to_f }
45
+ @a = 1.0 unless @a
46
+ elsif color.respond_to?(:to_rgba_ary)
47
+ @r, @g, @b, @a = color.to_rgba_ary
48
+ end
49
+ end
50
+
51
+ # Converts the color to an RGBA array of integers
52
+ # ranging from 0 to 255, as SDL wants.
53
+ def to_sdl_rgba_ary
54
+ self.to_rgba_ary.collect { |i| (i * 255).to_i }
55
+ end
56
+
57
+ def to_rgba_ary
58
+ return [@r, @g, @b, @a]
59
+ end
60
+
61
+ def to_s
62
+ "#<#{self.class} [#{@r}, #{@g}, #{@b}, #{@a}]>"
63
+ end
64
+ alias :inspect :to_s
65
+
66
+ class << self
67
+ def new_from_rgba( rgba )
68
+ new( rgba )
69
+ end
70
+
71
+ def new_from_sdl_rgba( rgba )
72
+ new_from_rgba( rgba.collect { |i| i / 255.0 } )
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,49 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color'
21
+ require 'rubygame/color/palettes/palette'
22
+ require 'rubygame/color/palettes/x11'
23
+
24
+ module Rubygame
25
+ module Color
26
+
27
+ # :enddoc:
28
+
29
+ # The CSS module contains all the colors in the CSS/HTML palette
30
+ # by symbol name, e.g. :alice_blue, :dark_olive_green, etc.
31
+ #
32
+ # NOTE: The CSS palette is identical to the X11 palette except for
33
+ # four colors: gray, green, maroon, and purple.
34
+ #
35
+ # Differences between CSS and X11 derived from
36
+ # http://en.wikipedia.org/wiki/X11_color_names
37
+ # as accessed on 2007-12-17
38
+ #
39
+ CSS = Palette.new({
40
+ :gray => ColorRGB.new( [0.50196, 0.50196, 0.50196] ),
41
+ :green => ColorRGB.new( [0.00000, 0.50196, 0.00000] ),
42
+ :maroon => ColorRGB.new( [0.50196, 0.00000, 0.00000] ),
43
+ :purple => ColorRGB.new( [0.50196, 0.00000, 0.50196] )
44
+ })
45
+
46
+ CSS.include X11
47
+
48
+ end
49
+ end
@@ -0,0 +1,100 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ class Rubygame::Color::Palette
21
+
22
+ # Create a new Palette with the given name => color pairs.
23
+ def initialize( colors = {} )
24
+ @includes = []
25
+
26
+ @colors = {}
27
+ colors.each_pair do |name, color|
28
+ @colors[sanitize_name(name)] = color
29
+ end
30
+ end
31
+
32
+ # Retrieve a color by name from this palette.
33
+ #
34
+ # The name can be a Symbol or String. See #sanitize_name.
35
+ #
36
+ # If the color cannot be found in this palette, search
37
+ # each of the #included palettes (recursively, depth-first,
38
+ # to a maximum depth of 5 levels).
39
+ #
40
+ # If the color is not found in this palette or any included
41
+ # palettes, raise IndexError.
42
+ #
43
+ def []( name )
44
+ c = lookup( sanitize_name( name ) )
45
+ raise IndexError, "unknown color #{name}" unless c
46
+ return c
47
+ end
48
+
49
+ # Store a color by name in this palette. See #sanitize_name
50
+ def []=( name, color )
51
+ name = sanitize_name( name )
52
+ @colors[name] = color
53
+ end
54
+
55
+ # Include another palette in this one. If a color cannot be
56
+ # found in this palette, the included palette(s) will be searched.
57
+ # See also #uninclude.
58
+ #
59
+ # Has no effect if the palette is already included.
60
+ def include( palette )
61
+ @includes += [palette] unless @includes.include? palette
62
+ end
63
+
64
+ # Remove the other palette from this one, so that it won't be
65
+ # searched for missing colors anymore. Has no effect if the
66
+ # other palette hasn't been #included.
67
+ def uninclude( palette )
68
+ @includes -= [palette]
69
+ end
70
+
71
+ protected
72
+
73
+ # Recursive color lookup
74
+ def lookup( name, max_depth=5 ) # :nodoc:
75
+ return nil if max_depth < 0
76
+
77
+ color = @colors[name]
78
+
79
+ unless color
80
+ @includes.each { |palette|
81
+ c = palette.lookup(name, max_depth-1)
82
+ color = c if c
83
+ }
84
+ end
85
+
86
+ return color
87
+ end
88
+
89
+ private
90
+
91
+ # Takes either a Symbol or a String, and converts it to a
92
+ # lowercase Symbol with spaces converted to underscores.
93
+ #
94
+ # E.g. "Alice Blue" and :ALICE_BLUE both become :alice_blue.
95
+ #
96
+ def sanitize_name( name )
97
+ name.to_s.gsub(' ','_').downcase.intern
98
+ end
99
+
100
+ end
@@ -0,0 +1,177 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require 'rubygame/color'
21
+ require 'rubygame/color/palettes/palette'
22
+
23
+ module Rubygame
24
+ module Color
25
+
26
+ # :enddoc:
27
+
28
+ # The X11 module contains all the colors in the X11 palette
29
+ # by symbol name, e.g. :alice_blue, :dark_olive_green, etc.
30
+ #
31
+ # The list of colors is derived from
32
+ # http://en.wikipedia.org/wiki/X11_color_names
33
+ # as accessed on 2007-12-17.
34
+ X11 = Palette.new({
35
+ :alice_blue => ColorRGB.new( [0.94117, 0.97254, 1.00000] ),
36
+ :antique_white => ColorRGB.new( [0.98039, 0.92156, 0.84313] ),
37
+ :aqua => ColorRGB.new( [0.00000, 1.00000, 1.00000] ),
38
+ :aquamarine => ColorRGB.new( [0.49803, 1.00000, 0.83137] ),
39
+ :azure => ColorRGB.new( [0.94117, 1.00000, 1.00000] ),
40
+ :beige => ColorRGB.new( [0.96078, 0.96078, 0.86274] ),
41
+ :bisque => ColorRGB.new( [1.00000, 0.89411, 0.76862] ),
42
+ :black => ColorRGB.new( [0.00000, 0.00000, 0.00000] ),
43
+ :blanched_almond => ColorRGB.new( [1.00000, 0.92156, 0.80392] ),
44
+ :blue => ColorRGB.new( [0.00000, 0.00000, 1.00000] ),
45
+ :blue_violet => ColorRGB.new( [0.54117, 0.16862, 0.88627] ),
46
+ :brown => ColorRGB.new( [0.64705, 0.16470, 0.16470] ),
47
+ :burly_wood => ColorRGB.new( [0.87058, 0.72156, 0.52941] ),
48
+ :cadet_blue => ColorRGB.new( [0.37254, 0.61960, 0.62745] ),
49
+ :chartreuse => ColorRGB.new( [0.49803, 1.00000, 0.00000] ),
50
+ :chocolate => ColorRGB.new( [0.82352, 0.41176, 0.11764] ),
51
+ :coral => ColorRGB.new( [1.00000, 0.49803, 0.31372] ),
52
+ :cornflower_blue => ColorRGB.new( [0.39215, 0.58431, 0.92941] ),
53
+ :cornsilk => ColorRGB.new( [1.00000, 0.97254, 0.86274] ),
54
+ :crimson => ColorRGB.new( [0.86274, 0.07843, 0.23529] ),
55
+ :cyan => ColorRGB.new( [0.00000, 1.00000, 1.00000] ),
56
+ :dark_blue => ColorRGB.new( [0.00000, 0.00000, 0.54509] ),
57
+ :dark_cyan => ColorRGB.new( [0.00000, 0.54509, 0.54509] ),
58
+ :dark_goldenrod => ColorRGB.new( [0.72156, 0.52549, 0.04313] ),
59
+ :dark_gray => ColorRGB.new( [0.66274, 0.66274, 0.66274] ),
60
+ :dark_green => ColorRGB.new( [0.00000, 0.39215, 0.00000] ),
61
+ :dark_khaki => ColorRGB.new( [0.74117, 0.71764, 0.41960] ),
62
+ :dark_magenta => ColorRGB.new( [0.54509, 0.00000, 0.54509] ),
63
+ :dark_olive_green => ColorRGB.new( [0.33333, 0.41960, 0.18431] ),
64
+ :dark_orange => ColorRGB.new( [1.00000, 0.54901, 0.00000] ),
65
+ :dark_orchid => ColorRGB.new( [0.60000, 0.19607, 0.80000] ),
66
+ :dark_red => ColorRGB.new( [0.54509, 0.00000, 0.00000] ),
67
+ :dark_salmon => ColorRGB.new( [0.91372, 0.58823, 0.47843] ),
68
+ :dark_sea_green => ColorRGB.new( [0.56078, 0.73725, 0.56078] ),
69
+ :dark_slate_blue => ColorRGB.new( [0.28235, 0.23921, 0.54509] ),
70
+ :dark_slate_gray => ColorRGB.new( [0.18431, 0.30980, 0.30980] ),
71
+ :dark_turquoise => ColorRGB.new( [0.00000, 0.80784, 0.81960] ),
72
+ :dark_violet => ColorRGB.new( [0.58039, 0.00000, 0.82745] ),
73
+ :deep_pink => ColorRGB.new( [1.00000, 0.07843, 0.57647] ),
74
+ :deep_sky_blue => ColorRGB.new( [0.00000, 0.74901, 1.00000] ),
75
+ :dim_gray => ColorRGB.new( [0.41176, 0.41176, 0.41176] ),
76
+ :dodger_blue => ColorRGB.new( [0.11764, 0.56470, 1.00000] ),
77
+ :fire_brick => ColorRGB.new( [0.69803, 0.13333, 0.13333] ),
78
+ :floral_white => ColorRGB.new( [1.00000, 0.98039, 0.94117] ),
79
+ :forest_green => ColorRGB.new( [0.13333, 0.54509, 0.13333] ),
80
+ :fuchsia => ColorRGB.new( [1.00000, 0.00000, 1.00000] ),
81
+ :gainsboro => ColorRGB.new( [0.86274, 0.86274, 0.86274] ),
82
+ :ghost_white => ColorRGB.new( [0.97254, 0.97254, 1.00000] ),
83
+ :gold => ColorRGB.new( [1.00000, 0.84313, 0.00000] ),
84
+ :goldenrod => ColorRGB.new( [0.85490, 0.64705, 0.12549] ),
85
+ :gray => ColorRGB.new( [0.50196, 0.50196, 0.50196] ),
86
+ :green => ColorRGB.new( [0.00000, 0.50196, 0.00000] ),
87
+ :green_yellow => ColorRGB.new( [0.67843, 1.00000, 0.18431] ),
88
+ :honeydew => ColorRGB.new( [0.94117, 1.00000, 0.94117] ),
89
+ :hot_pink => ColorRGB.new( [1.00000, 0.41176, 0.70588] ),
90
+ :indian_red => ColorRGB.new( [0.80392, 0.36078, 0.36078] ),
91
+ :indigo => ColorRGB.new( [0.29411, 0.00000, 0.50980] ),
92
+ :ivory => ColorRGB.new( [1.00000, 1.00000, 0.94117] ),
93
+ :khaki => ColorRGB.new( [0.94117, 0.90196, 0.54901] ),
94
+ :lavender => ColorRGB.new( [0.90196, 0.90196, 0.98039] ),
95
+ :lavender_blush => ColorRGB.new( [1.00000, 0.94117, 0.96078] ),
96
+ :lawn_green => ColorRGB.new( [0.48627, 0.98823, 0.00000] ),
97
+ :lemon_chiffon => ColorRGB.new( [1.00000, 0.98039, 0.80392] ),
98
+ :light_blue => ColorRGB.new( [0.67843, 0.84705, 0.90196] ),
99
+ :light_coral => ColorRGB.new( [0.94117, 0.50196, 0.50196] ),
100
+ :light_cyan => ColorRGB.new( [0.87843, 1.00000, 1.00000] ),
101
+ :light_goldenrod_yellow => ColorRGB.new( [0.98039, 0.98039, 0.82352] ),
102
+ :light_green => ColorRGB.new( [0.56470, 0.93333, 0.56470] ),
103
+ :light_grey => ColorRGB.new( [0.82745, 0.82745, 0.82745] ),
104
+ :light_pink => ColorRGB.new( [1.00000, 0.71372, 0.75686] ),
105
+ :light_salmon => ColorRGB.new( [1.00000, 0.62745, 0.47843] ),
106
+ :light_sea_green => ColorRGB.new( [0.12549, 0.69803, 0.66666] ),
107
+ :light_sky_blue => ColorRGB.new( [0.52941, 0.80784, 0.98039] ),
108
+ :light_slate_gray => ColorRGB.new( [0.46666, 0.53333, 0.60000] ),
109
+ :light_steel_blue => ColorRGB.new( [0.69019, 0.76862, 0.87058] ),
110
+ :light_yellow => ColorRGB.new( [1.00000, 1.00000, 0.87843] ),
111
+ :lime => ColorRGB.new( [0.00000, 1.00000, 0.00000] ),
112
+ :lime_green => ColorRGB.new( [0.19607, 0.80392, 0.19607] ),
113
+ :linen => ColorRGB.new( [0.98039, 0.94117, 0.90196] ),
114
+ :magenta => ColorRGB.new( [1.00000, 0.00000, 1.00000] ),
115
+ :maroon => ColorRGB.new( [0.50196, 0.00000, 0.00000] ),
116
+ :medium_aquamarine => ColorRGB.new( [0.40000, 0.80392, 0.66666] ),
117
+ :medium_blue => ColorRGB.new( [0.00000, 0.00000, 0.80392] ),
118
+ :medium_orchid => ColorRGB.new( [0.72941, 0.33333, 0.82745] ),
119
+ :medium_purple => ColorRGB.new( [0.57647, 0.43921, 0.85882] ),
120
+ :medium_sea_green => ColorRGB.new( [0.23529, 0.70196, 0.44313] ),
121
+ :medium_slate_blue => ColorRGB.new( [0.48235, 0.40784, 0.93333] ),
122
+ :medium_spring_green => ColorRGB.new( [0.00000, 0.98039, 0.60392] ),
123
+ :medium_turquoise => ColorRGB.new( [0.28235, 0.81960, 0.80000] ),
124
+ :medium_violet_red => ColorRGB.new( [0.78039, 0.08235, 0.52156] ),
125
+ :midnight_blue => ColorRGB.new( [0.09803, 0.09803, 0.43921] ),
126
+ :mint_cream => ColorRGB.new( [0.96078, 1.00000, 0.98039] ),
127
+ :misty_rose => ColorRGB.new( [1.00000, 0.89411, 0.88235] ),
128
+ :moccasin => ColorRGB.new( [1.00000, 0.89411, 0.70980] ),
129
+ :navajo_white => ColorRGB.new( [1.00000, 0.87058, 0.67843] ),
130
+ :navy => ColorRGB.new( [0.00000, 0.00000, 0.50196] ),
131
+ :old_lace => ColorRGB.new( [0.99215, 0.96078, 0.90196] ),
132
+ :olive => ColorRGB.new( [0.50196, 0.50196, 0.00000] ),
133
+ :olive_drab => ColorRGB.new( [0.41960, 0.55686, 0.13725] ),
134
+ :orange => ColorRGB.new( [1.00000, 0.64705, 0.00000] ),
135
+ :orange_red => ColorRGB.new( [1.00000, 0.27058, 0.00000] ),
136
+ :orchid => ColorRGB.new( [0.85490, 0.43921, 0.83921] ),
137
+ :pale_goldenrod => ColorRGB.new( [0.93333, 0.90980, 0.66666] ),
138
+ :pale_green => ColorRGB.new( [0.59607, 0.98431, 0.59607] ),
139
+ :pale_turquoise => ColorRGB.new( [0.68627, 0.93333, 0.93333] ),
140
+ :pale_violet_red => ColorRGB.new( [0.85882, 0.43921, 0.57647] ),
141
+ :papaya_whip => ColorRGB.new( [1.00000, 0.93725, 0.83529] ),
142
+ :peach_puff => ColorRGB.new( [1.00000, 0.85490, 0.72549] ),
143
+ :peru => ColorRGB.new( [0.80392, 0.52156, 0.24705] ),
144
+ :pink => ColorRGB.new( [1.00000, 0.75294, 0.79607] ),
145
+ :plum => ColorRGB.new( [0.86666, 0.62745, 0.86666] ),
146
+ :powder_blue => ColorRGB.new( [0.69019, 0.87843, 0.90196] ),
147
+ :purple => ColorRGB.new( [0.50196, 0.00000, 0.50196] ),
148
+ :red => ColorRGB.new( [1.00000, 0.00000, 0.00000] ),
149
+ :rosy_brown => ColorRGB.new( [0.73725, 0.56078, 0.56078] ),
150
+ :royal_blue => ColorRGB.new( [0.25490, 0.41176, 0.88235] ),
151
+ :saddle_brown => ColorRGB.new( [0.54509, 0.27058, 0.07450] ),
152
+ :salmon => ColorRGB.new( [0.98039, 0.50196, 0.44705] ),
153
+ :sandy_brown => ColorRGB.new( [0.95686, 0.64313, 0.37647] ),
154
+ :sea_green => ColorRGB.new( [0.18039, 0.54509, 0.34117] ),
155
+ :seashell => ColorRGB.new( [1.00000, 0.96078, 0.93333] ),
156
+ :sienna => ColorRGB.new( [0.62745, 0.32156, 0.17647] ),
157
+ :silver => ColorRGB.new( [0.75294, 0.75294, 0.75294] ),
158
+ :sky_blue => ColorRGB.new( [0.52941, 0.80784, 0.92156] ),
159
+ :slate_blue => ColorRGB.new( [0.41568, 0.35294, 0.80392] ),
160
+ :slate_gray => ColorRGB.new( [0.43921, 0.50196, 0.56470] ),
161
+ :snow => ColorRGB.new( [1.00000, 0.98039, 0.98039] ),
162
+ :spring_green => ColorRGB.new( [0.00000, 1.00000, 0.49803] ),
163
+ :steel_blue => ColorRGB.new( [0.27450, 0.50980, 0.70588] ),
164
+ :tan => ColorRGB.new( [0.82352, 0.70588, 0.54901] ),
165
+ :teal => ColorRGB.new( [0.00000, 0.50196, 0.50196] ),
166
+ :thistle => ColorRGB.new( [0.84705, 0.74901, 0.84705] ),
167
+ :tomato => ColorRGB.new( [1.00000, 0.38823, 0.27843] ),
168
+ :turquoise => ColorRGB.new( [0.25098, 0.87843, 0.81568] ),
169
+ :violet => ColorRGB.new( [0.93333, 0.50980, 0.93333] ),
170
+ :wheat => ColorRGB.new( [0.96078, 0.87058, 0.70196] ),
171
+ :white => ColorRGB.new( [1.00000, 1.00000, 1.00000] ),
172
+ :white_smoke => ColorRGB.new( [0.96078, 0.96078, 0.96078] ),
173
+ :yellow => ColorRGB.new( [1.00000, 1.00000, 0.00000] ),
174
+ :yellow_green => ColorRGB.new( [0.60392, 0.80392, 0.19607] )
175
+ })
176
+ end
177
+ end
@@ -498,10 +498,8 @@ class Rect < Array
498
498
  def collide_rect?(rect)
499
499
  nself = self.normalize
500
500
  rect = Rect.new_from_object(rect).normalize!
501
- return ((( (rect.l)..(rect.r) ).include?(nself.l) or\
502
- ((nself.l)..(nself.r)).include?( rect.l)) and\
503
- (( (rect.t)..(rect.b) ).include?(nself.t) or\
504
- ((nself.t)..(nself.b)).include?( rect.t)))
501
+ return ((nself.l >= rect.l && nself.l <= rect.r) or (rect.l >= nself.l && rect.l <= nself.r)) &&
502
+ ((nself.t >= rect.t && nself.t <= rect.b) or (rect.t >= nself.t && rect.t <= nself.b))
505
503
  end
506
504
 
507
505
  # True if the given Rect is totally within the caller. Borders may
@@ -271,26 +271,60 @@ module Rubygame
271
271
  sprite.collide_group(self)
272
272
  end
273
273
 
274
- # call-seq: collide_group(group, klla=false, killb=false) -> Hash
274
+ # call-seq:
275
+ # collide_group(group, &block) -> Hash
276
+ # collide_group(group, killa=false, killb=false) -> Hash # deprecated
275
277
  #
276
278
  # Check collision between each member of the calling Group and each
277
279
  # member of +group+. Returns a Hash table with each member of the calling
278
280
  # Group as a key, and as a value an Array of all members of +group+ that
279
281
  # it collided with.
280
- def collide_group(group, killa=false, killb=false)
282
+ #
283
+ # If a block is given, that block is executed for every pair of colliding
284
+ # sprites. For example, if a1 collides with b1 and b2, the block will
285
+ # be called twice: once with [ a1, b1 ] and once with [ a1, b2 ].
286
+ #
287
+ # Example:
288
+ #
289
+ # # 'kills' both sprites when they collide
290
+ #
291
+ # groupA,collide_group(groupB) do |a, b|
292
+ # a.kill
293
+ # b.kill
294
+ # end
295
+ #
296
+ # *NOTE*: +killa+ and +killb+ are deprecated and will be removed in the future.
297
+ # It is highly recommended that you use the block argument instead.
298
+ #
299
+ # *IMPORTANT*: +killa+ and +killb+ will be ignored if a block is given!
300
+ #
301
+ # If +killa+ is true and a sprite in group A collides with a sprite in group B,
302
+ # the sprite in group A will have its #kill method called; the same goes for
303
+ # +killb+ and group B.
304
+ #
305
+ def collide_group(group, killa=false, killb=false, &block)
281
306
  sprites = {}
282
307
  self.each { |sprite|
283
308
  col = sprite.collide_group(group)
284
309
  sprites[sprite] = col if col.length > 0
285
310
  }
286
- if killa
287
- sprites.each_key { |sprite| sprite.kill }
288
- end
289
- if killb
290
- sprites.each_value do |array|
291
- array.each { |sprite| sprite.kill }
311
+
312
+ if block_given?
313
+ sprites.each_pair do |a, bs|
314
+ bs.each { |b| yield(a, b) }
315
+ end
316
+ else
317
+ # killa and killb only work if no block is given
318
+ if killa
319
+ sprites.each_key { |sprite| sprite.kill }
320
+ end
321
+ if killb
322
+ sprites.each_value do |array|
323
+ array.each { |sprite| sprite.kill }
324
+ end
292
325
  end
293
326
  end
327
+
294
328
  return sprites
295
329
  end
296
330