Segmentation Fault with opening and closing Pygame WIndows - python

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.

Related

Pygame window will not open

I'm completely new to Python and I just started learning Pygame. I'm trying to work with drawing shapes right now, but I can't get the window to open. Idk what the problem is, here's my code:
`#import libraries
import pygame
import math
#init pygame
pygame.init()
#clock
clock = pygame.time.Clock()
#window screen
screen_height_x = 500
screen_height_y = 400
screen = pygame.display.set_mode((screen_height_x, screen_height_y))
#RGBA colors
Black = (0, 0, 0)
White = (255, 255, 255)
Blue = (0, 0, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
#program
running = True
while running:
for event in pygame.event.get():
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()`
You're immediately quitting the main loop and you also aren't drawing anything. Try this instead:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()
Disclaimer: I cannot try this out at the moment but it should get you started in the right direction.
Is this a class part of the projet or the main class ? If it is the main class, you it should contain a main function.
If this is the main function, you should declare it by adding def main(): before the pygame.init()
And add the module to exectute this as the main script under Quit
if __name__ == "__main__":
main()
Sorry, im french my english is not the best aha !

Pygame in StudioCode on mac wont blit [duplicate]

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()

Pygame not updating screen with pause [duplicate]

This question already has answers here:
Why doesn't PyGame draw in the window before the delay or sleep?
(1 answer)
How to wait some time in pygame?
(3 answers)
Closed 2 years ago.
I was trying to use pygame to create a script that upon clicking run. The window changes the colours of the screen to blue, grey, red with one second delays between them, and then exit out of that loop and then run the game as per normal being the print("cycle done") code. Unfortunately what happens is that the window opens, hangs for around 3 seconds and then shows a red screen, rather than going through each of the colours.
import pygame as pg
running = True
calibration = False
pg.init()
screen = pg.display.set_mode((600, 400))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
timer = 0
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if not calibration:
pg.time.wait(1000)
screen.fill(blue)
pg.display.flip()
pg.time.wait(1000)
screen.fill(green)
pg.display.flip()
pg.time.wait(1000)
screen.fill(red)
pg.display.flip()
calibration = True
print(calibration)
print("cycle done")
clock.tick(60)
If you just wait for some time, you can use pygame.time.wait or pygame.time.delay. However, if you want to display a message and then wait some time, you need to update the display beforehand. The display is updated only if either pygame.display.update() or pygame.display.flip()
is called. See pygame.display.flip():
This will update the contents of the entire display.
Further you've to handles the events with pygame.event.pump(), before the update of the display becomes visible in the window. See pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
This all means that you have to call pygame.display.flip() and pygame.event.pump() before pygame.time.wait():
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if not calibration:
pygame.event.pump()
pg.time.wait(1000)
screen.fill(blue)
pg.display.flip()
pygame.event.pump()
pg.time.wait(1000)
screen.fill(green)
pg.display.flip()
pygame.event.pump()
pg.time.wait(1000)
screen.fill(red)
pg.display.flip()
pygame.event.pump()
pg.time.wait(1000)
calibration = True
print(calibration)
print("cycle done")
clock.tick(60)

Multiple line text in pygame

Just started a Python course at my school, and our first assignment requires us to write out text on multiple lines. I still cannot figure out how to do so with my code so if anyone could help, that'd be great. Code posted below.
import pygame, sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption('font example')
size = [640, 480]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
myfont = pygame.font.SysFont(None, 48)
text = myfont.render('My Name', True, (0, 0, 0), (255, 255, 255))
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery
screen.fill((255, 255, 255))
screen.blit(text, textrect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(20)
simply you can start new line after each print or sys stdout stream output by specify "\n" (ex: print ("hello" + "\n"
then your application will write to diffrent lines

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.

Categories

Resources