首页 > 解决方案 > Pygame/Pyscroll:TypeError:源对象必须是表面

问题描述

所以,我有一个“MapClass.py”文件,里面有一个“Map”数据类和一个“MapManager”类:

import dataclasses
import pygame
import pytmx
import pyscroll

@dataclasses.dataclass
class Map:
    name: str
    walls: list[pygame.Rect]
    group: pyscroll.PyscrollGroup

class MapManager:

    def __init__(self, screen, player):
        self.maps = dict()
        self.current_map = "playtest"
        self.screen = screen
        self.player = player

        self.load_map("playtest")

    def load_map(self, name):
        tmx_data = pytmx.util_pygame.load_pygame(f"assets/maps/{name}.tmx")
        map_data = pyscroll.data.TiledMapData(tmx_data)
        map_layer = pyscroll.BufferedRenderer(
            map_data,
            self.screen.get_size(),
        )
        map_layer.zoom = 3

        walls = []
        for object in tmx_data.objects:
            if object.type == "collision":
                walls.append(pygame.Rect(object.x, object.y, object.width, object.height))

        group = pyscroll.PyscrollGroup(map_layer=map_layer, default_layer=5)
        group.add(self.player)

        map = Map(name, walls, group)
        self.maps[name] = map

    def get_currentmap(self): return self.maps[self.current_map]

    def get_group(self): return self.get_currentmap().group

    def get_walls(self): return self.get_currentmap().walls

但是,当我启动它时,它给了我这个错误:

  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\MapClass.py", line 20, in __init__
    self.load_map("playtest")
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\MapClass.py", line 25, in load_map
    map_layer = pyscroll.BufferedRenderer(
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\venv\lib\site-packages\pyscroll\orthographic.py", line 73, in __init__
    self.set_size(size)
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\venv\lib\site-packages\pyscroll\orthographic.py", line 228, in set_size
    self._initialize_buffers(buffer_size)
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\venv\lib\site-packages\pyscroll\orthographic.py", line 550, in _initialize_buffers
    self.redraw_tiles(self._buffer)
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\venv\lib\site-packages\pyscroll\orthographic.py", line 242, in redraw_tiles
    self._flush_tile_queue(surface)
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\venv\lib\site-packages\pyscroll\orthographic.py", line 567, in _flush_tile_queue
    surface.blits(blit_list, doreturn=False)
  TypeError: Source objects must be a surface
  

我似乎无法理解为什么会这样?是我还是模块?因为我似乎在这里看不到问题..我认为问题是因为这个:

map_layer = pyscroll.BufferedRenderer(
                map_data,
                self.screen.get_size(),
            )

(第 25-28 行)非常感谢!

标签: pythonpygamepygame-surfacepytmx

解决方案


推荐阅读