Pygame : AttributeError: 'NoneType' object has no attribute 'fill'

1.6k Views Asked by At

So,This was my code,i was coding normally by watching a tutorial but suddenly when i used the fill attribution, an error popped up saying the following :

line 15, in display.fill((25, 25, 29)) AttributeError: 'NoneType' object has no attribute 'fill'

And under is the code,that i wrote,if anybody willingly helped me then,i would be very happy!

Under Bellow is my code

import pygame

pygame.init()

pygame.display.set_mode((800, 600))

display = pygame.display.set_caption("Space Invaders!")

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

display.fill((25, 25, 29))
pygame.display.update()
2

There are 2 best solutions below

1
On BEST ANSWER

While I haven't got pygame so I can't test the code, I strongly suspect your issue is related to these three lines and how they relate to each other:

pygame.display.set_mode((800, 600))

display = pygame.display.set_caption("Space Invaders!")

display.fill((25, 25, 29))

You have set the display mode and now you want to fill it. However, you haven't actually assigned the output of display.set_mode() to display, you've assigned the output of display.set_caption() - which, as someone else has already commented, is nothing as display.set_caption() doesn't return a value.

So, when you try to use display, it doesn't contain anything.

Consider trying the following code instead (though I don't know if the order is important):

display = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Space Invaders!")
6
On

I suspect that pygame failed to initialize. This propogated to:

display = pygame.display.set_caption("Space Invaders!")

Returning 'NoneType' object which finally fails when you run:

display.fill((25, 25, 29))

use a breakpoint at "display =..." to see the return value.

after looking further....it's syntax/formatting related. Here are my corrections to get it going:

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
display = pygame.display.set_caption("Space Invaders!")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        screen.fill((25, 25, 29))
        pygame.display.update()