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()
Related
I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6
i keep having this error and i dont know how to fix it:
TypeError: invalid destination position for blit
i am trying to build like a game of tag with 2 players on one screen and i need the window to show who is 'it'
here is my code:
import pygame as pyg
import pygame.gfxdraw
import time
# Screen
pygame.init()
screen = pyg.display.set_mode((1280, 720), pygame.RESIZABLE)
width,height= screen.get_size()
pygame_icon = pyg.image.load('icon.png')
pyg.display.set_icon(pygame_icon)
pyg.display.set_caption("taggy")
clock = pyg.time.Clock()
black = (0,0,0)
background = black
tikker = 0
PlayerNames = ['Player 1', 'Player 2']
test_font = pyg.font.SysFont("Comic Sans MS", 50)
text = pyg.surface, test_font.render(PlayerNames[0], True, black)
while True:
for event in pyg.event.get():
if event.type == pyg.QUIT:
quit()
# Code
screen.fill((background))
screen.blit(screen, text, (width/2), 50)
pyg.display.flip()
clock.tick(60)
Does anyone knows how to fix it?
i use vscode and python 3.9.9
Thanks for your time!
:]
I think I found a solution. Function blit request mandatory items: pygame.suface and position so something like this: screen.blit(picture, (x, y)). You don't need to enter the screen because it is already in the command. The program thinks that screen is your image and the text is position. So you need just delete the screen from the function, so replace screen.blit(screen, text, (width/2), 50)to screen.blit( text, (width/2), 50) I hope it helpled.
Full good code:
import pygame as pyg
import pygame.gfxdraw
import time
import sys
# Screen
pygame.init()
pygame.font.init()
screen = pyg.display.set_mode((1280, 720), pygame.RESIZABLE)
width,height= screen.get_size()
pyg.display.set_caption("taggy")
clock = pyg.time.Clock()
black = (0,0,0)
white = (255, 255, 255)
background = black
tikker = 0
PlayerNames = ['Player 1', 'Player 2']
test_font = pyg.font.SysFont("Comic Sans MS", 50)
text = test_font.render(PlayerNames[0], True, white)
while True:
for event in pyg.event.get():
if event.type == pyg.QUIT:
pygame.quit()
sys.exit()
# Code
screen.fill((background))
screen.blit(text, (800, 600))
pyg.display.flip()
clock.tick(60)
I edited cordinates of text a little bit you may will need to change it. If you will use width and height as cordinates the text will go out of screen.
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
This question already has answers here:
How to load colorful emojis in pygame?
(2 answers)
Closed last year.
How do I render a emoji in a string in python 3.6 using pygame(font.render(), font.freetype/font.font)? If not possible anymore, I need a code for render in Pillow Draw.text(). I would be grateful.
Discard this: (Filling this question of nothing because looks like my post is mostly code, so I'm adding some nothings to enable the post)
# -*- coding: UTF-8 -*-
import pygame, emoji
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False
pygame.font.init()
font = pygame.freetype.SysFont("segoe-ui-symbol.ttf", size=50)
#font = pygame.font.SysFont("segoe-ui-symbol.ttf", 72)
font_color = (255,255,255, 255)
font_background = (0,0,0, 0)
text_string = emoji.emojize('😖')
#text_string = u'ujiHelloÁpQ|, World 👍😖'
#text_string = u'ujiHelloÁpQ|\U0001f44d, World 😖'
#print(emoji.emojize('Python is :thumbs_up_sign:'))
#text = font.render("♛", True, font_color, (0,0))
#text = font.render(text_string, True, font_color, (0,0))
text = font.render(text_string, fgcolor=font_color, size=0)
image_size = list(text[0].get_size())
text_image = pygame.Surface(image_size)
text_image.fill(font_background)
pygame.display.flip()
text_image.blit(text[0], (0,0))
#print(dir(text_image))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
screen.fill((0, 0, 0))
#screen.blit(text,
screen.blit(text_image,
(320 - text[0].get_width() // 2, 240 - text[0].get_height() // 2))
pygame.display.flip()
clock.tick(60)
I see two possible problems here.
First, if nothing is showing up or the app seems to stall, then Pygame cannot find the SysFont. This was happening on Mac, because there is no segoe-ui-symbol.ttf as system font. As such, I downloaded a version from the internet and created a normal freetype.Font from it.
It works fine.
The second problem may be that the font itself doesn't have those characters in it. Sometimes the OS replaces characters that a font cannot render with a different font.
I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6