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.
Related
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.
This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I'm fairly new to pymunk and I wanted to make physics engine for my future games, there's just a small problem, My code won't draw the circle that pygame is trying to visualize. Here's my code.
import pygame
import pymunk
import sys
def create_item(space):
body = pymunk.Body(1, 100, body_type = pymunk.Body.DYNAMIC)
body.position = (450, 50)
shape = pymunk.Circle(body, 80)
space.add(body, shape)
return shape
def draw_items(items):
for item in items:
pygame.draw.circle(screen, item_color, item.body.position, 80)
def quit():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def display_update():
screen.fill(bg_color)
clock.tick(FPS)
space.step(1/60)
pygame.display.flip()
# Constructor
pygame.init()
# Constants and Variables
WIDTH = 900
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
clock = pygame.time.Clock()
# Colors
bg_color = 30, 30, 40
item_color = 200, 200, 200
# Pymunk Variables
space = pymunk.Space()
space.gravity = (0, 500)
items = []
items.append(create_item(space))
# Loops
def main():
running = True
while running:
quit()
display_update()
draw_items(items)
main()
I don't really know what the problem is here, it doesn't give me an error or something like that and I only get a clean blank canvas with my bg color.(also sorry for the bad comments)
You have to draw the objects before updating the display
def main():
running = True
while running:
quit()
screen.fill(bg_color)
draw_items(items)
pygame.display.flip()
clock.tick(FPS)
space.step(1/60)
You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage
I am currently following a beginner pygame tutorial on YouTube here but even though I copied the code exactly from the tutorial my pygame window only stays open for about a second and then closes.
note: someone asked this question about three years ago here but it didn't fix my problem.
my code is below
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption('hello world')
Your script is ending and so pygame closes everything.
You have to create a loop in order for your game to continue running, with a condition to exit the loop.
You also need to initialize the display with pygame.display.init()
import pygame
pygame.init()
pygame.display.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption('hello world')
clock = pygame.time.Clock()
FPS = 60 # Frames per second.
# Some shortcuts for colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# For example, display a white rect
rect = pygame.Rect((0, 0), (32, 32))
image = pygame.Surface((32, 32))
image.fill(WHITE)
# Game loop
while True:
# Ensure game runs at a constant speed
clock.tick(FPS)
# 1. Handle events
for event in pygame.event.get():
# User pressed the close button ?
if event.type == pygame.QUIT:
quit()
# Close the program. Other methods like 'raise SystemExit' or 'sys.exit()'.
# Calling 'pygame.quit()' won't close the program! It will just uninitialize the modules.
# 2. Put updates to the game logic here
# 3. Render
win.fill(BLACK) # first clear the screen
win.blit(image, rect) # draw whatever you need
pygame.display.flip() # copy to the screen
I'm making a game analyser, and I thought it would be nice if I had a user iterface instead of just using text and raw input to communicate. I am having problems with 'blitting' an image to my screen.
My image is inside the pycharm file called 'CATANYLISER' as is my code.
import pygame
pygame.init()
# py-game variables
(width, height) = (1000, 600)
window = pygame.display.set_mode((width, height))
window_title = pygame.display.set_caption('the CATANALYSER')
does_quit = False
# py-game images
empty_board = pygame.image.load('empty_board.png')
# py-game window loop
while not does_quit:
# receives input from window
for event in pygame.event.get():
# stops program when red x clicked
if event.type == pygame.QUIT:
does_quit = True
window.blit(empty_board, (0, 0))
pygame.display.update()
# activates when the loop finishes, just makes sure everything shuts down properly
pygame.quit()
The expected result is the image on the screen in the top left corner. However when I run the program, I have an empty screen (pygame.QUIT still works).
When I run this code there is no error message, and I am completely lost about how to fix this.
first, make sure that the empty_board.png is in your working directory.
Second, you have to clear the screen before each frame using window.fill([255, 255, 255])
Finally, you could try using pygame.display.flip() instead of update()
My code would look like:
import pygame
pygame.init()
window = pygame.display.set_mode([640, 480])
doQuit = False
board = pygame.image.load("empty_board.png")
while not doQuit:
window.fill([255, 255, 255])
window.blit(board, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
doQuit = True
pygame.quit()
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()