TypeError: argument 1 must be pygame.Surface, not str , doesnt work
Question:
This is the code ->
import pygame
import os
pygame.init()
width,height = (500,700)
bg=os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
WIN = pygame.display.set_mode((width,height))
def draw():
WIN.blit(bg,(0,0))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
draw()
main()
i have no clue why the error shows up, my previous projects have been working with this way.
Answers:
bg
is a string, containing the filename, but not an pygame.Surface
object. Use pygame.image.load
to load an image from a file source:
bbg_filename = os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
bg = pygame.image.load(bg_filename)
Scale the background to the size of the window:
bg = pygame.transform.scale(bg, (width, height)).convert()
Update the display by calling either pygame.display.update()
or pygame.display.flip()
after drawing the scene:
draw()
pygame.display.flip()
Complete example:
import pygame
import os
pygame.init()
width,height = (500,700)
WIN = pygame.display.set_mode((width,height))
bg_filename = os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
bg = pygame.image.load(bg_filename)
bg = pygame.transform.scale(bg, (width, height)).convert()
def draw():
WIN.blit(bg,(0,0))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
draw()
pygame.display.flip()
main()
This is the code ->
import pygame
import os
pygame.init()
width,height = (500,700)
bg=os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
WIN = pygame.display.set_mode((width,height))
def draw():
WIN.blit(bg,(0,0))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
draw()
main()
i have no clue why the error shows up, my previous projects have been working with this way.
bg
is a string, containing the filename, but not an pygame.Surface
object. Use pygame.image.load
to load an image from a file source:
bbg_filename = os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
bg = pygame.image.load(bg_filename)
Scale the background to the size of the window:
bg = pygame.transform.scale(bg, (width, height)).convert()
Update the display by calling either pygame.display.update()
or pygame.display.flip()
after drawing the scene:
draw()
pygame.display.flip()
Complete example:
import pygame
import os
pygame.init()
width,height = (500,700)
WIN = pygame.display.set_mode((width,height))
bg_filename = os.path.join('C:\Users\USER\Desktop\Python\picture match','background.jpg')
bg = pygame.image.load(bg_filename)
bg = pygame.transform.scale(bg, (width, height)).convert()
def draw():
WIN.blit(bg,(0,0))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
draw()
pygame.display.flip()
main()