【问题标题】:Not sure how to display sprite不知道如何显示精灵
【发布时间】:2017-06-13 22:25:23
【问题描述】:

我正在尝试为我的游戏生成一个敌人,并且有人告诉我更新我的精灵组可以做到这一点。由于某种原因,敌人根本不会产生。没有得到任何错误或任何东西。这是我的课:

class Spawn(pygame.sprite.Sprite):
    def __init__(self,primaryx,primaryy):
        pygame.sprite.Sprite.__init__(self)
        global directionM
        self.directionM=directionM
        x1=random.randint(100,400)
        y1=random.randint(100,400)
        self.x1=x1
        self.y1=y1
        self.primaryx=primaryx
        self.primaryy=primaryy
    def AIPrototype(self):
        minionup=pygame.image.load("Alien.png").convert_alpha()
        miniondown=pygame.image.load("Aliendown.png").convert_alpha()
        minionleft=pygame.image.load("Alienleft.png").convert_alpha()
        minionright=pygame.image.load("Alienright.png").convert_alpha()
        global x,y,posx,posy
        seperate=random.randint(1,1000)
        screen.blit(self.directionM,(self.primaryx,self.primaryy))
        if seperate==2:
            self.primaryx=x+100
        if seperate==20:
            self.primaryx=x-100
        if seperate==150:
            self.primaryy=y+100
        if seperate==200:
            self.primaryy=y-100
        self.x1=self.primaryx
        self.y1=self.primaryy
        if self.x1<x:
            xspeed1=1
            slopex1=x-self.x1
        if self.x1>x:
            xspeed1=-1
            slopex1=self.x1-x
        if self.y1<y:
            yspeed1=1
            slopey1=y-self.y1
        if self.y1>y:
            yspeed1=-1
            slopey1=self.y1-y       
    #
        hypo1=((slopex1**2)+(slopey1**2))**0.5
        speedmark1=hypo1/1
        speedy1=slopey1/speedmark1
        speedx1=slopex1/speedmark1
        movex1=speedx1
        movey1=speedy1
        if self.x1<=640 and self.x1>=0:
            if self.x1>x:
                self.x1+=xspeed1*movex1
                if self.x1<x:
                    xspeed1=0
        if self.y1<=480 and self.x1>=0:
            if self.y1>y:
                self.y1+=yspeed1*movey1
                if self.y1<y:
                    yspeed1=0
        if self.x1<=640 and self.x1>=0:
            if self.x1<x:
                self.x1+=xspeed1*movex1
                if self.x1>x:
                    xspeed1=0
        if self.y1<=480 and self.x1>=0:
            if self.y1<y:
                self.y1+=yspeed1*movey1
                if self.y1>y:
                    yspeed1=0
    #
        if self.x1>640:
            self.x1=640
        if self.x1<0:
            self.x1=0
        if self.y1>480:
            self.y1=480
        if self.y1<0:
            self.y1=0
        if self.y1>=posy-20 and self.y1<=posy+20 and self.x1>=x-20 and self.x1<=x+20:
            Spawn.kill()
        self.primaryx=self.x1
        self.primaryy=self.y1

这就是我所说的一切:

spritegroup = pygame.sprite.Group()
spawn = Spawn(600,200)
spritegroup.add(spawn)
clock = pygame.time.Clock() 
keepGoing = True        

try:
    while keepGoing:
        clock.tick(60) 
        screen.fill(THECOLORS['red'])
        char()#start
        x+1
        posxlist.append(x)
        posylist.append(y)
        spritegroup.update()
        spritegroup.draw(screen)
        pygame.display.flip()

如何显示/调用精灵?我为我的混乱和低效的代码道歉。我是使用类和精灵的新手。

【问题讨论】:

    标签: python-3.x class pygame sprite


    【解决方案1】:

    调用spritegroup.update() 不会产生新的精灵,它会调用所有包含的精灵的update 方法。由于您的 Spawn sprite 子类没有 update 方法,spritegroup.update() 实际上对您的程序没有影响。

    您可以通过创建一个实例并将其添加到 sprite 组来生成新的 sprite,就像您在此处所做的那样:

    spawn = Spawn(600, 200)
    spritegroup.add(spawn)
    

    【讨论】:

    • 更多关于精灵和精灵组的信息可以在Program Arcade Games找到(上一章是关于类的)。
    • 但我在代码中这样做了,并没有生成任何精灵。对吗?
    • 你的精灵类还需要一个self.image(一个pygame.Surface)和一个self.rect(一个pygame.Rect)属性才能正常工作。我忽略了这一点,因为您的示例不完整,我无法对其进行测试。您应该编辑问题中的代码并提供minimal, complete and verifiable example。并阅读我上面链接的教程,因为它会教你基础知识。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2014-12-20
    相关资源
    最近更新 更多