Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Saturday, 13 July 2013

Yet Another 10 PRINT sketch in ruby-processing

Added some tweaks for smoother operation ie key_pressed/key_released to sequence action. See at open processing here.
# 
# Inspired by:
# 10 PRINT CHR$(205.5+RND(1)) : GOTO 10
# 
# Sketch by:
# Jacob Joaquin
# jacobjoaquin@gmail.com
# twitter @jacobjoaquin
# 

ODDS = 0.618

attr_reader :num_tiles_x, :num_tiles_y, :tile_size, :c1, :c2

def setup
  size 800, 800
  @tile_size = 50
  @num_tiles_x = width / tile_size
  @num_tiles_y = height / tile_size
  @c2 = color(235, 70, 47)
  @c1 = color(255)
  background c2
  fill c1
  no_stroke
  no_loop
end

def draw
  do_maze
end

def key_pressed
  case key
  when 'q'
    resize_tile 2
  when 'w'
    resize_tile 0.5
  when 's'
    save "maze_sketch.png"
  end
end

def key_released
  case key
  when 'q', 'w'
    background c2
    redraw
  end
end

def resize_tile v
  @tile_size *= v
  @tile_size *= v
  @num_tiles_x = width / tile_size
  @num_tiles_y = height / tile_size
end

def do_maze
  (-1 .. num_tiles_x).each do |i|
    (-1 .. num_tiles_y).each do |j|
      x1, y1 = i * tile_size, j * tile_size
      x2, y2 = x1, y1 + tile_size
      d = height / (tile_size / 2)
      n = y1 / d
      x = y2 / d

      if (rand > ODDS)
        x1 += tile_size
        quad(x1, y1 - n, x1 + n, y1, x2, y2 + x, x2 - x, y2)
      else
        x2 += tile_size
        quad(x1 - n, y1, x1, y1 - n, x2 + x, y2, x2, y2 + x)
      end
    end
  end
end

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2