【发布时间】:2021-06-07 04:47:05
【问题描述】:
所以我正在开发一款有趣的平台游戏。我尝试运行代码,但出现此错误:
'World' 对象没有属性'draw'
这是我的世界级:
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_data 只是一个包含一堆数字的列表,这些数字告诉代码世界应该是什么样子。
请帮帮我,我一直在努力解决这个问题。
【问题讨论】:
-
你的缩进是否正确?
-
好吧,如果此处的缩进与您的代码匹配,则该类的结构不正确,因此它认为
draw方法是__init__函数的一部分,而不是World的一部分完全上课
标签: python