11# By inheriting from LibraryProxy draw loop is by reflection sketch draw loop
22class DeadGrid < LibraryProxy
3- attr_reader :app , :block_size
4- attr_accessor :grid , :width , :height
3+ include Processing :: Proxy
4+ attr_reader :app , :block_size , :grid , :width , :height
55
66 def initialize ( app )
77 @app = app
88 @block_size = app . block_size
99 @width = app . width / block_size - 1
1010 @height = app . height / block_size - 1
11- @grid = Array . new
12- 0 . upto ( width ) do |i |
13- grid [ i ] = Array . new
14- 0 . upto ( height ) { |j | @grid [ i ] [ j ] = 0 }
15- end
11+ init_dead_grid ( width , height )
1612 end
1713
1814 def to_s
19- string = ""
20- string << "\n DeadGrid\n "
21- string << "-" * width << "\n "
22- 0 . upto ( width ) do |i |
23- 0 . upto ( height ) { |j | string << grid [ j ] [ i ] . to_s }
24- string << "\n "
15+ "\n DeadGrid\n " . tap do |string |
16+ string << "-" * width << "\n "
17+ grid . each do |row |
18+ string << row . inspect << "\n "
19+ end
2520 end
26- string
2721 end
2822
29- def draw
30- app . stroke ( 255 , 255 , 255 , 125 )
31- app . stroke_weight ( 1 )
32- 0 . upto ( width ) do |i |
33- 0 . upto ( height ) do |j |
34- x = i * block_size
35- y = j * block_size
23+ def init_dead_grid ( width , height )
24+ @grid = ( 0 ..width ) . map do |row |
25+ ( 0 ..height ) . map { 0 }
26+ end
27+ end
3628
37- if ( @grid [ i ] [ j ] != 0 )
38- if ( @grid [ i ] [ j ] == 1 )
39- app . fill ( 255 , 0 , 0 )
40- elsif ( @grid [ i ] [ j ] == 2 )
41- app . fill ( 120 , 200 , 50 )
42- end
43- app . rect ( x , y , block_size , block_size )
29+ def draw
30+ stroke ( color ( 255 , 255 , 255 , 125 ) )
31+ stroke_weight ( 1 )
32+ app . grid ( width , height ) do |row , column | # jruby_art convenience method grid
33+ x = row * block_size
34+ y = column * block_size
35+ if ( @grid [ row ] [ column ] != 0 )
36+ if ( @grid [ row ] [ column ] == 1 )
37+ fill ( color ( 255 , 0 , 0 ) )
38+ elsif ( @grid [ row ] [ column ] == 2 )
39+ fill ( color ( 120 , 200 , 50 ) )
4440 end
41+ rect ( x , y , block_size , block_size )
4542 end
4643 end
4744 end
4845
4946 def randomize
50- 0 . upto ( width ) do |i |
51- 0 . upto ( height ) do |j |
52- @grid [ i ] [ j ] = rand ( 0 ..1 )
53- end
47+ app . grid ( width , height ) do |row , column | # jruby_art convenience method grid
48+ grid [ row ] [ column ] = rand ( 0 ..1 )
5449 end
5550 end
5651
@@ -86,6 +81,7 @@ def there?(x, y)
8681
8782# By inheriting from LibraryProxy draw loop is by reflection sketch draw loop
8883class Block < LibraryProxy
84+ include Processing ::Proxy
8985 attr_reader :app , :block_size
9086 attr_accessor :x , :y , :width , :height , :direction
9187
@@ -100,8 +96,8 @@ def initialize(app)
10096 end
10197
10298 def draw
103- app . fill ( 55 , 0 , 255 )
104- app . rect ( @x * block_size , @y * block_size , @width , @height )
99+ fill ( color ( 55 , 0 , 255 ) )
100+ rect ( @x * block_size , @y * block_size , @width , @height )
105101 end
106102end
107103
0 commit comments