做什么? __init __()需要3个参数(给定1个)?

时间:2014-05-29 21:55:18

标签: python arguments init

这是我给出的错误以及指定的代码行。为过去的帖子道歉。

Traceback (most recent call last):
  File "H:\Users\Daniel\Desktop\Final Project\Gold Hunter.py", line 352, in <module>
    main()
  File "H:\Users\Daniel\Desktop\Final Project\Gold Hunter.py", line 346, in main
    score = game()
  File "H:\Users\Daniel\Desktop\Final Project\Gold Hunter.py", line 195, in game
    pirate = Pirate()
TypeError: __init__() takes exactly 3 arguments (1 given)

实际代码

  main() Line 352
  score = game() Line 346
  pirate = Pirate() Line 195

盗版构造函数它给出了错误NameError:全局名称&#39; dx&#39;未定义

class Pirate(pygame.sprite.Sprite):

    EAST = 0

    def __init__(self, screen, dx):
        self.screen = screen
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("king_pirate/running e0000.bmp")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        self.rect.inflate_ip(-50, -30)
        self.rect.center = (0, random.randrange(30,450))
        self.img = []

        self.loadPics()
        self.frame = 0
        self.delay = 4
        self.pause = self.delay
        self.dx = dx

1 个答案:

答案 0 :(得分:2)

我同意李的评论。我们看不到class Pirate:构造函数,但很明显你的__init__函数在定义时会使用三个参数。这些是self, argument1, argument2,所以当您在第195行调用pirate = Pirate()时,您必须实际提供2个参数(它将自己获得self)。您需要为它在构造函数中定义的argument1和argument2。发布构造函数以获取更多帮助。您的第195行应该类似于pirate = Pirate(argument1, argument2)

祝你好运!