Pygame_实例图片rect和.add添加到列表失败

时间:2018-11-09 00:11:50

标签: python pygame add rect

我猜是pygame的另一个奇怪的情况。我想创建一个放置类,该类将是statusdrops(健康,装甲等...)和武器drops(增加伤害,切换武器等...)的父类。我需要这样做,因为这两个子函数几乎具有相同的功能,但是由于某种原因,我的继承失败了,我检查了python文档,仍然不确定发生了什么。

这是使用3个类的第一次尝试,给我这个错误:'StatusDrops'对象没有属性'rect'。但是我为外星人使用了完全相同的方法,到目前为止效果很好

丢弃父类

class Drops(pygame.sprite.Sprite):
def __init__(self, dw, dh, x, y):
    super().__init__()

    self.display_width = dw
    self.display_height = dh
    self.posx = x
    self.posy = y
    self.speedx = 3
    self.speedy = 5
    self.pickedUp = 0

#Set Rotation
def setRotation(self):
    self.rotationx = self.rotationy = random.randrange(-1, 1)
    if self.rotationx == 0:
        self.setRotation()

#Set both side's rotation chance
def setRotationChance(self):
    self.rotationxChance = random.randrange(1, 151)
    self.rotationyChance = random.randrange(1, 151)

#Change rotation side
def changeRotation(self):
    #Side steps rotation
    if self.rotationxChance == 1:
        if self.rotationx == 1:
            self.rotationx = -1
        else:
            self.rotationx = 1

    #Forward steps rotation
    if self.rotationyChance == 1:
        if self.rotationy == 1:
            self.rotationy = -1
        else:
            self.rotationy = 1

#Movement           
def move(self):
    self.rect.x += (self.rotationx * self.speedx)
    self.rect.y += (self.rotationy * self.speedy)

#Kill if picked up or went of the boundaries
def checkStatus(self):
    if self.pickedUp == 1:
        self.kill()

    if self.rect.x > self.display_width or self.width < 0:
        self.kill()
    elif self.rect.y < 0 or self.rect.y > self.display_height:
        self.kill()

StatusDrop子类(由于出现错误,我没有继续使用awerDrop)

class StatusDrops(Drops):
def _init__(self, dw, dh, x, y):
    super().__init__(dw, dh, x, y)

    self.type = self.setType()
    self.setAttributes(self)
    self.rect = self.image.get_rect(center = (self.posx, self.posy))
    self.image.set_colorkey(YELLOW)

    self.setRotation()


def setType(self):
    x = 1#random.randrange(1, 5)
    return x

#Set attributes, what kind of status will upgrade(health, stamina, tenacity, nano shell)
def setAttributes(self):
    #Hero increase's stamina(max health)
    if self.type == 1:
        self.image = staminaIcon
        self.width = 28
        self.height = 23
    #Hero heal, max heal income = 30% of stamina
    elif self.type == 2:
        self.image = healIcon
        self.width = 28
        self.height = 23
    #hero increases resistance to slow and stun(lower duration) up to 30%
    elif self.type == 3:
        self.image = tenacityIcon
        self.width = 28
        self.height = 23
    #hero increases resistance to incoming damage(armor) up to 50%
    elif self.type == 4:
        self.image = nanoshellIcon
        self.width = 28
        self.height = 22

    self.image = staminaIcon
    self.width = 28
    self.height = 23

def update(self):       
    #Movement
    self.move()       
    #Call both sides rotation chances
    self.setRotationChance()
    self.changeRotation()
    #Check drop status
    self.checkStatus()

那之后(我什至在函数上用super()替换了self,但仍然无法正常工作)为了修复它,我选择不使用父类,而是像两个不同的类一样创建它们。这解决了缺少属性rect的问题,但是当我尝试生成它们(在外星人死亡后)时,.add()打印了此错误:

文件“ / home / marios / Documents / Programming-TEI / Python_Spyder / Graphics / Game / Star Guardian / Star_Guardian.py”,行1210,在game_Loop中     statusDrop = StatusDrops(display_width,display_height,(private.rect.x +(private.width / 2)),(private.rect.y +(private.height / 2)))

init 中的文件“ /usr/local/lib/python3.6/dist-packages/pygame/sprite.py”,第124行     self.add(* groups)

添加文件“ /usr/local/lib/python3.6/dist-packages/pygame/sprite.py”,第142行     self.add(* group)

TypeError:*之后的add()参数必须是可迭代的,而不是int

这是rect错误修复的新Status类

class StatusDrops(pygame.sprite.Sprite):
def _init__(self, dw, dh, x, y):
    super().__init__()

    self.display_width = dw
    self.display_height = dh
    self.posx = x
    self.posy = y
    self.speedx = 3
    self.speedy = 5
    self.pickedUp = 0
    self.type = self.setType()
    self.setAttributes(self)
    self.rect = self.image.get_rect(center = (self.posx, self.posy))
    self.image.set_colorkey(YELLOW)

    self.setRotation()


def setType(self):
    x = 1#random.randrange(1, 5)
    return x

#Set attributes, what kind of status will upgrade(health, stamina, tenacity, nano shell)
def setAttributes(self):
    #Hero increase's stamina(max health)
    if self.type == 1:
        self.image = staminaIcon
        self.width = 28
        self.height = 23
    #Hero heal, max heal income = 30% of stamina
    elif self.type == 2:
        self.image = healIcon
        self.width = 28
        self.height = 23
    #hero increases resistance to slow and stun(lower duration) up to 30%
    elif self.type == 3:
        self.image = tenacityIcon
        self.width = 28
        self.height = 23
    #hero increases resistance to incoming damage(armor) up to 50%
    elif self.type == 4:
        self.image = nanoshellIcon
        self.width = 28
        self.height = 22

    self.image = staminaIcon
    self.width = 28
    self.height = 23

def update(self):       
    #Movement
    self.move()       
    #Call both sides rotation chances
    self.setRotationChance()
    self.changeRotation()
    #Check drop status
    self.checkStatus()

#Set Rotation
def setRotation(self):
    self.rotationx = self.rotationy = random.randrange(-1, 1)
    if self.rotationx == 0:
        self.setRotation()

#Set both side's rotation chance
def setRotationChance(self):
    self.rotationxChance = random.randrange(1, 151)
    self.rotationyChance = random.randrange(1, 151)

#Change rotation side
def changeRotation(self):
    #Side steps rotation
    if self.rotationxChance == 1:
        if self.rotationx == 1:
            self.rotationx = -1
        else:
            self.rotationx = 1

    #Forward steps rotation
    if self.rotationyChance == 1:
        if self.rotationy == 1:
            self.rotationy = -1
        else:
            self.rotationy = 1

#Movement           
def move(self):
    self.rect.x += (self.rotationx * self.speedx)
    self.rect.y += (self.rotationy * self.speedy)

这里出现.add()错误(我对导弹和外星人使用相同的方法生成并起作用)

all_sprites_list = pygame.sprite.Group()
status_drops_list = pygame.sprite.Group()

statusDrop = StatusDrops(display_width, display_height, (private.rect.x + (private.width / 2)), (private.rect.y + (private.height / 2)))
                all_sprites_list.add(statusDrop)
                status_drops_list.add(statusDrop)

谢谢您的时间,如果您再需要我,请问我!

0 个答案:

没有答案