require 'tk' require 'tkextlib/tile' root = TkRoot.new {title "Test showing Dice"} $content = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid( :sticky => 'nsew') TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1 $dice_values = Hash.new $total_score = 0 $hand_score = 0 class Bone attr_reader :position attr_accessor :value, :scored, :image, :button def initialize(name) @value = 1 @position = name @scored = false @image = TkPhotoImage.new(:file => "dice#{@value}.gif") @button = Tk::Tile::Label.new($content){image @image}.grid(:column => @position, :row => 1, :sticky =>'we') end def roll @scored = false value = rand(6) +1 self.load(value) end def scored? @scored end def load(num) @value = num if num == 3 @scored = true end @image =TkPhotoImage.new(:file => "dice#{@value}.gif") @button = Tk::Tile::Label.new($content){image @image}.grid(:column => @position, :row => 1, :sticky =>'we') end end roll_all = Tk::Tile::Button.new($content) {text 'Roll All!'; command {$bones.each{|bone|bone.roll}}}.grid(:column => 1, :row => 3, :sticky => 'w') roll_unscored = Tk::Tile::Button.new($content) {text 'Roll unscored!'; command {$bones.each{|bone|unless bone.scored?; bone.roll;end}}}.grid(:column => 2, :row => 3, :sticky => 'w') $bones = Array.new num = 0 until num == 6 num += 1 $bones << Bone.new("#{num}") end TkWinfo.children($content).each {|w| TkGrid.configure w, :padx => 5, :pady => 5} Tk.mainloop