Python 属性错误:';世界';对象没有属性';绘制';

Python 属性错误:';世界';对象没有属性';绘制';,python,Python,所以我正在做一个平板游戏来娱乐。我尝试运行代码,但出现以下错误: “世界”对象没有属性“绘制” 这是我的世界级: class World(): def __init__(self, data): self.tile_list = [] #load images def draw(): dirt_img = pygame.image.load('assets/images/dirt.png') grass_img = pygam

所以我正在做一个平板游戏来娱乐。我尝试运行代码,但出现以下错误:

“世界”对象没有属性“绘制”

这是我的世界级:

class World():
def __init__(self, data):
    self.tile_list = []

    #load images
    def draw():
    

        dirt_img = pygame.image.load('assets/images/dirt.png')
        grass_img = pygame.image.load('assets/images/grass.png')
        row_count = 0
        for row in data:
            col_count = 0
            for tile in row:
                if tile == 1:
                    img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
                if tile == 2:
                    img = pygame.transform.scale(grass_img, (tile_size, tile_size))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
                col_count += 1
            row_count += 1
然后我在这里调用world.draw属性:

world = World(world_data)
world.draw()
此外,如果您想知道,world_数据只是一个列表,其中包含一组数字,告诉代码世界应该是什么样子


请帮帮我,我已经尝试解决这个问题很久了。

问题在于您的压痕。您需要以某种方式更改它们,以便
draw
方法成为
World
类的一部分

class World:
    def __init__(self, data):
        self.tile_list = []
        self.data = data

    #load images
    def draw(self):
        pass
同样,当您这样做时,您需要给这个方法一个
self
参数,因为类中的每个方法都必须有它,除非它是一个

我认为你想做点什么,但你做错了。当您创建类的实例时,将执行放入
\uuuu init\uuu
方法中的任何代码;但是您希望稍后调用
draw
方法。最好的方法是,如我所说,定义
draw
方法,然后需要将
data
参数保存为实例变量,并在需要时将其用作
self.data

for row in self.data:

缩进是否正确?如果此处的缩进与代码匹配,则类的结构不正确,因此它认为
draw
方法是
\uuuuu init\uuuuu
函数的一部分,而该函数根本不属于
世界