my code gives me a tuple error and i dont know why

Question:

MAIN CODE

  1. i dont really know why i keep on getting tuple errors the code looks fine
  2. its just the loop part of my game for now, this is it
        import pygame 
    
    
    # important window variables
    WIDTH, HEIGHT = 900, 500
    
    WIN = WIDTH, HEIGHT
    
    SCREEN = pygame.display.set_mode((WIN))
    
    pygame.display.set_caption('SPACE GAME')
    
    WHITE = (0,0,0)
    
    # display function (what shows up on the screen)
    def display():
        WIN.fill(WHITE)
        pygame.display.update()
    
    
    # main function/loop function
    def main():
        run = True
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
    
            display()
        pygame.quit()
    
    
    if __name__ == '__main__':
        main()


Asked By: nabeel ahmed

||

Answers:

I’m confident this is the issue (though without the real error traceback, I could be wrong.)

The basic issue is that you set the value of WIN as a tuple
i.e.
WIN = BASE, HEIGHT

Then in the display() function, you do the following:

  WIN.fill(WHITE)

Which is what I think the error is. WIN being a tuple,
has no fill method. What I think you wanted to use is
SCREEN.fill(WHITE)

I took a gander at the documentation (as I don’t know pygame),
and it confirms that: pygame.display.set_mode((WIN))
returns a Surface object which does have a fill()
method.

So, in conclusion:


    # important window variables
    WIDTH, HEIGHT = 900, 500
    
    WIN = WIDTH, HEIGHT
    
    SCREEN = pygame.display.set_mode((WIN))
    
    pygame.display.set_caption('SPACE GAME')
    
    WHITE = (0,0,0)
    
    # display function (what shows up on the screen)
    def display():
        SCREEN.fill(WHITE)
        pygame.display.update()
    
    
    # main function/loop function
    def main():
        run = True
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
    
            display()
        pygame.quit()
    
    
    if __name__ == '__main__':
        main()
Answered By: ewong
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.