Ruby tk:在程序执行时绘制

时间:2014-12-10 18:05:36

标签: ruby tk

要显示使用Tk Tk.mainloop创建的任何元素,必须在程序结束时调用。但是,如果我想在应用程序继续时绘制/显示GUI的不同部分呢?

更具体地说,我的情况是tic-tac-toe游戏。首先我画板:

def initialize_board
  @root = TkRoot.new { minsize(400, 450) }
  @root.title = "Tic-tac-toe"

  @cv = TkCanvas.new @root
  @cv.place('height' => 350, 'width' => 350, 'x' => 50, 'y' => 100)
  TkcLine.new @cv, 0, 100, 300, 100, width: 5
  TkcLine.new @cv, 0, 200, 300, 200, width: 5
  TkcLine.new @cv, 100, 0, 100, 300, width: 5
  TkcLine.new @cv, 200, 0, 200, 300, width: 5

  @menu = TkMenu.new
  one = TkMenu.new @menu
  @menu.add('cascade', :menu => one, :label => 'Select player')
  one.add('command', :label => 'Human first', :command => proc { set_players_and_play(HumanPlayer, ComputerPlayer) })
  one.add('command', :label => 'Computer first', :command => proc { set_players_and_play(ComputerPlayer, HumanPlayer) })

  @root.menu @menu

  Tk.mainloop
end

然后在控制台中询问用户他想要选择的位置,当他做的时候,标记应该显示在棋盘上,依此类推:

def print_board
  (1..9).each do |i|
    position, marker = i, @board[i]
    if marker.eql? "X"
      case position
      when 1
        TkcLine.new @cv, 20, 20, 80, 80, width: 2
        TkcLine.new @cv, 20, 80, 80, 20, width: 2
        #Tk.mainloop
      end
    elsif marker == "O"
    end
  end
end

然而,在游戏结束之前,所有标记都不会显示在棋盘上。当游戏继续进行时,我如何绘制这些标记?

1 个答案:

答案 0 :(得分:0)

  

[W]如果我想在应用程序运行时绘制/显示GUI的不同部分,那该怎么办?

     

[A] ll直到游戏结束,标记才会显示在板上。随着游戏的进行,我该如何绘制这些标记?

答案已经在您的代码中了!

即使在调用Tk.mainloop之后,只要在回调中,任何所需的Ruby代码仍然可以运行。

您的代码片段:command => proc { set_players_and_play(HumanPlayer, ComputerPlayer) }包含一个proc文字。 proc是Tk回调的示例。您的代码段指示Tk GUI系统在选择菜单项时调用该proc

因此,如您所见,菜单项具有回调。其他许多Tk小部件也可以。

我在您的代码中添加了几行以避免运行时错误(并指出了这样做的地方)。现在,它在第一个位置绘制了一个“ X”(从菜单中选择“计算机优先”时):

require 'tk' # Added this line.

def initialize_board
  @root = TkRoot.new { minsize(400, 450) }
  @root.title = "Tic-tac-toe"

  @cv = TkCanvas.new @root
  @cv.place('height' => 350, 'width' => 350, 'x' => 50, 'y' => 100)
  TkcLine.new @cv, 0, 100, 300, 100, width: 5
  TkcLine.new @cv, 0, 200, 300, 200, width: 5
  TkcLine.new @cv, 100, 0, 100, 300, width: 5
  TkcLine.new @cv, 200, 0, 200, 300, width: 5

  @menu = TkMenu.new
  one = TkMenu.new @menu
  @menu.add('cascade', :menu => one, :label => 'Select player')
  one.add('command', :label => 'Human first', :command => proc { set_players_and_play(HumanPlayer, ComputerPlayer) })
  one.add('command', :label => 'Computer first', :command => proc { set_players_and_play(ComputerPlayer, HumanPlayer) })

  @root.menu @menu

  Tk.mainloop
end

def print_board
  (1..9).each do |i|
    position, marker = i, @board[i]
    if marker.eql? "X"
      case position
      when 1
        TkcLine.new @cv, 20, 20, 80, 80, width: 2
        TkcLine.new @cv, 20, 80, 80, 20, width: 2
        #Tk.mainloop
      end
    elsif marker == "O"
    end
  end
end

# Added these lines, below:

HumanPlayer = 'Human player'
ComputerPlayer = 'Computer player'

size = 3 * 3
@board = Array.new size + 1, ''

def set_players_and_play(player_first, player_second)
  puts "#{player_first} first, then #{player_second}"
  if ComputerPlayer == player_first
    @board[1] = 'X'
    print_board
  end
end

initialize_board
print_board

这在Windows 7上使用Ruby 2.2.5(带有Tk 8.5.12)对我有用。