require 'tk' $w, $h = 400, 400 $x0, $y0 = 50, 20 $n, $dim = 4, 75 def click(c, e) puts "#{e.x} #{e.y}" end root = TkRoot.new(:title => 'Sliding Tile Puzzle') canvas = TkCanvas.new(root, :width => $w, :height => $h, :background => 'black') canvas.pack k = 0 while k < $n j = 0 while j < $n x, y = $x0 + $dim*j, $y0 + $dim*k num = k*$n + j + 1 tile = TkcRectangle.new(canvas, x, y, x+$dim, y + $dim, :outline => 'white') text = TkcText.new(canvas, x+$dim/2, y+$dim/2, :text => "#{num}", :fill =>'white') j += 1 end k += 1 end tile.fill = 'white' text.text = "" root.bind('1') { |event| click(canvas,event) } root.bind('q') { exit } Tk.mainloop