Skip to content

Instantly share code, notes, and snippets.

@texel
Created March 12, 2010 07:29
Show Gist options
  • Save texel/330123 to your computer and use it in GitHub Desktop.
Save texel/330123 to your computer and use it in GitHub Desktop.
# Balls!
class Ball
attr_accessor :x_pos, :y_pos, :x_speed, :y_speed
def initialize(x_pos, y_pos, x_speed, y_speed)
self.x_pos = x_pos
self.y_pos = y_pos
self.x_speed = x_speed
self.y_speed = y_speed
end
end
def setup
size 800, 600
@line_count = 5
@line_speed = 10
@balls = []
@line_count.times do
@balls << init_ball
end
end
def range_rand(min, max)
min + rand(max - min)
end
def init_ball
x_pos = width / 2 + range_rand(-width / 3, width / 3)
y_pos = height / 2 + range_rand(-height / 3, height / 3)
x_speed = range_rand(1, @line_speed)
y_speed = range_rand(-@line_speed, @line_speed)
Ball.new(x_pos, y_pos, x_speed, y_speed)
end
def draw
fill 0, 10
rect_mode CORNER
rect 0, 0, width, height
stroke 255
@balls.each do |ball|
line ball.x_pos, ball.y_pos, mouse_x, mouse_y
# Update Position
ball.x_pos += ball.x_speed
ball.y_pos += ball.y_speed
if ball.x_pos >= width || ball.x_pos < 0
ball.x_speed *= -1
end
if ball.y_pos >= height || ball.y_pos < 0
ball.y_speed *= -1
end
end
end
def add_ball!
@balls << init_ball
end
def remove_ball!
@balls.pop
end
def key_pressed
case key_code
when UP
add_ball!
when DOWN
remove_ball!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment