Search code examples
pythonpygamepycharmpygame-surfaceattribution

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


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()

Solution

  • 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!")