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()
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.
pygame.display.set_caption. — Set the current window caption.
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.
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With