Nicolas F
Nicolas F

Reputation: 515

Pygame python3 error (AttributeError: 'pygame.Surface' object attribute 'fill' is read-only)

I'm working on the snake game using pygame and python3, and I'm getting this error:

Traceback (most recent call last):
  File "snakegame.py", line 39, in <module>
    main()
  File "snakegame.py", line 36, in main
    redrawWindow(win)
  File "snakegame.py", line 18, in redrawWindow

    surface.fill = ((0,0,0))
AttributeError: 'pygame.Surface' object attribute 'fill' is read-only

I'm following a youtube video: https://www.youtube.com/watch?v=CD4qAhfFuLo and he is able to run the program without errors, but I can't. Hopefully you can help, thank you! Here is my program:

import pygame

def drawGrid(w, rows, surface):
    sizeBtnw = w // rows

    x = 0
    y = 0

    for l in range(rows):
        x = x + sizeBtnw
        y = y + sizeBtnw

        pygame.draw.line(surface, (255, 255), (x, 0), (x, w))
        pygame.draw.line(surface, (255, 255), (0, y), (w, y))

def redrawWindow(surface):
    global rows, width
    surface.fill = ((0,0,0))
    drawGrid(width, rows, surface)
    pygame.display.update()


def main():
    global width, rows
    width = 500
    rows = 20
    win = pygame.display.set_mode((width, width))
    #s = snake((255, 0, 0), (10, 10))
    flag = True;

    clock = pygame.time.Clock()

    while flag == True:
        pygame.time.delay(50)
        clock.tick(10)
        redrawWindow(win)
    pass

main()

Upvotes: 0

Views: 355

Answers (1)

Putnam
Putnam

Reputation: 714

fill is a function, not a value--you want surface.fill((0,0,0)) rather than surface.fill = ((0,0,0))

Upvotes: 2

Related Questions