Replace Text On Pygame Window - python

I am making a score system on my python file. I tried using this:
font = pygame.font.SysFont(None, 25)
text = font.render('Score: '+str(score), True, black)
gameDisplay.blit(text, [0,0])
For some reason, the new score goes on top of the old score, and you can't read it. It makes a huge blob of numbers. Is there a way of replacing the text?

In your game loop, you have to clear what was previously drawn by filling the screen then painting:
gameDisplay.fill(your_color_tuple)
# afterwards add your drawing code

Related

Python-pptx compatibility with text boxes in LibreOffice

Just starting to try to use python-pptx, and have fallen at the first. Linux Mint 20.1, python 3.85, LibreOffice 6.4.
This is basically the 'Hello World' from the documentation.
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
print('before text ', txBox.left, txBox.top, txBox.width, txBox.height)
tf.text = "This is a long line of text inside a textbox"
print('after text ', txBox.left, txBox.top, txBox.width, txBox.height)
prs.save('test.pptx')
The text is more than a single line for the textbox. Printing out its bounds before and after text insertion shows that as far as python-pptx is concerned, the textbox width hasn't changed.
When the resulting presentation is viewed in LibreOffice, instead of wrapping the text within its boundaries, the textbox has expanded symmetrically around the mid point, pushing the start of the text off the lefthand edge of the page.
I was hoping the LibreOffice was only incompatible with powerpoint for rare edge cases, but text in text boxes is the meat and bread of presentations.
When I upload it to google slides, the text wraps within the left and right text box boundaries, but spills out of the bottom edge. The textbox shows up as 1" x 1" in the right place.
If I use onedrive.live.com, the text is left justified in the box and spills out of the righthand side without wrapping, and the textbox is shown as being 1" x 1" in the right place.
If I use onlinedocumentviewer.com, the display is the same as onedrive, though I can't get to see the text box.
Unfortunately I can't test the behaviour on a native powerpoint installation.
Maybe there's an autosize or fixed flag, which left unset leaves each viewer defaulting it in its own idiosyncratic way? How do we control text boxes / frames when targetting LibreOffice?
I possibly have a workaround to break up my text into single lines and use one per text box, but I'd rather understand the whether it can be done the proper way.
After some floundering around in the docs, I stumbled across the text_frame .word_wrap property. Setting to True gets the text to wrap in LibreOffice. While I was there, setting text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE reduces font size to get it all to fit in the box.
Is there a list of properties like .word_wrap in one place anywhere?

Pygame text displays wrong fonts

I am using pygame to make a game, and I want to display some text, but the font doesn't change to any fonts that I've downloaded. I am using a Mac and the font that I downloaded appears in Font Book. I've used pygame.font.init, define the font as font = pygame.font.SysFont('font name', size), and use font.render(message, False, colour).
When defining the font, I tried using both the absolute and relative paths. When I run the program, it displays the text in the default font instead of the font I have downloaded. Why does this happen?
Use pygame.font.Font() instead of SysFont to use downloaded fonts
Using this syntax:
font = pygame.font.Font("font name/path", size)
use the font.render() function to create a font surface you can display:
font.render(yourMessage, True/False, colour)

Green screen in MoviePy

I'm trying to automate the implementation of some animations in my videos using MoviePy. These animations are played on a green background, so I've been searching for green screen related items on MoviePy. There are not too many results on this Though. However, I did find some code that should work with this. The code that I'm using is the following:
final_clip = concatenate_videoclips(self.clips, method="compose")
subclip = mpe.vfx.mask_color(subclip, color=[100, 227, 4])
subclip = subclip.resize(height=int(final_clip.h / 2))
subclip = subclip.set_position((final_clip.w / 2 - subclip.w / 2, final_clip.h / 2))
comp = CompositeVideoClip([image_clip, final_clip, subclip.set_start(final_clip.duration / 2)])
So, final_clip is every clip stitched together, subclip is the animation on a green screen that I want to display on top somewhere in the middle of the video and comp is the endresult. comp Combines final_clip and subclip and places it on an ImageClip, which serves as background. This all works and gives no errors, but the green in subclip is not gone, it's just the full frame still visible. And I know the RGB color [100, 227, 4] should be correct as I used multiple pieces of software to verify which kind of green it was. So I have subclip, which I want to be half the width and half the height of the video, and placed in the center, but a bit down. The positioning and sizing of the clip is done correctly, it's really just the green that is not being removed here.
I don't get what's wrong here, the samples that I found online use this exact same technique, so if anyone could help me out here, that would be much appreciated!
Also, if anyone knows any hints to speed up the compilation time in MoviePy, that would be a great help as well!

How to display an utf-8 character in pygame?

Is there a way to render a tick mark using pygame? I tried using the unicode directly like this:
def write(screen, text, color, position, size):
font = pygame.font.Font(pygame.font.get_default_font(), size)# Defining a font with font and size
text_surface = font.render(text, True, color)# Defining the text color which will be rendered
screen.blit(text_surface, (position[0], position[1])) # Rendering the font
write(window, u'\u2713', self.color, position, size)
But this just draws a rectangle.
This 'rectangle' that is being written is in fact the representation of the ✓ sign using a pygame.font.get_default_font(). In order to display a ✓ sign you need to be sure, that this sign is included in a font that you are using. I propose the following solution:
Download the font that includes this symbol (e.g. Segoe UI Symbols from here)
Unzip the font package into the data folder (or main applicaation folder)
Change the font that you are using to the downloaded one
font = pygame.font.Font("seguisym.ttf", size)
Use a write function either using a write(window, u'\u2713', self.color, position, size) method or directly using write(screen, "✓", self.color, position, size)

Python PIL using different font

I have an image I would like to add text to but I want to use both a different style font and a something bigger than the smallest thing known to mankind. Heck you can hardly read the text it's so darn small. I can print the text to the screen but I can't change the font style or the size of it as I don't know where the fonts are stored. I would think there would be more than one font/font size standardly available for use with Python/PIL. Where are they stored at? I am on a Linux.
Adapted from this answer:
image = Image.new("RGBA", (600,150), (255,255,255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", fontsize)
draw.text((10, 0), txt, (0,0,0), font=font)
Looks like you can specify any .ttf file and any fontsize you want using the above modules and functions.

Categories

Resources