I have this piece of code right here:
import pygame
pygame.display.init()
pygame.font.init()
win = pygame.display.set_mode((100, 100))
font = pygame.font.Font('./arial.ttf', 10)
text = font.render('سلام', True, (255, 255, 255))
win.fill((0, 0, 0))
win.blit(text, (0, 0))
pygame.display.update()
for i in range(5):
pygame.event.clear()
pygame.time.delay(1000)
pygame.quit()
What I expect: سلام
What I get: سلام
How to fix it? (I don't care if I need to use another library to fix it, But I must use pygame for rest of code)
I also tried it on both pygame 1.9.6 and 2.0.0
You have to use arabic_reshaper library.
pip install arabic-reshaper
Refer to this https://github.com/mpcabd/python-arabic-reshaper
You will need python-bidi as well
pip install python-bidi
You can do it as follow
import pygame
import arabic_reshaper
from bidi.algorithm import get_display
pygame.display.init()
pygame.font.init()
win = pygame.display.set_mode((100, 100))
font = pygame.font.Font('arial.ttf', 10)
text_to_be_reshaped = 'اللغة العربية رائعة'
reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)
bidi_text = get_display(reshaped_text)
print(reshaped_text)
text = font.render(bidi_text, True, (255, 255, 255))
win.fill((0, 0, 0))
win.blit(text, (0, 0))
pygame.display.update()
for i in range(5):
pygame.event.clear()
pygame.time.delay(1000)
pygame.quit()
OUTPUT
Related
I was trying to print out just a simple title page using pygame.
My original code looked like:
import pygame
import pygame.freetype
pygame.init()
pygame.display.set_caption('hello world')
screen = pygame.display.set_mode((800, 600), 0, 32)
title_font = pygame.font.Font(".../Montserrat-BlackItalic.ttf", 24)
title = title_font.render('sup', False, (250, 250, 250))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
screen.fill((25, 25, 25))
screen.blit(title, (0, 0))
pygame.display.update()
pygame.quit()
And it works. However, the font was super blurry. I read that using freetype font will help to make it less blurry but when I tried, I received this error:
File ".../untitled1.py", line 22, in <module>
screen.blit(title, (0, 0))
TypeError: argument 1 must be pygame.Surface, not tuple
I only changed my code to look for the font type so that it looked like that:
title_font = pygame.freetype.Font(".../Montserrat-BlackItalic.ttf", 24)
How do I get less blurry fonts now?
The API interface of pygame.freetype is different from that of pygame,fon. The pygame.freetype module has 2 options to display the text. It can either create a Surface like the pygame.fornt module or it can render the text directly on the screen (respectively a Surface). pygame.freetype.Font.render returns a tuple with the rendered text and a rectangle size of the surface. The equivalent of
title_font = pygame.font.Font(".../Montserrat-BlackItalic.ttf", 24)
title = title_font.render('sup', False, (250, 250, 250))
screen.blit(title, (0, 0))
is either
ft_font = pygame.freetype.Font(".../Montserrat-BlackItalic.ttf", 24)
title_surf, title_rect = ft_font.render('sup', (250, 250, 250))
screen.blit(title_surf, title_rect)
or
ft_font = pygame.freetype.Font(".../Montserrat-BlackItalic.ttf", 24)
ft_font.render_to(screen, (0, 0), 'sup', (250, 250, 250))
I'm having trouble with the framerate in my game. I've set it to 60 but it only goes to ~25fps. This was not an issue before displaying the background (was fine with only win.fill(WHITE)). Here is enough of the code to reproduce:
import os, pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50, 50)
pygame.init()
bg = pygame.image.load('images/bg.jpg')
FPS = pygame.time.Clock()
fps = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
winW = 1227
winH = 700
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_icon(win)
def redraw_window():
#win.fill(WHITE)
win.blit(bg, (0, 0))
win.blit(text_to_screen('FPS: {}'.format(FPS.get_fps()), BLUE), (25, 50))
pygame.display.update()
def text_to_screen(txt, col):
font = pygame.font.SysFont('Comic Sans MS', 25, True)
text = font.render(str(txt), True, col)
return text
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
FPS.tick(fps)
pygame.quit()
Ensure that the background Surface has the same format as the display Surface. Use convert() to create a Surface that has the same pixel format. That should improve the performance, when the background is blit to the display, because the formats are compatible and blit do not have to do an implicit transformation.
bg = pygame.image.load('images/bg.jpg').convert()
Furthermore, it is sufficient to create the font once, rather than every time when a text is drawn. Move font = pygame.font.SysFont('Comic Sans MS', 25, True) to the begin of the application (somewhere after pygame.init() and before the main application loop)
Instead use screen.blit(pygame.image.load(picture.png))
Just image = pygame.image.load(picture.png) then screen.blit(image)
( if you keep loading your pictures continuously it will get lag )
I'm having trouble with the framerate in my game. I've set it to 60 but it only goes to ~25fps. This was not an issue before displaying the background (was fine with only win.fill(WHITE)). Here is enough of the code to reproduce:
import os, pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50, 50)
pygame.init()
bg = pygame.image.load('images/bg.jpg')
FPS = pygame.time.Clock()
fps = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
winW = 1227
winH = 700
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_icon(win)
def redraw_window():
#win.fill(WHITE)
win.blit(bg, (0, 0))
win.blit(text_to_screen('FPS: {}'.format(FPS.get_fps()), BLUE), (25, 50))
pygame.display.update()
def text_to_screen(txt, col):
font = pygame.font.SysFont('Comic Sans MS', 25, True)
text = font.render(str(txt), True, col)
return text
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
FPS.tick(fps)
pygame.quit()
Ensure that the background Surface has the same format as the display Surface. Use convert() to create a Surface that has the same pixel format. That should improve the performance, when the background is blit to the display, because the formats are compatible and blit do not have to do an implicit transformation.
bg = pygame.image.load('images/bg.jpg').convert()
Furthermore, it is sufficient to create the font once, rather than every time when a text is drawn. Move font = pygame.font.SysFont('Comic Sans MS', 25, True) to the begin of the application (somewhere after pygame.init() and before the main application loop)
Instead use screen.blit(pygame.image.load(picture.png))
Just image = pygame.image.load(picture.png) then screen.blit(image)
( if you keep loading your pictures continuously it will get lag )
I want to render some unicode characeters on screen.
Using pygame.font displays a weird character.
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("TEST")
FONT = pygame.font.Font(None, 64)
font_surf = FONT.render("♛", True, pygame.Color("red"))
screen.blit(font_surf, (20, 20))
pygame.display.flip()
pygame.time.delay(1000)
I also tried using pygame.freetype. It displays nothing at all.
import pygame.freetype
pygame.freetype.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("TEST")
FONT = pygame.freetype.Font(None)
FONT.render_to(screen, (20, 20), "♛", size=(40, 40))
pygame.display.flip()
pygame.time.delay(1000)
You need to add your font name and location .
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
On Python 3.4 you no longer need the u before "♛" like in Python 2.7.
unistr = "♛"
sample based on that other link but for 3.4 as example is 2.7
# -*- coding: utf-8 -*-
import pygame
import sys
unistr = "♛"
pygame.font.init()
srf = pygame.display.set_mode((500,500))
f = pygame.font.Font("segoe-ui-symbol.ttf",64)
srf.blit(f.render(unistr,True,(255,0,0)),(0,0))
pygame.display.flip()
while True:
srf.blit(f.render(unistr,True,(255,255,255)),(0,0))
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
I'm having trouble with the framerate in my game. I've set it to 60 but it only goes to ~25fps. This was not an issue before displaying the background (was fine with only win.fill(WHITE)). Here is enough of the code to reproduce:
import os, pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50, 50)
pygame.init()
bg = pygame.image.load('images/bg.jpg')
FPS = pygame.time.Clock()
fps = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
winW = 1227
winH = 700
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_icon(win)
def redraw_window():
#win.fill(WHITE)
win.blit(bg, (0, 0))
win.blit(text_to_screen('FPS: {}'.format(FPS.get_fps()), BLUE), (25, 50))
pygame.display.update()
def text_to_screen(txt, col):
font = pygame.font.SysFont('Comic Sans MS', 25, True)
text = font.render(str(txt), True, col)
return text
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
FPS.tick(fps)
pygame.quit()
Ensure that the background Surface has the same format as the display Surface. Use convert() to create a Surface that has the same pixel format. That should improve the performance, when the background is blit to the display, because the formats are compatible and blit do not have to do an implicit transformation.
bg = pygame.image.load('images/bg.jpg').convert()
Furthermore, it is sufficient to create the font once, rather than every time when a text is drawn. Move font = pygame.font.SysFont('Comic Sans MS', 25, True) to the begin of the application (somewhere after pygame.init() and before the main application loop)
Instead use screen.blit(pygame.image.load(picture.png))
Just image = pygame.image.load(picture.png) then screen.blit(image)
( if you keep loading your pictures continuously it will get lag )