Как мне сделать это текстовое анимационное шоу в Pygame? - PullRequest
1 голос
/ 19 января 2020

Я прошу прощения, но я новичок в 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)

1 Ответ

1 голос
/ 19 января 2020

Для отображения текста в 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()
...