Laschet Jain
Laschet Jain

Reputation: 744

Pygame screen.fill() not filling up the color properly

I've just started learning Pygame . I'm following this tutorial. I ran the following program but it shows black color instead of blue :

import pygame

h = input("Enter the height of the window : ")
w = input("Enter the width of the window : ")
screen = pygame.display.set_mode((w,h))

running = True
while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running=0

screen.fill((0,0,1))
pygame.display.flip()

Upvotes: 10

Views: 59368

Answers (2)

Thomas Calverley
Thomas Calverley

Reputation: 31

It shows black because you're running the screen.fill() after your game loop. This means it will only fill the screen after you're done playing. Best practice is to put it somewhere inside the loop, and then any sprites you want to draw should be done after - so that if the sprite moves, its old position will be colored over by the background.

Also, as mentioned in other answers: (0,0,1) is black with the barest hint of blue. A good rgb colour-picker can be found by googling "rgb color picker".

Finally, you should change out "event=pygame.event.poll()" to be "for event in pygame.event.get()" as shown, as this will allow it to detect multiple events simultaneously. Unnecessary for this example, obviously, but I assume it wouldn't be left as a blue screen that can only be closed.

import pygame

pygame.init()

h = input("Enter the height of the window : ")
w = input("Enter the width of the window : ")
screen = pygame.display.set_mode((w,h))

running = True
while running:
    for event in pygame.event.poll():
        if event.type == pygame.QUIT:
            running=0
        #any additional event checks
    screen.fill((0,0,255))
    #any additional drawings
    pygame.display.flip()

pygame.quit()

Upvotes: 3

Anand S Kumar
Anand S Kumar

Reputation: 90889

For Blue color, you should use 255 as the third element in the tuple, not 1.

Example -

while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running=0
    screen.fill((0,0,255))
    pygame.display.flip()

Upvotes: 9

Related Questions