Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a window in pygame without displaying it

I create a screen in pygame using:

screen = pygame.display.set_mode((width,height))

Then I draw 200 random gray scale circles on it and save the screen as an image. (I need to save this screen as an image so I can compare it to a target image later on):

pygame.image.save(screen, "Circles.png")

Now, I need to randomly change different parameters of different circles by putting the numpy array that contains the circle parameters for all the circles in a loop with a big number as its iteration value.

And I need to redraw the mutated circles on a screen (at the end of each iteration) and save that screen as a png image again.

Now the problem is, every time I use pygame.display.set_mode((width,height)) it opens up the display window and it significantly slows down my program.

I would like to create a screen and save it in a variable but don't need to display that screen at each iteration. I haven't been able to figure out what command to use to avoid displaying the screen.

I appreciate your help.

like image 754
Mary Avatar asked Oct 15 '25 13:10

Mary


1 Answers

If you don't want a window to appear, try creating the surface like this:

screen = pygame.Surface((width,height))

Then you can draw your circles on this surface and save it the same way as you did before. As others have said, clear the surface with the fill() method.

like image 122
user2746752 Avatar answered Oct 18 '25 09:10

user2746752



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!