Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pygame

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()
like image 547
Laschet Jain Avatar asked Aug 04 '15 14:08

Laschet Jain


People also ask

How do you fill the screen with color in Pygame?

display. set_mode(): This function is used to initialize a screen for display. fill(): This method is used to fill the display with the color specified.

What is pygame display Set_caption?

pygame.display.set_caption. — Set the current window caption.

What does the pygame display Set_mode () function do?

display is a module, and set_mode is a function inside that module. It actually creates an instance of the pygame. Surface class, and returns that.


2 Answers

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()
like image 124
Anand S Kumar Avatar answered Oct 31 '22 18:10

Anand S Kumar


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()
like image 1
Thomas Calverley Avatar answered Oct 31 '22 18:10

Thomas Calverley