【发布时间】:2015-04-27 04:32:55
【问题描述】:
谁能帮我解释一下下面的sn-p是做什么的。此代码 sn-p 取自 http://wiki.tcl.tk/12374。它旨在创建井字游戏。了解 Tk/Tcl 的资源并不多,所以这给了我很大的困难。
proc DrawBoard {{redraw 0}} {
global S B GAME C
if {$redraw} { ;# Must redraw everything
.c delete all
set w2 [expr {$B(w2) - 15}] ;# Make a little margins
set h2 [expr {$B(h2) - 15}]
set hbar [expr {$h2 / 3.0}]
set vbar [expr {$w2 / 3.0}]
set B(0) [list -$w2 -$h2 -$vbar -$hbar] ;# All 9 cells
set B(1) [list -$vbar -$h2 $vbar -$hbar]
set B(2) [list $vbar -$h2 $w2 -$hbar]
set B(3) [list -$w2 -$hbar -$vbar $hbar]
set B(4) [list -$vbar -$hbar $vbar $hbar]
set B(5) [list $vbar -$hbar $w2 $hbar]
set B(6) [list -$w2 $hbar -$vbar $h2]
set B(7) [list -$vbar $hbar $vbar $h2]
set B(8) [list $vbar $hbar $w2 $h2]
for {set i 0} {$i < 9} {incr i} { ;# Rectangle for each cell
.c create rect $B($i) -tag b$i -fill {} -outline {}
.c bind b$i <Button-1> [list DoClick $i]
set B($i) [ShrinkBox $B($i) 25]
}
.c create line -$w2 $hbar $w2 $hbar -tag bar ;# Draw the cross bars
.c create line -$w2 -$hbar $w2 -$hbar -tag bar
.c create line $vbar -$h2 $vbar $h2 -tag bar
.c create line -$vbar -$h2 -$vbar $h2 -tag bar
.c itemconfig bar -width 20 -fill $::C(bars) -capstyle round
}
.new config -state [expr {$GAME(tcnt) == 0 ? "disabled" : "normal"}]
for {set i 0} {$i < 9} {incr i} {
.c itemconfig b$i -fill {} ;# Erase any win lines
DrawXO $GAME(board,$i) $i
}
foreach i $GAME(win) { ;# Do we have a winner???
.c itemconfig b$i -fill $C(win)
}
}
好的,我最关心的问题是w2, h2, hbar, vbar 变量。特别是它们的声明方式。例如,set w2 [expr {$B(w2) - 15}]。 w2 怎么能被定义为引用它自己???作者使用这些变量来绘制井字线,但我什至不知道这些变量是什么意思。这些变量是否指定了画布的某个维度,以便作者可以使用它来绑定特定区域以单击活动?
如果我理解了这四个变量,那么其他一切都会有意义!
这是板的外观图像:
【问题讨论】:
-
w2和B(w2)实际上是不同的变量,就像bar、foobar和foo(bar)是不同的变量。