1

I apologize, but I am new to PyGame and I want to be able to have this text animation show in a PyGame window. (I use repl.it by the way)

In this animation, I constantly print new lines of text that are colorized to make a double-helix effect and I also use the "time" function to make it wait in between printing the line of text to make it more believable

Here is the code:

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)
Timotej Leginus
  • 304
  • 3
  • 18

1 Answers1

1

To display text in pygame, set font, render, put on screen. To replace text, you just need remove previous text and put new. To use multiple color text, you have to put one color text multiple times.

I have made sample code from your question. Note: in this code red text is always left of yellow.

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()
shimo
  • 2,156
  • 4
  • 17
  • 21