【问题标题】:Pygame graphics error: Couldn't open blue.pngPygame 图形错误:无法打开 blue.png
【发布时间】:2020-04-26 18:08:00
【问题描述】:

我一直在学习 Pygame 中的图形,我试图对背景进行 blit,但是当我运行我的程序时,它给了我这个错误:

pygame.error: Couldn't open blue.png
the png and the python file are in the same folder on my desktop.

我的代码如下所示:

from os import path

background = pygame.image.load('blue.png')
background_rect = background.get_rect()

screen.blit(background, background_rect)
screen.blit(background, background_rect)
all_sprites.draw(screen)
pygame.display.flip()
screen.fill(BLACK)

【问题讨论】:

  • 应该可以 - 你能仔细检查路径、文件名和文件权限吗?或者尝试图像的绝对路径?
  • 谢谢,我仔细检查了路径,我忘记了文件夹

标签: python graphics pygame


【解决方案1】:

这不起作用,因为'blue.png' 只是一个字符串;你必须使用path.join("NAME OF BASE DIRECTORY", "blue.png")。这将告诉 pygame 在哪里查找名称为 "blue.png" 的文件。有几种方法可以使它运行得更快:调用pygame.image.load(path.join("BASE DIR", "blue.png")).convert() 这会将图像与窗口的格式相同,从而使渲染速度更快,您会在屏幕上闪烁两次,这毫无意义并且会减慢时间,如果您不使用 OpenGL 只需使用 pygame.update(),因为它稍微快一些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2019-02-25
    • 2016-05-25
    相关资源
    最近更新 更多