Python,Pygame,Class对象不再可调用

时间:2016-09-28 10:09:01

标签: python pygame

我正在使用Pygame编写游戏,现在我遇到了这个问题,我想在游戏中为我的怪物添加一个生成计时器。现在计时器工作正常,第一次产生的波浪被制作出来,一切都很棒,非常棒,直到第二次波浪说到:

Monster1 = Monster()
  

TypeError:'Monster'对象不可调用

现在这个我没有得到,因为它在此之前已经全面调用了它。

我经常浏览我的文本只是为了看看我是否正在改变课堂上的任何功能,或者让它不再可以调用,但我只是不知道。

这个循环使得产生怪物

while Game:
    if time.clock() - WaveTimer > 10:
    WaveTimer = time.clock()
        print "The Moon is full"
        for i in range(3*SpawnRate):
            Monster1 = Monster()
            monster_list.add(Monster1)
            character_list.add(Monster1)
            SpawnRate += 1

这是怪物类

class Monster(Character):
    Current_HP = 50
    Max_Hp = 50
    walking_frame = []
    fighting_frame = []
    hit_frame = []
    Fighting = False
    Flag = True
    frame1= 0
    LastFrame1 = 0
    TimeBetweenAtkFrames = 0.1
    X_Speed = 0
    Y_Speed = 0
    damaged = False
    RecordedAni = True
    damagedspeed = 1
    image_file = "C:/Users/Gideon/PycharmProjects/untitled1/Sprite Images/All.png"
    sprite_sheet = SpriteSheet(image_file)
    hitimage = sprite_sheet.get_image(96, 336, 48, 48)
    hitimage = pygame.transform.scale(hitimage, (50, 50))
    for i in range(5):
        image = sprite_sheet.get_image(i * 48, 288, 48, 48)
        fighting_frame.append(image)
    for i in range(4):
        image = sprite_sheet.get_image(i * 48, 144, 48, 48)  # Temp
        walking_frame.append(image)
    for i in range(4):
        image = sprite_sheet.get_image(i * 48, 192, 48, 48)  # Temp
        walking_frame.append(image)
    image = sprite_sheet.get_image(0, 144, 48, 48)
    image = pygame.transform.scale(image, (50, 50))
    orgimage = image
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.Loc_x = random.randrange(20,700)
        self.Loc_y = random.randrange(20,100)  # temp
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = [self.Loc_x, self.Loc_y]
        for i in range (2):
            image = self.sprite_sheet.get_image(i * 48, 336, 48, 48)
            self.hit_frame.append(image)
            i += 1
    def move(self,character):
        if self.Alive:
            if self.Current_HP < self.Max_Hp:
                self.damagedspeed = 2
            if character.Loc_x > self.Loc_x + 25:
                self.X_Speed = 1.5 * self.damagedspeed
                self.Loc_x += self.X_Speed
            if character.Loc_x < self.Loc_x - 25:
                self.X_Speed = 1.5 * self.damagedspeed
                self.Loc_x -= self.X_Speed
            if character.Loc_y > self.Loc_y + 25:
                self.Y_Speed = 1.5 * self.damagedspeed
                self.Loc_y += self.Y_Speed
            if character.Loc_y < self.Loc_y - 25:
                self.Y_Speed = 1.5 * self.damagedspeed
                self.Loc_y -= self.Y_Speed
            if (self.Flag):
                self.Animation()
            wall_hit_list = pygame.sprite.spritecollide(self, CollideList, False)
            for wall in wall_hit_list:
                if self.X_Speed > 0:
                    self.X_Speed = 0
                    self.Loc_x = self.Loc_x - 5
                if self.X_Speed < 0:
                    self.X_Speed = 0
                    self.Loc_x = self.Loc_x + 5
                if self.Y_Speed > 0:
                    self.Y_Speed = 0
                    self.Loc_y = self.Loc_y - 5
                if self.Y_Speed < 0:
                    self.Y_Speed = 0
                    self.Loc_y = self.Loc_y + 5
            self.FightAnimation(character)
            self.image = pygame.transform.rotate(self.orgimage, self.Angle)
            self.rect = self.image.get_rect()
            self.rect.left, self.rect.top = [self.Loc_x, self.Loc_y]
            self.X_Speed = 0
            self.Y_Speed = 0
    def FightAnimation(self,character):
    #      Monster Fight Animation
        if character.Loc_x <= self.Loc_x + 25 and character.Loc_x >= self.Loc_x - 25:
            if character.Loc_y <= self.Loc_y + 25 and character.Loc_y >= self.Loc_y - 25:
                if(self.Fighting == False):
                    self.LastFrame1 = time.clock()
                    self.Fighting = True
                    if (self.frame1 < len(self.fighting_frame)):
                        self.Flag = False
                        self.image = self.fighting_frame[self.frame1]
                        self.image = pygame.transform.scale(self.image, (60, 60))
                        self.orgimage = self.image
                        if (self.frame1 == 3):
                            character.Current_HP -= 20
                        self.frame1 += 1
                    else:
                        self.frame1 = 0
            else:
                if (self.frame1 != 0):
                    self.frame1 = 0
                self.Flag = True
        else:
            if (self.frame1 != 0):
                self.frame1 = 0
                self.image = self.fighting_frame[self.frame1]
                self.image = pygame.transform.scale(self.image, (60, 60))
                self.orgimage = self.image
            self.Flag = True
        if self.Fighting and time.clock() - self.LastFrame1 > self.TimeBetweenAtkFrames:
            self.Fighting = False

如果您还需要了解其他信息,请告诉我,我会添加它 如果这真的很乱我很抱歉这是我第一次使用StackOverFlow网站:$

1 个答案:

答案 0 :(得分:1)

在代码的某处,您“覆盖”名称Monster,然后该名称不再指向您的班级。

确保您在代码中的任何位置都没有Monster = whatever

另请注意,Current_HPMax_Hp等是类成员,而不是实例成员,因此如果您在Current_HP实例上遇到Monster,则可以将其更改为所有 Monster个实例。