Skip to content

Instantly share code, notes, and snippets.

@jschementi
Created December 14, 2009 07:43
Show Gist options
  • Save jschementi/255885 to your computer and use it in GitHub Desktop.
Save jschementi/255885 to your computer and use it in GitHub Desktop.
class Bouncer
def initialize xvelocity, yvelocity, canvas
@xvelocity = xvelocity
@yvelocity = yvelocity
@canvas = canvas
end
def update target
if (Canvas.get_left(target) + @xvelocity) >= (@canvas.actual_width - target.width) or (Canvas.get_left(target) + @xvelocity) <= 0
@xvelocity = -@xvelocity
end
if (Canvas.get_top(target) + @yvelocity) >= (@canvas.actual_height - target.height) or (Canvas.get_top(target) + @yvelocity) <= 0
@yvelocity = -@yvelocity
end
Canvas.set_top target, Canvas.get_top(target) + @yvelocity
Canvas.set_left target, Canvas.get_left(target) + @xvelocity
end
end
def each_object target
Bouncer.new rand(10) - 5, rand(10) - 5, canvas
end
# create a ton of random sized, colored, and stroked rectangles
include System::Windows::Shapes
include System::Windows::Media
include System::Windows::Controls
canvas.children.clear
colors = %W(red orange yellow lime blue purple violet)
brushes = colors.map{|c| SolidColorBrush.new(Colors.send(c)) }
canvas_dim = lambda{ [canvas.actual_width, canvas.actual_height] }
500.times do
size = rand(canvas_dim[].max / 10)
shape = Rectangle.new
shape.width, shape.height = size, size
shape.fill = brushes[rand brushes.size]
shape.stroke = brushes[rand brushes.size]
shape.stroke_thickness = rand(canvas_dim[].max / 70) + 4
canvas.children.add shape
Canvas.set_left shape, rand(canvas_dim[].first - size)
Canvas.set_top shape, rand(canvas_dim[].last - size)
end
# Load the DLLs that allow you work with graphics
require 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
require 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
# Get the window and canvas from the host application
window = System::Windows::Application.current.main_window
canvas = window.find_name('_canvas')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment