【问题标题】:How do i make this text animation show in pygame如何在 pygame 中显示此文本动画
【发布时间】:2020-05-05 09:56:36
【问题描述】:

我很抱歉,但我是 PyGame 的新手,我希望能够在 PyGame 窗口中显示此文本动画。 (我顺便用repl.it)

在这个动画中,我不断打印新的文本行,这些文本被着色以产生双螺旋效果,并且我还使用“时间”功能使其在打印文本行之间等待以使其更可信

代码如下:

print(chr(27)+'[2j')
print('\033c')
print('\x1bc')
import colorful as cf
import time
while True:
  print(cf.red('        0'))
  time.sleep(0.2)
  print(cf.red('      0' + cf.yellow('   0')))
  time.sleep(0.2)
  print(cf.red('    0' + cf.yellow('        0')))
  time.sleep(0.2)
  print(cf.red('   0' + cf.yellow('           0')))
  time.sleep(0.2)
  print(cf.red('  0' + cf.yellow('             0')))
  time.sleep(0.2)
  print(cf.red(' 0' + cf.yellow('               0')))
  time.sleep(0.2)
  print(cf.red(' 0' + cf.yellow('               0')))
  time.sleep(0.2)
  print(cf.red(' 0' + cf.yellow('              0')))
  time.sleep(0.2)
  print(cf.red('  0' + cf.yellow('            0')))
  time.sleep(0.2)
  print(cf.red('   0' + cf.yellow('         0')))
  time.sleep(0.2)
  print(cf.red('     0' + cf.yellow('     0')))
  time.sleep(0.2)
  print(cf.yellow('        0'))
  time.sleep(0.2)
  print(cf.yellow('      0' + cf.red('   0')))
  time.sleep(0.2)
  print(cf.yellow('    0' + cf.red('        0')))
  time.sleep(0.2)
  print(cf.yellow('   0' + cf.red('           0')))
  time.sleep(0.2)
  print(cf.yellow('  0' + cf.red('             0')))
  time.sleep(0.2)
  print(cf.yellow(' 0' + cf.red('               0')))
  time.sleep(0.2)
  print(cf.yellow(' 0' + cf.red('               0')))
  time.sleep(0.2)
  print(cf.yellow(' 0' + cf.red('              0')))
  time.sleep(0.2)
  print(cf.yellow('  0' + cf.red('            0')))
  time.sleep(0.2)
  print(cf.yellow('   0' + cf.red('         0')))
  time.sleep(0.2)
  print(cf.yellow('     0' + cf.red('     0')))
  time.sleep(0.2)

【问题讨论】:

  • 我不是你的领域,但问题不是很清楚。

标签: python animation pygame repl.it


【解决方案1】:

要在 pygame 中显示文本,设置字体、渲染、放在屏幕上。要替换文本,您只需删除以前的文本并放入新文本。要使用多种颜色的文字,您必须多次放置一种颜色的文字。

我根据您的问题制作了示例代码。注意:在这段代码中,红色文本总是在黄色的左边。

import pygame
import sys
import time

pygame.init()
screen = pygame.display.set_mode((640, 480))
done = False

font = pygame.font.SysFont("comicsansms", 72)

# position offset to display text
pos_x = 100
pos_y = 0

red_yellow = [
    ['        0', ''],
    ['      0', '   0'],
    ['    0', '        0'],
    ['   0', '           0'],
    ['  0', '             0'],
    [' 0', '               0'],
    [' 0', '               0'],
    [' 0', '              0'],
    ['  0', '            0'],
    ['   0', '         0'],
    ['     0', '     0'],
]

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            done = True
    screen.fill((255, 255, 255))
    for t_r, t_y in red_yellow:
        # screen.fill((255, 255, 255))
        text_r = font.render(t_r, True, (255, 0, 0))  # red
        text_y = font.render(t_y, True, (255, 255,0))  # yellow
        screen.blit(text_r, (pos_x, pos_y)) # put red text
        screen.blit(text_y, (pos_x + text_r.get_width(), pos_y))  # put yellow right to the red text
        pygame.display.flip()
        time.sleep(0.2)
        pos_y += text_r.get_height()
    # break

pygame.quit()
sys.exit()

【讨论】:

  • 这有点用,但它会重写文本而不是在新行中进行
  • 如果它是可行的,我也需要它永远循环
  • 如何让它写入新行而不是重写同一行?
  • 我已经添加了这一行pos_y += text_r.get_height()。您可以根据需要更改此高度。
猜你喜欢
  • 1970-01-01
  • 2014-01-17
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
相关资源
最近更新 更多