Pygame 图片将不再加载
Pygame images won't load anymore
自从我开始这个以来,我已经 char.png 在与我的 .py 文件相同的文件夹中 而不是 一个子文件夹,它会加载我的图像但是当我添加在左右移动的能力中,现在我得到一个错误。
Traceback (most recent call last):File "C:\Users\Shiloh\Google Drive\Python\Platformer\main.py", line 25, in <module>
characterSprite = pygame.image.load('char.png')
pygame.error: Couldn't open char.png
我回去了,删除了我添加的所有内容,因为它正在运行,但它仍然不会再加载图像。我没有移动任何文件,我只是简单地更新了它们。我搜索并尝试了之前在此处给出的答案,但其中 none 似乎适用于我的位置。提前致谢。
import pygame
pygame.init()
#Window Resolution
display_width = 800
display_height = 450
gameResolution = pygame.display.set_mode( (display_width,display_height) )
#Window Title
pygame.display.set_caption('Bubble Bobble')
#Colors
colorRed = (255,0,0)
colorOrange = (255,160,96)
colorYellow = (255,216,0)
colorGreen = (0,255,0)
colorBlue = (0,0,255)
colorPurple = (235,193,255)
colorBlack = (0,0,0)
colorWhite = (255,255,255)
gameClock = pygame.time.Clock()
characterSprite = pygame.image.load('char.png')
def sprite(x,y):
gameResolution.blit(characterSprite,(x,y))
x = (display_width * 0.5)
y = (display_height * 0.5)
x_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
#print(event) #Prints to console
gameResolution.fill(colorWhite)
sprite(x,y)
pygame.display.update() #You can also use pygame.display.flip()
gameClock.tick(60) #Sets the value of the FPS
pygame.quit()
quit()
所以我认为对于 python 3.x,对于 __file__
你必须用引号引起来。所以
mydir = os.path.dirname('__file__')
pygame.image.load(os.path.join(mydir, 'char.png'))
应该可以解决问题。
如果没有,所选答案 here 会提到权限问题。如果您 运行 以 root 身份运行该程序,它将在主目录中启动。这可能是问题所在,并且您在使用绝对路径时没有遇到问题。所以,如果你运行将它作为 root...停止它。
你可以使用这个判断你是否在错误的当前工作目录中
print (os.getcwd())
或当前文件
print (os.path.dirname(os.path.realpath('__file__')))
自从我开始这个以来,我已经 char.png 在与我的 .py 文件相同的文件夹中 而不是 一个子文件夹,它会加载我的图像但是当我添加在左右移动的能力中,现在我得到一个错误。
Traceback (most recent call last):File "C:\Users\Shiloh\Google Drive\Python\Platformer\main.py", line 25, in <module>
characterSprite = pygame.image.load('char.png')
pygame.error: Couldn't open char.png
我回去了,删除了我添加的所有内容,因为它正在运行,但它仍然不会再加载图像。我没有移动任何文件,我只是简单地更新了它们。我搜索并尝试了之前在此处给出的答案,但其中 none 似乎适用于我的位置。提前致谢。
import pygame
pygame.init()
#Window Resolution
display_width = 800
display_height = 450
gameResolution = pygame.display.set_mode( (display_width,display_height) )
#Window Title
pygame.display.set_caption('Bubble Bobble')
#Colors
colorRed = (255,0,0)
colorOrange = (255,160,96)
colorYellow = (255,216,0)
colorGreen = (0,255,0)
colorBlue = (0,0,255)
colorPurple = (235,193,255)
colorBlack = (0,0,0)
colorWhite = (255,255,255)
gameClock = pygame.time.Clock()
characterSprite = pygame.image.load('char.png')
def sprite(x,y):
gameResolution.blit(characterSprite,(x,y))
x = (display_width * 0.5)
y = (display_height * 0.5)
x_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
#print(event) #Prints to console
gameResolution.fill(colorWhite)
sprite(x,y)
pygame.display.update() #You can also use pygame.display.flip()
gameClock.tick(60) #Sets the value of the FPS
pygame.quit()
quit()
所以我认为对于 python 3.x,对于 __file__
你必须用引号引起来。所以
mydir = os.path.dirname('__file__')
pygame.image.load(os.path.join(mydir, 'char.png'))
应该可以解决问题。
如果没有,所选答案 here 会提到权限问题。如果您 运行 以 root 身份运行该程序,它将在主目录中启动。这可能是问题所在,并且您在使用绝对路径时没有遇到问题。所以,如果你运行将它作为 root...停止它。
你可以使用这个判断你是否在错误的当前工作目录中
print (os.getcwd())
或当前文件
print (os.path.dirname(os.path.realpath('__file__')))