Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame: how to change background color [duplicate]

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([800,600])
white = [255, 255, 255]
red = [255, 0, 0]
screen.fill(white)
pygame.display.set_caption("My program")
pygame.display.flip()



background = input("What color would you like?: ")
if background == "red":
    screen.fill(red)

running = True
while running:
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
        running = False
        pygame.quit()

I'm trying to ask the user what background color he would like to have. If the user writes red, the color doesn't change and still stays white.

like image 283
user7307944 Avatar asked Dec 16 '16 17:12

user7307944


People also ask

How do I change the background color in Python?

Right-click the upper-left corner of the Python console window and select Properties. In the dialog box that appears, pick the tab labeled Colors. On it you can set the screen background and text color.

What is pygame display Flip ()?

pygame.display.flip. — Update the full display Surface to the screen.

How do I change the hue in pygame?

The hue of each pixel in an image can be shifted using PixelArray to iterate over each pixel, Surface. unmap_rgb to get a Color object from each pixel, and Color. hsla to do the hue shift.


2 Answers

It will redraw as red the next time you update the display. Add pygame.display.update():

background = input("What color would you like?: ")
if background == "red":
    screen.fill(red)
    pygame.display.update()

Or, you could move the pygame.display.flip() to after you (conditionally) change the background color.

See also Difference between pygame.display.update and pygame.display.flip

like image 60
e0k Avatar answered Oct 20 '22 11:10

e0k


In fact, screen.fill(red) changes the color of the pixels in the Surface object screen. You need to update the display after changing the color.
Note, however, that you should only update the display once at the end of the application loop. Multiple updates to the display per frame cause flickering. See also Why is the PyGame animation is flickering.

backcolor = white
if background == "red":
    backcolor = red

running = True
while running:
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
        running = False

    # clear background
    screen.fill(backcolor)

    # draw scene
    # [...]

    # update display
    pygame.display.flip() 

Explanation:

You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().

See pygame.display.flip():

This will update the contents of the entire display.

While pygame.display.flip() will update the contents of the entire display, pygame.display.update() allows updating only a portion of the screen to updated, instead of the entire area. pygame.display.update() is an optimized version of pygame.display.flip() for software displays, but doesn't work for hardware accelerated displays.

like image 2
Rabbid76 Avatar answered Oct 20 '22 12:10

Rabbid76