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.
Related
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 does my text not appear? I am trying to make a program in which the text is shown above the image. In the program, I still see the image, but not the text.
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((800, 600))
image1 = pygame.image.load('frog.gif')
image2 = pygame.image.load('frog.jpg')
pygame.display.set_caption('\'Dancing frog\'')
image1 = pygame.transform.scale(image1, (800, 800))
image2 = pygame.transform.scale(image2, (800, 800))
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render('this should appear', True, (255, 255, 255))
screen.blit(text, [10, 10])
run = True
while run:
screen.blit(image1, (0, -200))
pygame.display.update()
time.sleep(0.2)
screen.blit(image2, (0, -200))
pygame.display.update()
time.sleep(0.2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit() ```
You must blit the text Surface after the bbackgorund image:
screen.blit(image1, (0, -200))
screen.blit(text, [10, 10])
pygame.display.update()
This question already has an answer here:
Why is my pygame application loop not working properly?
(1 answer)
Closed 2 years ago.
I cant blit anything to my pygame screen using studio code on my mac. Is this a known issue or is there a way to fix it I am overlooking? I'm not getting any errors it's just not doing anything. I am kinda new to pygame so anything could work. here's my code:
pygame.display.set_caption('The space Simulator')
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
screen = pygame.display.set_mode([1000, 1000])
background = pygame.Surface((1000,1000))
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = background.get_rect().centerx
running=True
while running:
screen.blit(image, (textpos))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
You're just not flushing / updating the drawing primitives to the screen. Really you just need a pygame.display.update() or pygame.display.flip() after all your blits are done.
I guess you removed parts of your code to keep it simple for the question, but I put them back in for a working answer.
I also re-arranged the code, and removed the creation of the background Surface just to get the centre co-ordinate. This operation can be performed on the existing screen Surface.
import pygame
red=(255, 0, 0)
white=(255, 255, 255)
black=(0, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
pygame.init()
pygame.display.set_caption('The space Simulator')
screen = pygame.display.set_mode([1000, 1000])
#image = pygame.image.load(r'/Users/Mr.Penguin280/Desktop/Photos/Logo.jpg')
image = pygame.image.load('background.png' ).convert()
image = pygame.transform.smoothscale( image, (1000,1000) )
#background = pygame.Surface((1000,1000))
myfont = pygame.font.SysFont( None, 24 )
text1 = myfont.render('WELCOME TO MY SIMULATOR.', True, red)
textpos = text1.get_rect()
textpos.centerx = screen.get_rect().centerx
running=True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(image, (0,0))
screen.blit(text1, (textpos))
pygame.display.flip()
pygame.quit()
I'm working on a project where I am trying to alternate between the camera preview on a picamera, and some text on screen using pygame windows. I have gotten to the point where I can open the picamera, then some text, then the picamera again, but when I try to open a pygame window for more text, I get a segmentation fault.
I think the main problem is quitting the pygame window, without quitting the other things I need to open up another pygame window. The commands like sys.exit, and pygame.quit seem to quit things to much. I've tried alternatives like putting the text into a while loop and then making the loop false at the end so that it closes the window without an actual quit command, but that didn't seem to close anything really. The code works perfectly up until the second time I try to initialize pygame. That's when it gives me the segmentation fault and opens a new window in my python idle with a whole bunch of other code that I didn't write.
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption(' ')
camera()
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('You', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
time.sleep(1)
pygame.quit()
camera()
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption(' ')
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('test', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
time.sleep(1)
What I would like to be able to do is switch between picamera and text several more times, so if I can figure out how to fix it this one time, then I just have to copy and paste the code a bit more to get the next iterations. I'm brand new to coding.
Ok. so I made a silly mistake. The problem is, for some reason, initializing pygame twice; which I still don't understand since pygame.quit() should have quit it I think. But I just removed the second pygame.init() and replaced the first pygame.quit() with pygame.display.quit() instead.
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()