我正在尝试使用Pygame中的递归来编写Sierpinski Carpet。代码运行良好,问题是屏幕只显示一个递归调用,而不是8,如代码所示。我很确定它正在经历那些递归调用,因为我设置了时间延迟并且需要时间来实际完成绘图。 我的问题是:每次有递归调用时代码中会缺少什么?谢谢,这是代码:
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
def recursiveSquare((x, y), side, color, level):
if level:
(x2, y2) = (x + side, y)
(x3, y3) = (x, y + side)
(x4, y4) = (x + side, y + side)
pygame.draw.polygon(screen, color, [(x, y), (x2, y2), \
(x4, y4), (x3, y3)])
recursiveSquare(((side * 0.3), (side * 0.3)), \
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3) * 4.5, (side * 0.3)), \
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3) * 8, (side * 0.3)), \
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3), (side * 0.3) * 4.5), \
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3) * 8, (side * 0.3) * 4.5),\
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3), (side * 0.3) * 8), \
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3) * 4.5, (side * 0.3) * 8),\
(side * 0.3), color, level - 1)
recursiveSquare(((side * 0.3) * 8, (side * 0.3) * 8),\
(side * 0.3), color, level - 1)
pygame.init()
size = [600, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Sierpinski Carpet")
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(white)
level = 3
recursiveSquare((200, 200), 200, negro, level)
pygame.display.flip()
clock.tick(20)
pygame.quit()
quit()