pygame矩形简易递归画

"""pygame矩形简易递归画"""

import pygame
 
# 定义颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255) 
 
def recursive_draw(x, y, width, height):
    """ 递归地画矩形函数. """
    pygame.draw.rect(screen, BLACK,[x, y, width, height],1)
 
    # 矩形宽度大于14则再次重画
    if(width > 14):
        # x,y坐标往右下角移
        x += width * .1
        y += height * .1
        width *= .8
        height *= .8
        # 再次画,这是尾递归
        recursive_draw(x, y, width, height)
 
pygame.init()
 
# 设置屏幕宽度和高度并且创建screen对象做为最底层渲染面
size = [700, 500]
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("pygame矩形简易递归画")
 
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)
 
    # 递归地画矩形
    recursive_draw(0, 0, 700, 500)

    # 显示
    pygame.display.flip()
 
    # 限制 60 的每秒显示帧数。frames per second
    clock.tick(60)
 
pygame.quit()