如何在另一张图片前面制作播放器的图像?

时间:2014-03-05 10:33:03

标签: python windows image pygame

我有这个代码运行正常,但唯一的问题是播放器的图像落后于其他图像,所以你无法看到播放器的位置。如何制作它以使玩家的图像像背景图层一样位于该图像的前面。如何在其他背景图像后面显示其他图像。比如我有这个背景树:

class Tree(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('tree.png')

        self.rect = self.image.get_rect()

我有玩家的班级:

class Player(pygame.sprite.Sprite):
    change_x = 0
    change_y = 0
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('mapMover.png')

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def changespeed_x(self,x):
        self.change_x = x

    def changespeed_y(self,y):
        self.change_y = y

    def update(self, barrier_list, Upcar_list, Downcar_list):
        self.rect.x += self.change_x

        barrier_hit_list = pygame.sprite.spritecollide(self, barrier_list, False)
        Upcar_hit_list = pygame.sprite.spritecollide(self, Upcar_list, False)
        Downcar_hit_list = pygame.sprite.spritecollide(self, Downcar_list, False)

        for barrier in barrier_hit_list:
            if self.change_x > 0:
                self.rect.right = barrier.rect.left
            else:
                self.rect.left = barrier.rect.right

        for Upcar in Upcar_hit_list:
            if self.change_x > 0:
                self.rect.right = Upcar.rect.left
                Upcar.rect.x += 3
            else:
                self.rect.left = Upcar.rect.right
                Upcar.rect.x -= 3
            return

        for Downcar in Downcar_hit_list:
            if self.change_x > 0:
                self.rect.right = Downcar.rect.left
                Downcar.rect.x += 3
            else:
                self.rect.left = Downcar.rect.right
                Downcar.rect.x -= 3
            return

        self.rect.y += self.change_y

        barrier_hit_list = pygame.sprite.spritecollide(self, barrier_list, False)
        Upcar_hit_list = pygame.sprite.spritecollide(self, Upcar_list, False)
        Downcar_hit_list = pygame.sprite.spritecollide(self, Downcar_list, False)

        for barrier in barrier_hit_list:
            if self.change_y > 0:
                self.rect.bottom = barrier.rect.top
            else:
                self.rect.top = barrier.rect.bottom

        for Upcar in Upcar_hit_list:
            if self.change_y > 0:
                self.rect.top = Upcar.rect.bottom
                Upcar.rect.y += 3
            else:
                self.rect.bottom = Upcar.rect.top
                Upcar.rect.x -= 3
            return

        for Downcar in Downcar_hit_list:
            if self.change_y > 0:
                self.rect.top = Downcar.rect.bottom
                Downcar.rect.y += 3
            else:
                self.rect.bottom = Downcar.rect.top
                Downcar.rect.x -= 3
            return

Python 2.6,Pygame Sprites,Windows 7

2 个答案:

答案 0 :(得分:4)

您没有显示相关代码,但问题很明显:

你在屏幕上的最后一个Surface是“在顶部”。如果您首先绘制播放器精灵,然后在其上绘制背景图像,则只会显示背景图像。

因此,您必须以正确的顺序绘制精灵,或者,因为您使用Sprite类,但是您的精灵会加入LayeredUpdates组,并为其提供_layer属性

答案 1 :(得分:1)

如上所述,你首先在主循环中绘制/ blitting播放器,然后是背景。

错:

while not done: # The main loop, it may be different in your code.

    player.draw() 
    background.draw() # What you're probably doing.

右:

while not done: # The main loop, it may be different in your code.

    background.draw() # What you should do.
    player.draw()

可以肯定的是,无论你如何实现它,只需绘制背景,在主循环块开始之后右键。如果你是在一个单独的程序中更新/绘制你的游戏,如:

def update():
    background.draw() # What you should do.
    player.draw()

while not done:
    update()

请确保您在单独的功能之前绘制背景。记住:背景,你应该总是在任何东西(之前/之前)的“后面”画出它:)。