首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在pygame中使用递归绘制矩形来制作表格?

如何在pygame中使用递归绘制矩形来制作表格?
EN

Stack Overflow用户
提问于 2018-12-16 14:33:21
回答 1查看 1.1K关注 0票数 2

我正在尝试用pygame中的递归创建一个类似于excel文档的外观。我得到了第一个if语句来填充屏幕的第一行,并希望它每次都下降50 (我的矩形的高度),并继续前进,直到它再次命中屏幕的边缘,完全填满屏幕。我做了另一个for循环来尝试这一点,但是它在(0,0)处停止并遗漏了一个矩形,有没有办法在一个循环中做到这一点,这样屏幕就会充满并产生一堆列和行?谢谢。

代码语言:javascript
复制
 """
    Recursively draw rectangles.
    Sample Python/Pygame Programs
    Simpson College Computer Science
    http://programarcadegames.com/
    http://simpson.edu/computer-science/
    """
    import pygame
    # Colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    def recursive_draw(x, y, width, height):
        """ Recursive rectangle function. """
        pygame.draw.rect(screen, BLACK,
        [x, y, width, height],
        1)
        # Is the rectangle wide enough to draw again?
        if(x < 750):
            # Scale down
            x += 150
            y = 0
            width = 150
            height = 50
            # Recursively draw again
            recursive_draw(x, y, width, height)
        if (x < 750):
            # Scale down
            x += 0
            y += 50
            width = 150
            height = 50
            # Recursively draw again
            recursive_draw(x, y, width, height)
    pygame.init()
    # Set the height and width of the screen
    size = [750, 500]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("My Game")
    # Loop until the user clicks the close button.
    done = False
    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()
    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        # Set the screen background
        screen.fill(WHITE)
        # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
        recursive_draw(0, 0, 150, 50)
        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
        # Limit to 60 frames per second
        clock.tick(60)
        # Be IDLE friendly. If you forget this line, the program will 'hang'
        # on exit.
    pygame.quit()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-16 17:25:37

我首先添加一个基本情况,这样当到达屏幕底部时,函数就会返回。将width添加到x,直到到达右侧,当它在右侧时,递增y += height并重置x = 0以开始绘制下一行。

代码语言:javascript
复制
import pygame


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

def recursive_draw(x, y, width, height):
    """Recursive rectangle function."""
    pygame.draw.rect(screen, BLACK, [x, y, width, height], 1)
    if y >= 500:  # Screen bottom reached.
        return
    # Is the rectangle wide enough to draw again?
    elif x < 750-width:  # Right screen edge not reached.
        x += width
        # Recursively draw again.
        recursive_draw(x, y, width, height)
    else:
        # Increment y and reset x to 0 and start drawing the next row.
        x = 0
        y += height
        recursive_draw(x, y, width, height)


pygame.init()
size = [750, 500]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
screen.fill(WHITE)
recursive_draw(0, 0, 150, 50)

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.display.flip()
    clock.tick(60)


pygame.quit()

使用嵌套的for循环来绘制网格会更容易:

代码语言:javascript
复制
def draw_grid(x, y, width, height, size):
    for y in range(0, size[1], height):
        for x in range(0, size[0], width):
            pygame.draw.rect(screen, BLACK, [x, y, width, height], 1)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53799976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档