【问题标题】:Attribute Error in a class - python - on pycharm [duplicate]类中的属性错误-python-在pycharm上[重复]
【发布时间】:2020-12-29 15:30:46
【问题描述】:

以下是错误

pygame 2.0.0(SDL 2.0.12,python 3.7.7)

Traceback (most recent call last):
 File "C:/Users/T######/PycharmProjects/pythonProject1/env.py", line 54, in < module >
   game_over, score = game.play_step()
 File "C:/Users/T######/PycharmProjects/pythonProject1/env.py", line 46, in play_step
   return game_over, self.score
AttributeError: 'SpaceInvaders' object has no attribute 'score'**

SpaceInvaders 类中,虽然它有 score 属性 -

class SpaceInvaders:

    def __int__(self, w=640, h=480):

        self.w = w
        self.h = h
        # init display
        self.display = pygame.display.set_mode((self.w, self.h))
        pygame.display.set_caption("SpaceInvaders")
        self.clock = pygame.time.Clock()
        # init game state
        self.direction = Direction.RIGHT

        self.head = Point[self.w/2, self.h/2]
        self.score = 0
    
    def play_step(self):
        game_over = False
        return game_over, self.score

这个问题有什么解决办法吗?

【问题讨论】:

标签: python


【解决方案1】:

它是__init__ 而不是__int__。 所以你的代码应该是这样的:

def __init__(self, w=640, h=480):
    ...
    self.score = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-19
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2018-01-08
    相关资源
    最近更新 更多