I can has colour? Link to heading
No, you can’t has colour.
…
At least not yet.
To be able to draw inside the window, we need to be able to call functions on the window.
Remember how we defined the window with pygame.display.set_mode()
?
Let’s put that window into a variable, like display_surf = pygame.display.set_mode()
.
Now we can use the .fill(<COLOUR>)
command to set the whole screen to one colour.
For now we will specify colour as a tuple of RGB-values, like (255,255,255)
.
We can set the color anytime after initializing the window, however if we are going draw moving stuff over top of the colour later, it makes sense to set the color right at the beginning of our loop, to have it act as background.
import pygame
pygame.init()
display_surf = pygame.display.set_mode((800,600), pygame.HWSURFACE | pygame.DOUBLEBUF)
while(True):
display_surf.fill((255,255,255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
You might notice, that the windows doesn’t close quite properly yet.
It seems to throw an error right after closing.
The reason for that is the way we try to quit the program.
For now we just quit pygame, but leave python running, which allows the loop to start over and throw an error.
We can circumvent that by changing the way we define the Loop.
Instead of True, we will now use a variable to store the state of the program.
Instead of calling pygame.quit()
, we simply set the state to False, break the Loop and call pygame.quit()
after the loop ends.
import pygame
is_running = True
pygame.init()
display_surf = pygame.display.set_mode((800,600), pygame.HWSURFACE | pygame.DOUBLEBUF)
while(is_running):
display_surf.fill((255,255,255))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
pygame.quit()
Now we’ve got a white window, which closes properly.
Next up we’ll take a deeper look at drawing in our window.
In the mean time, you could try building a disco window using the random
-module
and random.randint(start, stop)
.