我如何实际创建/运行此类?蟒

时间:2016-12-18 19:31:07

标签: python-3.x pygame

我正在制作一个游戏,你可以飞到周围的星系,应该有其他船只飞到某个地方,但我怎么能真正运行呢?让我们说像 X 那么多次。

所有这些什么时候有办法关闭所有船只?

class spaceship(): ###thigs that fly arround when GUI == 4

    def __init__(self,GUIstatus):

        self.shipUp = pygame.image.load("icons/shipUP.png")
        self.shipRight = pygame.image.load("icons/shipRIGHT.png")
        self.shipLeft = pygame.image.load("icons/shipLEFT.png")
        self.shipDown = pygame.image.load("icons/shipDOWN.png")
        self.shipImage = self.shipLeft
        self.shipnpc_x = 0
        self.shipnpc_y = 0
        self.GUIstatus = GUIstatus

        self.shipType = (random.randrange(0,10))
        self.randomspeed = (random.randrange(0,5))
        self.speed = 10 += self.randomspeed

        self.exists = 0

        ##Logic place for selecting and randomizing ship and things

    def Update(self):
            if self.exists == 1:
                return
            if self.exists == 0 and self.GUIstatus == 4:
                if dirrection == LEFT:
                    self.shipnpc_x +=selfspeed
                    self.shipImage = self.shipLeft

                if dirrection == RIGHT:
                    self.shipnpc_x -=selfspeed
                    self.shipImage = self.shipRight

                if dirrection == UP:
                    self.shipnpc_y -=self.speed
                    self.shipImage = self.shipUp

                if dirrection == DOWN:
                    self.npcship_y +=self.speed
                    self.shipImage = self.shipDown


    def Draw(self,screen):
            if  self.exists == 1:
                return
            screen.blit(self.shipImage,(self.shipnpc_x,self.shipnpc_y))

1 个答案:

答案 0 :(得分:1)

别担心,这实际上是初学者面临的一个非常普遍的问题。

你想要做的是将你的对象放在某种列表中(在python中,你通常使用[]),而不是在单个对象上调用方法,比如

ship1.Update()

使用循环迭代该列表,例如

for ship in ships:
    ship.Update()

但是因为你正在使用pygame,所以它更容易,因为有一些类可以为你做一些繁重的工作。

您应该首先使用SpriteGroup课程,让我们更改您的spaceship课程。注意评论

class Spaceship(pygame.sprite.Sprite): ###thigs that fly arround when GUI == 4

    def __init__(self, ships, GUIstatus):

        # ships is a Group for all ships 
        pygame.sprite.Sprite.__init__(self, ships)
        self.shipUp = ...

        # the image has to be in self.image
        self.image = self.shipLeft

        # no need for shipnpc_x and shipnpc_y, since we store
        # our position in self.rect
        self.rect = self.image.get_rect()
        self.GUIstatus = GUIstatus

        self.shipType = ...

    # this has to be called update instead of update
    # to work with groups
    def update(self):

        # to change the position, we just alter self.rect

        if self.exists == 0 and self.GUIstatus == 4:
            if dirrection == LEFT:
                self.rect.move_ip(-self.speed, 0)
                self.shipImage = self.shipLeft

            if dirrection == RIGHT:
                self.rect.move_ip(self.speed, 0)
                self.shipImage = self.shipRight

            if dirrection == UP:
                self.rect.move_ip(0, -self.speed)
                self.shipImage = self.shipUp

            if dirrection == DOWN:
                self.rect.move_ip(0, self.speed)
                self.shipImage = self.shipDown

    # no need for the Draw function, since it's handled
    # by the sprite class' draw function

现在进行这些更改后,我们只需要创建一个Group

ships = pygame.sprite.Group()

因此,无论何时创建宇宙飞船,只需将组传递给初始化程序,即可将其放入舰船组。如果你想要30艘船,只需使用一个循环:

for _ in xrange(30):
    Spaceship(ships, GUIstatus)

至于更新和绘图,只需致电

ships.update()
ships.draw(screen)
在您的主循环中

Group类负责在其中的每个update上调用drawSprite(基本上它只是在列表中迭代for就像我上面写的那样。)

就是这样!如果你想摆脱Spaceship,只需要调用它上面的kill函数(可能在更新方法中),或者使用像pygame.sprite.groupcollide之类的东西来自动完成它。 / p>

最后,您可能希望拥有多个群组,例如你在主循环中调用updatedraw的所有内容的一组,如果你创建一个射击游戏,一组用于所有敌人,一组用于你的子弹,所以你可以使用{ {3}}用于碰撞检测等......