Changing text color on output - colors

I have written a text based game for the class I am taking, but I want to see how I can change certain output text to different colors. Say the villain loses, the text prints out red or something, and vice versa, if he wins, the text prints out in like blue. Do I need to define all the colors first, or is there a way to add it to the string? example:
print('Thank you for playing but unfortunately you lost your life!')
How can I change the output to red text?

is this python?
If so here is a link to a few options python colors
in your case I guess you want to have something like:
text = colored('Thank you for playing but unfortunately you lost your life!', 'red', attrs=['reverse', 'blink'])
print(text)

I'm basing my game off Game of Thrones. Here a sample of the output when the character loses. I'm still working on the story.
else:
print('\nOh dear! You did not collect all of the items!')
print('You were caught by Daenerys and set on fire by Drogon!')
print('Drogon escaped and set the township on fire!')
print('Thank you for playing but unfortunately you lost your life!')
print('You must start over')

Related

Is there any way to draw large characters using ncursesw?

I want to create a main page for a program I'm trying to write and basically, I want to print the title at the center with a large font and menu and the rest after that. What I'm looking for is any character set in ncursesw or just anything which will help me draw large characters for title more precisely
For this (in ncurses), I tried A_REVERSE attribute and printed the respective places white, something like this
Large text
But other letters like z, g and t are hard to draw and they don't look good either.

ANSI Colors not showing where they are supposed to show

I would like for the entire text that pops up when you use the help() function to be affected by the color code that I used, but instead when I run the script the only part that is affected by said color code is a "None" text below the help() result
print(f'\033[34m {help("for")} \033[m')
This is the single line of code and how it looks in the terminal
The (pretty simple) workaround that I found is to just print the color codes on separate lines
Example:
print('\033[41m')
print(help('for'))
print('\033[m')
That code will make it so the output of the help('for') function has a red background

How to print text with multiple different types of colors in he same line in Python 3?

I am trying to do some visualizing data on the terminal and I am doing lots of printing to do that. The issue I am having is that certain character symbols look the same. I figured that coloring them differently would help me see the differences. I see from this link that there is a way to do it, but I don't understand what I am looking at. There is no explanation for what is going on in those solutions.
How do you specify, in the same line, text with different colors?
I should also mention that I am building the printable string OUTSIDE of the call to print(). How do you build the colorful string outside of the print() call?
A rewritten form of my question:
1. Colorize the text I print to the string with multiple types of colors.
2. Colorize the string BEFORE it gets sent to the "print()" call.
A couple examples would be great.
Using colorama just like the answer you linked is doing:
from colorama import Fore, Style
my_str = f"{Fore.BLUE}Hello, {Style.RESET_ALL} guys. {Fore.RED} I should be red."
print(my_str)
This gives me:
As you can see Fore.<color name> changes the color of the text after it, until the Style.RESET_ALL. After that you can change the color of the text again.
There could be multiple ways to achieve this. One which doesn't require any extra packages is to use ANSI color codes. Look at this link. Below are some examples.
s = "\033[1;32;40m Bright Green on black \033[1;31;43m Red on yellow \033[1;34;42m Blue on green \033[1;37;40m"
print(s)
Here in first code \033[1;32;40m, \033[ is the escape code followed by 1 for bold, 32 for bright green text and 40 for black background. The 3 codes are separated by ; and ended with m. Adding all the 3 codes (1, 32 and 40 here) isn't mandatory though.
output:
Other ways to achieve this can be found here.

Fastest way to get X/Y position of text or text-based shape in screenshot?

I'm trying to create a script for Linux that will detect where the text cursor is. This should be done in maximum 1 second. In order to implement this, the best solution seems to be to programmatically add some text via xdotool, take a screenshot via some other utility, try to figure out the position of that text, and then remove the text we've inserted using xdotool again.
I tried inserting a random string (like <-- CURSOR HERE). Using Tesseract 4 it takes about 20 seconds to find the position of the string, although it's very precise in terms of pixel coordinates. I was not able to use whitelisting (in version 4 of Tesseract) to narrow result to specific letters or digits only, which I assume would speed up processing.
I don't know what font the user will be using, but every font has dashes and slashes, so I could create some sort of shape (for instance, |/\|/\|/\|/\|), and use some library to detect that shape. What would be a good choice?
I don't care about what's on the rest of the screen: it could be more text, images, etc. I only need o know where my random string is (<-- CURSOR HERE, |/\|/\|/\|/\|, or can you think of anything else), and get its X/Y position in pixels.

display.newText isnt working

first time user of Stackoverflow;
Problem: even at the basic level of trying to print out simply Hello world nothing seems to happen.
using this:
local text = display.newText("helloworld", 50, 50, native.systemFont, 24)
print(text)
the varible "text" when printed holds the value of "Table: 010F3E50" which changes on each reload
im new to this language and this is really annoying - Any help would be fantastic
Cheers
Actually, you are trying to print the object. Not the text inside. So try:
print(text.text)
Instead of:
print(text)
Keep coding............ :)
The color of the text might be black by default which might be the same color as the background in the simulator. Meaning it's visible but you just can't see it!
Trying changing the color of the text object by doing this:
text:setFillColor(1,0,0) -- red text

Resources