我正致力于记忆游戏。我想在两行中显示卡片的图像。除第二行中的第一张卡外,所有卡都正确显示。运行此代码需要使用CodeSkulptor。整个计划在这里: http://www.codeskulptor.org/#user41_a22429Vx58_2.py
def draw(canvas):
global deck, cards, WIDTH, HEIGHT
num_start = 10
w = 67
h = 100
center_source = [w // 2, h // 2]
center_dest = [w // 2, h // 2]
for c in deck:
for card in cards:
if card == c:
if center_dest[0] <= WIDTH:
canvas.draw_image(cards[card], (center_source), (w , h), (center_dest), (w, h))
center_dest[0] += w + 2
elif center_dest[0] > WIDTH:
center_dest = [w // 2, h // 2 + h]
canvas.draw_image(cards[card], (center_source), (w , h), (center_dest), (w, h *2))
center_dest[0] += w + 2
答案 0 :(得分:1)
当center_dest
需要重置其水平位置时,您的elif条件仅触发1次。这是canvas.draw_image(cards[card], (center_source), (w , h), (center_dest), (w, h *2))
调用h * 2
导致不正确绘制的地方。
如果您删除*2
,它将有效。您还可以按如下方式重构逻辑,这样您就不会重复绘制:
if center_dest[0] > WIDTH:
center_dest = [w // 2, h // 2 + h]
canvas.draw_image(cards[card], (center_source), (w , h), (center_dest), (w, h))
center_dest[0] += w + 2
要使其可扩展为任意数量的行,请将更新更改为:
if center_dest[0] > WIDTH:
center_dest[0] = w // 2
center_dest[1] += h