my code gives me a tuple error and i dont know why
Question:
MAIN CODE
- i dont really know why i keep on getting tuple errors the code looks fine
- 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()
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()
MAIN CODE
- i dont really know why i keep on getting tuple errors the code looks fine
- 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()
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()