@JonB Thanks. Now it makes sense to me.
I think the approach that I will take is to just anchor_point all Nodes to (0, 0) because that seems to fit my brain better for complex layouts.
"""
CheckersScene contains:
One CheckersBoard ShapeNode painted White contains:
64 CheckersSquare ShapeNodes painted Red and Blue
"""
from scene import *
colorWhite = (1, 1, 1)
colorRed = (1, 0, 0)
colorBlue = (0, 0, 1)
class CheckersScene(Scene):
def setup(self):
self.board = CheckersBoard(self)
def squareAtCenterOfScreen(self):
theMin = min(self.size.w, self.size.h)
theRect = Rect(0, 0, theMin, theMin)
theOffset = abs(self.size.w - self.size.h) / 2
if self.size.w > self.size.h:
theRect.x = theOffset # landscape
else:
theRect.y = theOffset # portrait
return theRect
class CheckersBoard(ShapeNode):
def __init__(self, parent, squares_per_row=8):
x, y, w, h = parent.squareAtCenterOfScreen()
super().__init__(ui.Path.rect(x, y, w, h), anchor_point=(0, 0),
fill_color='white', parent=parent, position=(x, y))
sq_size = w / squares_per_row
for i in range(squares_per_row):
for j in range(squares_per_row):
frame = (j * sq_size, i * sq_size, sq_size, sq_size)
BoardSquare(self, frame, (i, j))
class BoardSquare(ShapeNode):
def __init__(self, parent, frame, inLocation):
x, y, w, h = frame
fill = colorRed if (inLocation[0] + inLocation[1]) % 2 else colorBlue
super().__init__(ui.Path.rect(x, y, w, h), anchor_point=(0, 0),
fill_color=fill, parent=parent, position=(x, y))
run(CheckersScene())