在 pygame 中垂直向下打印 for 循环中的项目?
Print items in for loop vertically down in pygame?
我正在为学校编写一个程序,它基本上像一个订购系统一样工作。在每个订单结束时,我必须询问用户是否想查看当天的累计总数(我已将其存储在可以订购的项目字典中的列表中)。我以前从未使用过 pygame,因此我正在尝试。作为 for 循环的一部分,我想垂直向下打印我的总数。这是我目前所拥有的:
pygame.init()
background_colour = (231,247,146)
(width, height) = (1000, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Totals')
myFont = pygame.font.SysFont("arial", 40)
myFont2 = pygame.font.SysFont("arial", 20)
text = myFont.render("In store totals:", True, (0, 0, 0))
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
screen.blit(text, (20, 20))
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
initial = 70
screen.blit(text2, (20, initial))
pygame.display.update()
initial += 40
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
我曾希望使用“初始”并向上递增会打印每行 20 低于另一行但它没有,而是所有打印都聚集在一行上。这对我来说是一个学习曲线,所以我什至可能没有正确完成 for 循环,但是有人知道我如何让文本在列表中垂直向下打印吗?
您必须在循环之前设置第一行的位置并在循环中增加位置:
row_y = 70
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
screen.blit(text2, (20, row_y))
row_y += 40
在应用程序循环结束时更新一次显示就足够了。多次调用 pygame.display.update()
或 pygame.display.flip()
会导致闪烁。
典型的 PyGame 应用程序循环必须:
- 限制每秒帧数以限制 CPU 使用
pygame.time.Clock.tick
- 通过调用
pygame.event.pump()
or pygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过调用
pygame.display.update()
or pygame.display.flip()
更新显示
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(background_colour)
screen.blit(text, (20, 20))
row_y = 70
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
screen.blit(text2, (20, row_y))
row_y += 40
pygame.display.update()
pygame.quit()
exit()
由于您使用 pygame.font,我将在此处使用相同的内容。但是 pygame.freetype 有更多的功能。所以考虑一下 .
pygame.init()
background_colour = (231,247,146)
(width, height) = (1000, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Totals')
myFont = pygame.font.SysFont("arial", 40)
myFont2 = pygame.font.SysFont("arial", 20)
text = myFont.render("In store totals:", True, (0, 0, 0))
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
screen.blit(text, (20, 20))
for event in pygame.event.get(): # event loop first
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
initial = 70 # initialize this variable before the for loop
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
screen.blit(text2, (20, initial))
initial += 40
pygame.display.update() # update the display outside the for loop ( inside the main game loop)
使用pygame.freetype
import pygame.freetype
FONT = pygame.freetype.SysFont("arial", 24)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
screen.blit(text, (20, 20))
initial = 70
for item, values in totalsdict.items():
text_surface, rect = FONT.render(f'{item.title()}: {values[0]}',(0,0,0))
screen.blit(text_surface, (20, initial))
initial += 40
pygame.display.update()
我正在为学校编写一个程序,它基本上像一个订购系统一样工作。在每个订单结束时,我必须询问用户是否想查看当天的累计总数(我已将其存储在可以订购的项目字典中的列表中)。我以前从未使用过 pygame,因此我正在尝试。作为 for 循环的一部分,我想垂直向下打印我的总数。这是我目前所拥有的:
pygame.init()
background_colour = (231,247,146)
(width, height) = (1000, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Totals')
myFont = pygame.font.SysFont("arial", 40)
myFont2 = pygame.font.SysFont("arial", 20)
text = myFont.render("In store totals:", True, (0, 0, 0))
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
screen.blit(text, (20, 20))
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
initial = 70
screen.blit(text2, (20, initial))
pygame.display.update()
initial += 40
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
我曾希望使用“初始”并向上递增会打印每行 20 低于另一行但它没有,而是所有打印都聚集在一行上。这对我来说是一个学习曲线,所以我什至可能没有正确完成 for 循环,但是有人知道我如何让文本在列表中垂直向下打印吗?
您必须在循环之前设置第一行的位置并在循环中增加位置:
row_y = 70
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
screen.blit(text2, (20, row_y))
row_y += 40
在应用程序循环结束时更新一次显示就足够了。多次调用 pygame.display.update()
或 pygame.display.flip()
会导致闪烁。
典型的 PyGame 应用程序循环必须:
- 限制每秒帧数以限制 CPU 使用
pygame.time.Clock.tick
- 通过调用
pygame.event.pump()
orpygame.event.get()
. 来处理事件
- 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过调用
pygame.display.update()
orpygame.display.flip()
更新显示
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(background_colour)
screen.blit(text, (20, 20))
row_y = 70
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
screen.blit(text2, (20, row_y))
row_y += 40
pygame.display.update()
pygame.quit()
exit()
由于您使用 pygame.font,我将在此处使用相同的内容。但是 pygame.freetype 有更多的功能。所以考虑一下 .
pygame.init()
background_colour = (231,247,146)
(width, height) = (1000, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Running Totals')
myFont = pygame.font.SysFont("arial", 40)
myFont2 = pygame.font.SysFont("arial", 20)
text = myFont.render("In store totals:", True, (0, 0, 0))
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
screen.blit(text, (20, 20))
for event in pygame.event.get(): # event loop first
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
initial = 70 # initialize this variable before the for loop
for item, values in totalsdict.items():
text2 = myFont2.render(f'{item.title()}: {values[0]}', True, (0,0,0))
screen.blit(text2, (20, initial))
initial += 40
pygame.display.update() # update the display outside the for loop ( inside the main game loop)
使用pygame.freetype
import pygame.freetype
FONT = pygame.freetype.SysFont("arial", 24)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if running == False:
pygame.quit()
screen.blit(text, (20, 20))
initial = 70
for item, values in totalsdict.items():
text_surface, rect = FONT.render(f'{item.title()}: {values[0]}',(0,0,0))
screen.blit(text_surface, (20, initial))
initial += 40
pygame.display.update()