用非正统的角色类在pygame中分层精灵?

时间:2014-08-11 21:23:28

标签: python pygame sprite

预警这是我关于stackoverflow的第一篇文章,如果我错误地格式化了这篇文章,或者我不清楚,我很高兴听到你的建设性批评。谢谢你的理解!!我下次会做得更好!!

SO: 我试图创建一个游戏,我的角色在屏幕上四处奔跑,类似于Earthbound。我无法正确分层我的精灵。我希望我的控制角色出现在其他物体后面,当他在后面时#34;他们在前面"在前面"时出现在前面。

我查看了很多关于精灵分层的帖子,我在LayeredUpdates小组中找到了一些有用的信息,并且只是在“画家”的角度来看画像。在我的游戏中创造深度的幻觉。

我的问题可能在于我构建我的角色分类的方式..

def __init__(self,file_path,x,y,rate=3):
    pygame.sprite.Sprite.__init__(self)
    self.rate=rate

    token_path = SpriteSheet('token.png')
    self.image = token_path.get_image(0,0,RECT_WIDTH,RECT_HEIGHT)
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y+(48-RECT_HEIGHT)

我使用透视令牌图像作为我的rect对象(用于碰撞检测),然后根据我的令牌矩形位置将所有pyganim动画blit到屏幕...就像这样:

def draw(self,surface):
    surface.blit(self.image,(self.rect.x,self.rect.y))
    if self.moveUp or self.moveDown or self.moveLeft or self.moveRight:
        self.moveConductor.play()
        if self.direction == UP:
            self.animObjs['walk_up'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == DOWN:
            self.animObjs['walk_down'].blit(surface,(self.rect.x,self.rect.y-36))
        etc.....

当我将这些精灵添加到我的组时,它没有添加实际的动画,它添加了清晰的标记,所以当涉及到分层时,我最终会创建一个很好的清晰标记堆栈而不是分层精灵显示

我通过绘图顺序进行分层的尝试可以很好地使用每个组中的一个精灵:

def layer(self,dudes,surface):
    for dude in dudes:
        if self.rect.bottom>dude.rect.y+5:
            dude.draw(surface)
            self.draw(surface)
        else:
            self.draw(surface)
            dude.draw(surface)

但是当我向我的迭代组添加更多精灵时,他们不能正确分层。

有没有办法按照我构建我的角色类的方式实现分层系统?

这是我的sprite.Group代码,以防它相关:

visibles = pygame.sprite.LayeredUpdates()
prot = pygame.sprite.LayeredUpdates()

prot.add(nes)

visibles.add(aloysius)
visibles.add(bluecapboy)
visibles.add(applekid)

这是我的完整字符代码:

class Main_Character(pygame.sprite.DirtySprite):
layer=1
moveUp=moveDown=moveLeft=moveRight=False
moveUp_left=moveUp_right=moveDown_left=moveDown_right = False
direction = DOWN
animObjs={}

def __init__(self,file_path,x,y,rate=3):
    pygame.sprite.Sprite.__init__(self)
    self.rate=rate

    token_path = SpriteSheet('token.png')
    self.image = token_path.get_image(0,0,RECT_WIDTH,RECT_HEIGHT)
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y+(48-RECT_HEIGHT)

    path = SpriteSheet(file_path)

    self.down1 = pygame.transform.scale((path.get_image(0,0,16,24)),(32,48))
    self.down2 = pygame.transform.scale((path.get_image(16,0,16,24)),(32,48))
    self.up1 = pygame.transform.scale((path.get_image(32,0,16,24)),(32,48))
    self.up2 = pygame.transform.flip(self.up1,True,False)
    self.left1 = pygame.transform.scale((path.get_image(48,0,16,24)),(32,48))
    self.left2 = pygame.transform.scale((path.get_image(64,0,16,24)),(32,48))
    self.right1 = pygame.transform.flip(self.left1,True,False)
    self.right2 = pygame.transform.flip(self.left2,True,False)

    self.up_left1 = pygame.transform.scale((path.get_image(32,24,16,24)),(32,48))
    self.up_left2 = pygame.transform.scale((path.get_image(48,24,16,24)),(32,48))
    self.up_right1 = pygame.transform.flip(self.up_left1,True,False)
    self.up_right2 = pygame.transform.flip(self.up_left2,True,False)
    self.down_left1 = pygame.transform.scale((path.get_image(0,24,16,24)),(32,48))
    self.down_left2 = pygame.transform.scale((path.get_image(16,24,16,24)),(32,48))
    self.down_right1 = pygame.transform.flip(self.down_left1,True,False)
    self.down_right2 = pygame.transform.flip(self.down_left2,True,False)


    self.animObjs['walk_down']=pyganim.PygAnimation([(self.down1,.1),(self.down2,.1)])
    self.animObjs['walk_up']=pyganim.PygAnimation([(self.up1,.1),(self.up2,.1)])
    self.animObjs['walk_left']=pyganim.PygAnimation([(self.left2,.1),(self.left1,.1)])
    self.animObjs['walk_right']=pyganim.PygAnimation([(self.right2,.1),(self.right1,.1)])
    self.animObjs['walk_up_left']=pyganim.PygAnimation([(self.up_left1,.1),(self.up_left2,.1)])
    self.animObjs['walk_up_right']=pyganim.PygAnimation([(self.up_right1,.1),(self.up_right2,.1)])
    self.animObjs['walk_down_left']=pyganim.PygAnimation([(self.down_left1,.1),(self.down_left2,.1)])
    self.animObjs['walk_down_right']=pyganim.PygAnimation([(self.down_right1,.1),(self.down_right2,.1)])

    self.moveConductor = pyganim.PygConductor(self.animObjs)


def go_up(self):
    self.moveUp = True
    self.moveDown = False
    if self.moveLeft:
        self.direction = UP_LEFT
    elif self.moveRight:
        self.direction = UP_RIGHT
    if not self.moveLeft and not self.moveRight:
        self.direction = UP

def go_left(self):
    self.moveLeft = True
    self.moveRight = False
    if self.moveUp:
        self.direction = UP_LEFT
    elif self.moveDown:
        self.direction = DOWN_LEFT
    elif not self.moveUp and not self.moveDown:
        self.direction = LEFT

def go_down(self):
    self.moveDown = True
    self.moveUp = False
    if self.moveLeft:
        self.direction = DOWN_LEFT
    elif self.moveRight:
        self.direction = DOWN_RIGHT
    if not self.moveLeft and not self.moveRight:
        self.direction = DOWN

def go_right(self):
    self.moveRight=True
    self.moveLeft = False
    if self.moveUp and self.moveRight:
        self.direction = UP_RIGHT
    elif self.moveDown and self.moveRight:
        self.direction = DOWN_RIGHT
    elif not self.moveUp and not self.moveDown:
        self.direction = RIGHT


def stop_up(self):
    self.moveUp = False
    if self.moveLeft:
        self.direction = LEFT
    if self.moveRight:
        self.direction = RIGHT

def stop_left(self):
    self.moveLeft = False
    if self.moveUp:
        self.direction = UP
    if self.moveDown:
        self.direction = DOWN

def stop_down(self):
    self.moveDown = False
    if self.moveLeft:
        self.direction = LEFT
    if self.moveRight:
        self.direction = RIGHT

def stop_right(self):
    self.moveRight = False
    if self.moveUp:
        self.direction = UP
    if self.moveDown:
        self.direction = DOWN

def draw(self,surface):
    surface.blit(self.image,(self.rect.x,self.rect.y))
    if self.moveUp or self.moveDown or self.moveLeft or self.moveRight:
        self.moveConductor.play()
        if self.direction == UP:
            self.animObjs['walk_up'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == DOWN:
            self.animObjs['walk_down'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == LEFT:
            self.animObjs['walk_left'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == RIGHT:
            self.animObjs['walk_right'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == UP_LEFT:
            self.animObjs['walk_up_left'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == UP_RIGHT:
            self.animObjs['walk_up_right'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == DOWN_LEFT:
            self.animObjs['walk_down_left'].blit(surface,(self.rect.x,self.rect.y-36))
        if self.direction == DOWN_RIGHT:
            self.animObjs['walk_down_right'].blit(surface,(self.rect.x,self.rect.y-36))


    else:
        self.moveConductor.stop()
        if self.direction == UP:
            surface.blit(self.up1,(self.rect.x,self.rect.y-36))
        elif self.direction == DOWN:
            surface.blit(self.down1,(self.rect.x,self.rect.y-36))
        elif self.direction == LEFT:
            surface.blit(self.left1,(self.rect.x,self.rect.y-36))
        elif self.direction == RIGHT:
            surface.blit(self.right1,(self.rect.x,self.rect.y-36))
        elif self.direction == UP_LEFT:
            surface.blit(self.up_left1,(self.rect.x,self.rect.y-36))
        elif self.direction == UP_RIGHT:
            surface.blit(self.up_right1,(self.rect.x,self.rect.y-36))
        elif self.direction == DOWN_LEFT:
            surface.blit(self.down_left1,(self.rect.x,self.rect.y-36))
        elif self.direction == DOWN_RIGHT:
            surface.blit(self.down_right1,(self.rect.x,self.rect.y-36))
def update(self):
    if self.moveUp: self.rect.y-=self.rate
    if self.moveDown: self.rect.y+=self.rate
    if self.moveLeft: self.rect.x-=self.rate
    if self.moveRight: self.rect.x+=self.rate

def collision(self,dudes):
    for dude in dudes:
        if self.rect.colliderect(dude):
            if self.direction==RIGHT and self.rect.right < dude.rect.left+5:
                self.rect.x -= self.rate
            if self.direction==LEFT and self.rect.left > dude.rect.right-5:
                self.rect.x += self.rate
            if self.direction==DOWN and self.rect.bottom < dude.rect.top+5:
                self.rect.y -=self.rate
            if self.direction==UP and self.rect.top > dude.rect.bottom-5:
                self.rect.y += self.rate
            if self.direction==DOWN_RIGHT and self.rect.bottom<dude.rect.top+5 or self.direction==DOWN_RIGHT and self.rect.right<dude.rect.left+5:
                self.rect.x-=self.rate
                self.rect.y-=self.rate
            if self.direction==DOWN_LEFT and self.rect.bottom<dude.rect.top+5 or self.direction == DOWN_LEFT  and self.rect.left>dude.rect.right-5:
                self.rect.x+=self.rate
                self.rect.y-=self.rate
            if self.direction==UP_RIGHT and self.rect.top < dude.rect.bottom+5 or self.direction==UP_RIGHT and self.rect.right<dude.rect.left+5:
                self.rect.x-=self.rate
                self.rect.y+=self.rate
            if self.direction==UP_LEFT and self.rect.top < dude.rect.bottom+5 or self.direction==UP_LEFT and self.rect.left>dude.rect.right+5:
                self.rect.x+=self.rate
                self.rect.y+=self.rate

def layer(self,dudes,surface):
    for dude in dudes:
        if self.rect.bottom>dude.rect.y+5:
            dude.draw(surface)
            self.draw(surface)
        else:
            self.draw(surface)
            dude.draw(surface)

感谢您阅读!

0 个答案:

没有答案