Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting: pygame.error: Parameter 'surface' is invalid #192

Open
jhutar opened this issue Sep 16, 2024 · 0 comments
Open

Getting: pygame.error: Parameter 'surface' is invalid #192

jhutar opened this issue Sep 16, 2024 · 0 comments

Comments

@jhutar
Copy link

jhutar commented Sep 16, 2024

Hello and thank you for great library! Not sure what am I doing wrong, but I'm getting this traceback:

Traceback (most recent call last):
  File "/home/jhutar/Checkouts/CatWars/./reproducer.py", line 8, in <module>
    tmxdata = pytmx.util_pygame.load_pygame("catwars/assets/tileset/CatWars-level1.tmx")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jhutar/.local/lib/python3.12/site-packages/pytmx/util_pygame.py", line 183, in load_pygame
    return pytmx.TiledMap(filename, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jhutar/.local/lib/python3.12/site-packages/pytmx/pytmx.py", line 549, in __init__
    self.parse_xml(ElementTree.parse(self.filename).getroot())
  File "/home/jhutar/.local/lib/python3.12/site-packages/pytmx/pytmx.py", line 633, in parse_xml
    self.reload_images()
  File "/home/jhutar/.local/lib/python3.12/site-packages/pytmx/pytmx.py", line 686, in reload_images
    self.images[gid] = loader(rect, flags)
                       ^^^^^^^^^^^^^^^^^^^
  File "/home/jhutar/.local/lib/python3.12/site-packages/pytmx/util_pygame.py", line 146, in load_image
    tile = smart_convert(tile, colorkey, pixelalpha)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jhutar/.local/lib/python3.12/site-packages/pytmx/util_pygame.py", line 102, in smart_convert
    tile = original.convert()
           ^^^^^^^^^^^^^^^^^^
pygame.error: Parameter 'surface' is invalid

Code I'm using is:

#!/usr/bin/env python
import pygame
import pytmx
import sys

pygame.init()

tmxdata = pytmx.util_pygame.load_pygame("catwars/assets/tileset/CatWars-level1.tmx")
world_size = (
    tmxdata.width * tmxdata.tilewidth,
    tmxdata.height * tmxdata.tileheight,
)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(world_size)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    for layer in tmxdata:
        if layer.name == "ground":
            for tile in layer.tiles():
                x_top_left = tile[0] * tmxdata.tilewidth
                y_top_left = tile[1] * tmxdata.tileheight
                screen.blit(tile[2], (x_top_left, y_top_left))

    pygame.display.update()

    clock.tick(30)

It is possible to overcome that error if I do this workaround:

$ diff -u ~/.local/lib/python3.12/site-packages/pytmx/util_pygame.py{.ORIG,}
--- .../.local/lib/python3.12/site-packages/pytmx/util_pygame.py.ORIG	2024-09-16 21:27:27.803974724 +0200
+++ .../.local/lib/python3.12/site-packages/pytmx/util_pygame.py	2024-09-16 21:29:08.561690006 +0200
@@ -99,7 +99,10 @@
 
         # there are no transparent pixels in the image
         if px == tile_size[0] * tile_size[1]:
-            tile = original.convert()
+            try:
+                tile = original.convert()
+            except pygame.error:
+                tile = original
 
         # there are transparent pixels, and set for perpixel alpha
         elif pixelalpha:

tmx and png data are here:

Versions I'm using:

$ python --version
Python 3.12.5
$ rpm -q python3-pygame
python3-pygame-2.6.0-1.fc40.x86_64
$ pip freeze | grep -i pytmx
PyTMX==3.32

What am I doing wrong please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant