Skip to content
Advertisement

Pygame how to change background colour without erasing anything else

I’m making this drawing thing in Python with Pygame. I have functions that draw circles and rectangles, but I want to add a function that lets me change the background colour, while not erasing the other circles and rectangles. Here is the code for the rectangles:

JavaScript
JavaScript

A function for it would be best, but any solution would work.

Advertisement

Answer

You can’t. You have to redraw the entire scene in each frame.

Create classes for the shapes. For instance RectShape

JavaScript

Add the shapes to a list. Clear the background with the color you want, draw all the shapes in the list, and refresh the display in every frame:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement