Created
August 1, 2015 10:10
-
-
Save monkstone/bf031ae16c9df7f09281 to your computer and use it in GitHub Desktop.
Live Coding Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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