Ruby tk:在程序执行时绘制

问题描述 投票:2回答:1

要显示使用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

但是,在游戏结束之前,所有标记都不会显示在棋盘上。随着游戏的进行,我该如何绘制这些标记?

ruby tk
1个回答
0
投票

[W]如果我想在应用程序继续时绘制/显示GUI的不同部分?

[在比赛结束前,所有标记都不会显示在棋盘上。随着游戏的进行,我该如何绘制这些标记?

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

即使在调用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)。

© www.soinside.com 2019 - 2024. All rights reserved.