嘿,我正在寻找从列表中打印单词和数字的人。它将包含一个名称列表,然后是年龄的单独列表。我希望将其打印到我用递归打印矩形制作的“ excel”外观文档中。在理想的情况下,我可以组织每个require字段的列表,并可能使用一个循环,告诉它在某个x位置开始打印,并在循环中将每个单词向下打印某个x,从而轻松地填充行。任何帮助,将不胜感激。我曾尝试为这种情况专门寻找一些打印代码,但找不到任何代码。谢谢!
import pygame
pygame.font.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
font = pygame.font.SysFont("Arial", 25, True, False)
def main():
def recursive_draw(x, y, width, height):
"""Recursive rectangle function."""
pygame.draw.rect(screen, BLACK, [x, y, width, height], 1)
if y >= 600: # 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, 550]
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)
main()
pygame.quit()
答案 0 :(得分:2)
只需遍历数据,并为每个单元格增加y
,然后为每行增加x
。
这是它的样子:
import pygame
import pygame.freetype
def recursive_draw(surf, x, y, width, height):
"""Recursive rectangle function."""
pygame.draw.rect(surf, (0, 0, 0), [x, y, width, height], 1)
if y >= 600: # 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(surf, x, y, width, height)
else:
# Increment y and reset x to 0 and start drawing the next row.
x = 0
y += height
recursive_draw(surf, x, y, width, height)
data = [
(1, 'RED', 23, 'dog', 41),
(2, 'BLUE', 12, 'cat', 42),
(3, 'YELLOW', 12, 'horse', 43),
(4, 'GREEN', 99, 'bear', 55),
(5, 'CYAN', 52, 'snake', 14)
]
def main():
pygame.init()
font = pygame.freetype.SysFont("Arial", 25, True, False)
size = [750, 550]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
screen.fill((255, 255, 255))
background = screen.copy()
recursive_draw(background, 0, 0, 150, 50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
screen.blit(background, (0, 0))
# let's have a padding of 15px inside the cell
x = 15
y = 15
for row in data:
for cell in row:
font.render_to(screen, (x, y), str(cell), pygame.Color('dodgerblue'))
x += 150 # should be a constant
y += 50 # should be a constant
x = 15 # should be a constant, too :-)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pygame.quit()
您可以使用相同的方法来绘制矩形,因此不需要递归函数。嵌套循环就足够了。
要读取这样的文件(data.txt
):
1, 'RED', 23, 'dog', 41
2, 'BLUE', 12, 'cat', 42
3, 'YELLOW', 12, 'horse', 43
4, 'GREEN', 99, 'bear', 55
5, 'CYAN', 52, 'snake', 14
导入csv
模块并使用
data = []
with open('data.txt', newline='') as csvfile:
reader = csv.reader(csvfile, quotechar="'", skipinitialspace=True)
for row in reader:
data.append(row)
代替
data = [
(1, 'RED', 23, 'dog', 41),
...
]