我不断收到错误消息“ pygame.error:无法打开racecar.png”

问题描述

我正在尝试放置图片,但它一直在说pygame.error:无法打开racecar.png 我不知道我在做什么错。是我使用的pygame版本,python的版本,我使用的ide的问题还是其他原因? 我使用的pygame版本是pygame 2.0.0 dev6 我使用的python版本是python 3.8.6 我使用的是VS代码 这是我的代码

import pygame


 pygame.init()


 display_width = 800
 display_height = 600

 gamedisplay = pygame.display.set_mode((display_width,display_height))
 pygame.display.set_caption('A bit Racey')

 black = (0,0)
 white = (255,255,255)

 clock = pygame.time.Clock()
 crashed = False
 carImg = pygame.image.load('racecar.png')

 def car(x,y):
     gamedisplay.blit(carImg,(x,y))

 x =  (display_width * 0.45)
 y = (display_height * 0.8)

 while not crashed:
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             crashed = True

     gamedisplay.fill(white)
     car(x,y)

    
     pygame.display.update()
     clock.tick(60)

 pygame.quit()
 quit()

解决方法

“ racecar.png”的路径可能不是本地目录,而是其他目录。

它可能正试图在* .exe所在的文件夹中打开文件,尝试使用完整路径名或相对路径(可能相对于* .exe文件路径),例如:{{ 1}}