10
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.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
user7307944
  • 125
  • 1
  • 1
  • 8
  • 1
    Please include a runnable sample. What you've pasted is not syntactically valid Python, and will error on run. – mwchase Dec 16 '16 at 18:01
  • There appears to be a transcription error at the end of your code, as the indentation is incorrect (after `if i.type == pygame.QUIT:`). – e0k Dec 16 '16 at 18:11
  • 1
    pygame draws in buffer and `pygame.display.flip()` sends buffer on monitor. – furas Dec 16 '16 at 19:32
  • Please include proper indentation to avoid errors when running on lines 21 and 22, as they require one more indent to run properly. – MilkyWay90 Aug 21 '18 at 01:43

3 Answers3

16

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

Community
  • 1
  • 1
e0k
  • 6,961
  • 2
  • 23
  • 30
2

Create a variable to store the current color :

currentColor = (255,255,255) # or 'white', since you created that value

background = input("What color would you like?: ")
if background == "red":
    currentColor = red # The current color is now red

in the loop:

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

    screen.fill(currentColor) # Fill the screen with whatever the stored color is. 

    pygame.display.update() # Refresh the screen, needed whatever the color is, so don't remove this

So now, when you need to recolor the screen, just change currentColor to whatever you need, and the screen will automatically turn that color. Example :

if foo:
    currentColor = (145, 254, 222)
elif bar:
    currentColor = (215, 100, 91)

BTW, I think it is better to store color as a tuple instead of a list, like red = (255, 0, 0)

Also, you don't need pygame.display.update (or flip) anywhere else than in the loop. What this function does it just take the latest shape/value of every drawn item and pushes it to the screen, so you only need it as the last item in your loop, so it displays everything.

underscoreC
  • 729
  • 6
  • 13
2

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174