gpt4 book ai didi

python - 如何使用递归绘制矩形以在 pygame 中制作表格?

转载 作者:行者123 更新时间:2023-12-01 01:21:28 26 4
gpt4 key购买 nike

我正在尝试使用 pygame 中的递归创建一个看起来相似的 Excel 文档。我得到了第一个 if 语句来填充屏幕的顶行,并寻找它每次下降 50(矩形的高度)并继续下去,直到它再次到达屏幕边缘,完全填充屏幕。我做了另一个 for 循环来尝试这个,但它停止并错过了 (0,0) 处的一个矩形,有没有办法在一个循环中执行此操作,以便屏幕填充并生成一堆列和行?谢谢。

 """
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()

最佳答案

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

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 循环来绘制网格会更容易:

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)

关于python - 如何使用递归绘制矩形以在 pygame 中制作表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com