Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created August 1, 2015 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/bf031ae16c9df7f09281 to your computer and use it in GitHub Desktop.
Save monkstone/bf031ae16c9df7f09281 to your computer and use it in GitHub Desktop.
Live Coding Examples
# bounce.rb
class Bounce < Processing::App
BLUE = '#0000FF'
GREEN = '#00FF00'
RED = '#FF0000'
WHITE = '#FFFFFF'
BALL_SPEED = 5
BALL_SIZE = 32
attr_reader :backcolor, :fill_color, :ball_size, :ball_speed
attr_reader :boundary, :x_pos
def setup
size 300, 300
frame_rate 30
back_color(WHITE)
ball_color(BLUE)
@x_pos = BALL_SIZE / 2.0
@ball_speed = BALL_SPEED
@ball_size = BALL_SIZE
@boundary = Boundary.new(BALL_SIZE / 2.0, width - BALL_SIZE / 2.0)
no_stroke
smooth 4
end
def draw
background backcolor
fill fill_color
@x_pos += ball_speed
@ball_speed *= -1 unless boundary.include? x_pos
ellipse x_pos, 100, ball_size, ball_size
end
def ball_color(str)
@fill_color = color(str)
end
def back_color(str)
@backcolor = color(str)
end
Boundary = Struct.new(:lower, :upper) do
def include?(x)
(lower...upper).cover? x
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment