在pyqt4 python中创建一个对象

时间:2011-07-21 16:45:26

标签: python object pyqt4 pygame

这是我创建的pygame的代码。我可以像pyqt4一样在pyqt4中创建一个对象吗?我想创建一个具有自己属性的对象数组。或者有更好的创造方式吗?感谢

Main.py:

   comp = pygame.sprite.Group()

   dic = [{"name":"aa","loc":[30,170],"status":0},
           {"name":"bb","loc":[130,170],"status":0},
           {"name":"cc","loc":[230,170],"status":1}]

    for i in range(len(dic)):
        comp.add(PcSprite(dic[i]))

MySprites.py:

class PcSprite(pygame.sprite.Sprite):
    image = None
    pcname = ""
    location =""
    status=""


    def __init__(self,data):
        pygame.sprite.Sprite.__init__(self)
        self.pcname = data['name']
        self.status = data['status']
        self.location = data['loc']

        if self.status ==1:

            PcSprite.image = pygame.image.load("res/images/x.png")
        else:
            PcSprite.image = pygame.image.load("res/images/y.png")

        self.image = PcSprite.image
        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.topleft = self.location

    def clicked(self, target):
        hitbox = self.rect.inflate(-5, -5)
        return hitbox.colliderect(target)

    def hovered(self, target):
        hitbox = self.rect.inflate(-5, -5)
        return hitbox.colliderect(target)

0 个答案:

没有答案