Top half of text in pygame is not being displayed - python

I imported a custom font called "Celtic-Bit Thin" from a website to use in a project. The file had the correct extension (.ttf) that pygame can import but when used, only the bottom half of the string of text that is displayed is shown (You can only see the bottom half of each letter in the string of text displayed in the font I chose). I'm not sure if it is a problem with the font or the way I implemented it. I tried:
gameFont = pygame.font.Sysfont("Celtic-Bit Thin", 24)
font.render("Hello world!", False, (0,0,0))
I also tried doing it with pygame.font.Font()but it didn't work either. Is the font simply incompatible with pygame or is there some other requirement for importing fonts?
EDIT: Here is a minimal, runnable example:
import pygame
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800,600))
gameFont = pygame.font.SysFont("Celtic-Bit Thin", 36)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
runnung = False
screen.fill((255,255,255))
screen.blit(gameFont.render("Hello World!",False,(0,0,0)), (300, 300))
pygame.display.flip()
pygame.quit()
AND here is where I downloaded the font

I don't know why the font is cut off, but I've tried to render it with the pygame.freetype module instead of pygame.font and that works fine.
import pygame
import pygame.freetype # Import the freetype module.
pygame.init()
screen = pygame.display.set_mode((800,600))
gameFont = pygame.freetype.SysFont("Celtic-Bit Thin", 24)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255,255,255))
# You can use `render` ...
text_surface, rect = gameFont.render("Hello World!", (0,0,0))
screen.blit(text_surface, (40, 250))
# or `render_to`.
gameFont.render_to(screen, (40, 350), "Hello World!", (0,0,0))
pygame.display.flip()
pygame.quit()

Related

Pygame black background has a distorted/broken image

I started a basic project with pygame and tried to set up the basics.
import pygame
BLACK = (0, 0, 0)
WHITE = (200, 200, 200)
SIZE = width, height = 500, 700
def main() -> None:
global SCREEN
pygame.init()
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption('Tetronur')
running = True
SCREEN.fill(BLACK)
while running:
draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
def draw():
rect = pygame.Rect(100, 100, 400, 600)
pygame.draw.rect(SCREEN, WHITE, rect, 1)
if __name__ == '__main__':
main()
But the thing is, when I run the code, I get a screen looking like this:
PS: I use Python 3.9.6 and Pygame 2.0.1
Pygame draws to a hidden screen, you must call pygame.display.flip() or pygame.display.update() after drawing so that this hidden screen is displayed to the actual screen.

Why won't pygame rectangles show up?

I'm just going to give you all the code and say that I think it's probably something with the pygame. If someone sees the error or you try it on your own compiler and it works, it would be great if you could tell me what it is. Right now for me it just opens a black screen. Thanks so much, here's the code:
import pygame
from pygame.locals import *
pygame.init()
test = pygame.display.set_mode((1280, 660))
pygame.display.flip()
pygame.draw.rect(test, (255,255,255), (100, 100, 100, 100))
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT:
running = False
pygame.quit()
We want move pygame.display.flip() after all the drawing commands. This is basically telling the program to update the screen after all the draw commands. That being said, all draw commands after this line will not be updated in the visual screen.
Here is the revised code:
import pygame
from pygame.locals import *
pygame.init()
test = pygame.display.set_mode((1280, 660))
pygame.draw.rect(test, (255,255,255), (100, 100, 100, 100))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT:
running = False
pygame.quit()
If you are using a white screen 255,255,255 is white and your rectangles are just white, try to use (0, 0, 0) or any other color what are you experiencing?
Edited:
The problem is with pygame.display.flip() it is updating the display you should call it after the call to draw

How to fix this error with pygame and fonts [duplicate]

This question already has answers here:
pygame - How to display text with font & color?
(7 answers)
Closed 1 year ago.
import pygame, sys
pygame.init()
clock = pygame.time.Clock()
coordinate = pygame.mouse.get_pos()
screen = pygame.display.set_mode((1000,800), 0, 32)
pygame.display.set_caption("Mouse Tracker")
font = pygame.font.Font(None, 25)
text = font.render(coordinate, True, (255,255,255))
while True:
screen.fill((0,0,0))
screen.blit(text, (10, 10))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(60)
I am trying to make a program that tracks your mouse and shows the coordinates of it on the sreen. I get an error saying:
text = font.render(coordinate, True, (255,255,255))
TypeError: text must be a unicode or bytes.
I am using Python 3.9.1
There are couple of changes to be made:
coordinate = pygame.mouse.get_pos() returns a tuple which the variable coordinate is assigned to. The font.render() method takes a string as an argument not a tuple. So first you need to render the str(coordinate) and not just the coordinate which is actually a tuple. You can read more about rendering the font in pygame here
text = font.render(str(coordinate), True, (255,255,255)) #Rendering the str(coordinate)
Step one alone won't make your code functional, there are still some problems in your code. For blitting the coordinate of the mouse to the screen, you need to get the mouse coordinates at every single frame. For that to function you need to place the line coordinate = pygame.mouse.get_pos() in the while True loop, along with this you also need to place this line text = font.render(str(coordinate), True, (255,255,255)) in the while loop
import pygame,sys
#[...]#part of code
while True:
coordinate = pygame.mouse.get_pos() #Getting the mouse coordinate at every single frame
text = font.render(str(coordinate), True, (255,255,255))
#[...] other part of code
So the final working code should look somewhat like:
import pygame, sys
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1000,800), 0, 32)
pygame.display.set_caption("Mouse Tracker")
font = pygame.font.Font(None, 25)
while True:
coordinate = pygame.mouse.get_pos() #Getting the mouse coordinate at every single frame
text = font.render(str(coordinate), True, (255,255,255)) #Rendering the str(coordinate)
screen.fill((0,0,0))
screen.blit(text, (10, 10))
for event in pygame.event.get():
if event.type == pygame.QUIT:#
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(60)

Pygame not responding when trying to show texts on macOS Sierra

I've installed pygame version 1.9.4 on python 3.5.3 using instructions on programarcadegames.com. When I use pygame to draw lines and shapes it's just fine but when I try to use pygame.font.SysFont the pygame is not responding anymore. For example when I run following code a non-responsive pygame window will show up:
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
size = (400, 500)
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
screen.fill(WHITE)
font = pygame.font.SysFont(None, 25, True, False)
text = font.render("Some Text", True, BLACK)
screen.blit(text, [0, 0])
pygame.display.flip()
clock.tick(60)
pygame.quit()
Replacing font = pygame.font.SysFont(None, 25, True, False) with font = pygame.font.Font(None, 25) fixed the problem, but it is still unclear what caused it.

Pygame font error

So I decided to start making a game and I was testing it a bit and then I got this error:
Traceback (most recent call last):
File "TheAviGame.py", line 15, in <module>
font = pygame.font.Font(None,25)
pygame.error: font not initialized
I have no idea what I have done wrong so far...
Code:
#!/usr/bin/python
import pygame
blue = (25,25,112)
black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
groundcolor = (139,69,19)
gameDisplay = pygame.display.set_mode((1336,768))
pygame.display.set_caption("TheAviGame")
direction = 'none'
clock = pygame.time.Clock()
img = pygame.image.load('player.bmp')
imgx = 1000
imgy = 100
font = pygame.font.Font(None,25)
def mts(text, textcolor, x, y):
text = font.render(text, True, textcolor)
gamedisplay.blit(text, [x,y])
def gameloop():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.pygame == pygame.KEYDOWN:
if event.key == pygame.RIGHT:
imgx += 10
gameDisplay.fill(blue)
pygame.display.update()
clock.tick(15)
gameloop()
You never initialized pygame and pygame.font after importing:
# imports
pygame.init() # now use display and fonts
In order to use some pygame modules, either pygame or that specific module has to be initialised before you start using them - this is what the error message is telling you.
To initialize pygame:
import pygame
...
pygame.init()
To initialize a specific library (eg font):
import pygame
...
pygame.font.init()
In most cases, you will want to initialise all of pygame, so use the first version. In general, I would place this at the top of my code, just after importing pygame
You put None in the font:
font = pygame.font.Font(None, 25)
You can’t do that.
You have to put a type of font there.
And you should call pygame.init().

Categories

Resources