I coded this from a youtbe video and he seemed to do it fine but when I tried I got the error message at the bottom I am so confused and I need help please.
#!/usr/bin/env python import pygame, sys from pygame.locals import * pygame.init() screen = pygame.display.set_mode((370, 572), 0, 32) backgroundfile = "back.png" mousefile = "mouse.png" back = pygame.image.load(backgroundfile).convert() mouse = pygame.image.load(mousefile).convert_alpha() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() #Now we have initialized everything lets start with the main part screen.blit("back.png", (0,0)) pygame.display.flip()
When I run the program I get the error:
Traceback (most recent call last): File "Tutorial 5 First game.py", line 26, in <module> screen.blit("back.png", (0,0)) TypeError: argument 1 must be pygame.Surface, not str
I’m not sure what this means or how to fix
THIS WAS THE FIX
You have two problems. Your first is that you put quotes around
back.png
, making it into a string(str)
instead of a surface(pygame.Surface)
. Your second is that you put a tuple for the second argument instead of a rect(pygame.Rect).
To fix the first, simply put backgroundfile (what you previously saved the surface as) instead of"background.png"
. To fix the second, usebackgroudfile.get_rect()
to get the background’s rect. Your line should be:
screen.blit(backgroundfile, backgroundfile.get_rect()
This by itself will not work because you didn’t previously save backgroundfile as a surface object. Instead of
backgroundfile = "back.png"
put
backgroundfile = pygame.image.load("back.png")
This will return a surface, if"back.png"
is saved as a file in the same folder. Do the same thing with the other loaded image. Do all these things and your program should work.
Advertisement
Answer
You have two problems. Your first is that you put quotes around back.png
, making it into a string (str) instead of a surface (pygame.Surface). Your second is that you put a tuple for the second argument instead of a rect (pygame.Rect). To fix the first, simply put backgroundfile
(what you previously saved the surface as) instead of "background.png"
. To fix the second, use backgroudfile.get_rect()
to get the background’s rect. Your line should be:
screen.blit(backgroundfile, backgroundfile.get_rect()
This by itself will not work because you didn’t previously save backgroundfile as a surface object. Instead of
backgroundfile = "back.png"
put
backgroundfile = pygame.image.load("back.png")
This will return a surface, if “back.png” is saved as a file in the same folder. Do the same thing with the other loaded image. Do all these things and your program should work.