Skip to content

Instantly share code, notes, and snippets.

@benjaminws
Created January 22, 2012 21:24
Show Gist options
  • Save benjaminws/1658894 to your computer and use it in GitHub Desktop.
Save benjaminws/1658894 to your computer and use it in GitHub Desktop.
Learning ruby with shoes!
# KBounce clone
# Setup a basic canvas, 600x400, not resizable
Shoes.app :width => 600, :height => 400, :resizable => false do
background black
# Put our vertical and horizontal baracades in a list
@linesv = []
@linesh = []
# Create our ball
@ball = oval(10, 10, 10, fill: white)
# Default positioning
x_direction = 10
y_direction = 10
# Animate at 100 FPS
@anim = animate 100 do
# Check the bounds of our canvas and our lines, react to them, by "bouncing"
if @ball.left-5 >= 600 or @ball.left+5 < 0 or encounters_vertical_baracade(@linesv, @ball)
x_direction = -1 * x_direction
end
if @ball.top-5 >= 400 or @ball.top+5 < 5 or encounters_horizontal_baracade(@linesh, @ball)
y_direction = -1 * y_direction
end
@ball.move(@ball.left + x_direction, @ball.top + y_direction)
end
# Register clicks, draw our rectangles
click do |button,x,y|
@linesv << rect(x, 0, 10, 400, fill: red) if button == 1
@linesh << rect(0, y, 600, 10, fill: red) if button != 1
end
end
# Determine if we hit a horizontal baracade
def encounters_horizontal_baracade lines, ball
lines.each do |line|
return true if ball.top >= line.top and ball.top <= line.top+10
end
false
end
# Determine if we hit a vertical baracade
def encounters_vertical_baracade lines, ball
lines.each do |line|
return true if ball.left >= line.left and ball.left <= line.left+10
end
false
end
@dnoyes
Copy link

dnoyes commented Jan 22, 2012

haha... you said "ball"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment