How to Animate Lyrics/Text with Colors in Python with source code.

Pubblicato il: 28 dicembre 2024
sul canale di: CodeWithSC
6,979
126

Learn how to create an eye-catching text animation program in Python! 🎨✨ This beginner-friendly tutorial will guide you step by step on how to:

Use ANSI escape codes to add colors to your text.
Animate lyrics letter by letter with Python.
Organize your code with functions and the main() structure.
By the end of this video, you’ll have a fun, colorful program to impress your friends or enhance your coding portfolio!

Code:
import time # This imports the time module to control the speed of the animation
import sys # This imports the sys module to handle system-specific parameters like printing to the screen

// These are color codes used to change the text color in the terminal
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
RESET = '\033[0m' # This resets the color back to normal

// Lyrics stored as a list of tuples. Each tuple contains a color code and a line of lyrics.
lyrics = [
(RED, "I only see my goals, I don't believe in failure"),
(GREEN, "Cause I know the smallest voices, they can make it major"),
(YELLOW, "I got my boys with me, at least those in favor"),
(BLUE, "And if we don't meet before I leave, I hope I'll see you later"),
(MAGENTA, "Once I was 20 years old, my story got told"),
(CYAN, "I was writing'bout everything I saw before me"),
(RESET, "Once, I was 20 years old"),
]

// Function to animate text letter by letter with color
def animate_text(color, text):
for char in text: # Loop through each character in the text
sys.stdout.write(color + char + RESET) # Print the character in the specified color
sys.stdout.flush() # Immediately print the character to the screen
time.sleep(0.05) # Wait for 0.05 seconds before printing the next character
print() # Print a new line after finishing the text

// Main function to display the lyrics with animation
def main():
for color, line in lyrics: # Loop through each tuple in the lyrics list
animate_text(color, line) # Call the animate_text function to display each line
time.sleep(0.3) # Wait for 0.3 seconds before showing the next line

// This condition ensures that the main function runs only if this file is executed directly
if _name_ == "__main__":
main() # Call the main function to start the program

#PythonTutorial #TextAnimation #CodingForBeginners


In questa pagina del sito puoi guardare il video online How to Animate Lyrics/Text with Colors in Python with source code. della durata di ore minuti seconda in buona qualità , che l'utente ha caricato CodeWithSC 28 dicembre 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 6,979 volte e gli è piaciuto 126 spettatori. Buona visione!