【问题标题】:I keep getting the error pygame.error: Unsupported image format, im using a png and I can't find any info on this我不断收到错误 pygame.error: Unsupported image format, im using a png and I can't find any information on this
【发布时间】:2021-09-16 13:05:34
【问题描述】:

我一直在寻找解决此问题的方法,但找不到任何可行的方法,我一直在使用本教程 https://www.youtube.com/watch?v=FfWpgLFMI7w 大约 22 分钟,他添加了我的代码的字符

import pygame

pygame.init()

#create screen
screen = pygame.display.set_mode((800, 600))

#title and iconp
pygame.display.set_caption("Game")

#player
Player = pygame.image.load('Ship.png')
playerX = 370
playerY = 480

def player():
    screen.blit(playerImg, (playerX, playerY))

running = True
while running:

    screen.fill((0, 128, 155))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player()
    pygame.display.update()

我的错误是 pygame.error: Unsupported image format。如果您能提供帮助,我们将不胜感激!

【问题讨论】:

  • 也许 Ship.png 实际上不是一个有效的 PNG 文件——我们没有任何方法可以判断。其他软件可以查看吗?
  • 您在使用 M1 Mac 吗?你是如何安装 pygame 的?
  • 我使用的是 Windows 10,我可以在油漆中查看船 png,但仅此而已,如果有帮助,它是 1.21kb 64x64 和 24 位颜色

标签: python pygame


【解决方案1】:

看! playerImg 已被定义,所以我认为您使用 Player 而不是 playerImg 并告诉我发生了什么

【讨论】:

  • 我不太清楚你的意思,它一直给我第 12 行的错误,我没有改变关于如何定义东西来修复它。
  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

您为变量使用不同的名称,Player playerImg

在加载图像和使用图像时使用相同的名称:

playerImg= pygame.image.load('Ship.png')
playerX = 370
playerY = 480

def player():
    screen.blit(playerImg, (playerX, playerY))

或者给player函数添加参数:

Player = pygame.image.load('Ship.png')
playerX = 370
playerY = 480

def player(playerImg, x, y):
    screen.blit(playerImg, (x, y))

running = True
while running:

    # [...]

    player(Player, playerX, playerY)

    # [...]

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2021-03-25
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2014-05-28
    • 2019-11-13
    相关资源
    最近更新 更多