如何改变地图和抹去所有被分割的精灵



我正在制作一款RPG,在完成第一张地图后,我必须添加从一个地图传递到另一个地图的可能性,因为我不想使用滚动。

实际上,我会检查我的角色的位置是否与特定的精灵(门,不可见的方块等)相同,然后更改字体并加载新的*.txt文件。但问题是,之前的精灵仍然存在!

我检查了一些方法来解决这个问题,很多人说我应该使用fill()功能来覆盖屏幕。它有效,但旧的精灵仍然存在;如果我的角色想去有墙的地方,他不能。

我也尝试清空精灵列表,但它反而导致游戏崩溃,因为角色在这个列表中,即使我重新加载角色…

这是唯一阻碍我实现宇宙测试版本的东西,如果有人能帮助我…

以下是我的游戏循环:
    while continuer_jeu:
            #Limit the speed
            pygame.time.Clock().tick(30)
            for event in pygame.event.get():
                    if event.type == QUIT:
                            continuer_jeu=0
                            continuer_accueil=0
                            continuer_rules=0
                            continuer=0
                            carte=0
                    if event.type == KEYDOWN:
                            #If user press escape => back to the title screen
                            if event.key == K_ESCAPE:
                                    continuer_jeu = 0
                            #Chara's moving key
                            elif event.key == K_RIGHT:
                                    Noc.deplacer('droite')
                            elif event.key == K_LEFT:
                                    Noc.deplacer('gauche')
                            elif event.key == K_UP:
                                    Noc.deplacer('haut')
                            elif event.key == K_DOWN:
                                    Noc.deplacer('bas')
            #Refresh with new positions
            fenetre.blit(fond, (0,0))
            niveau.afficher(fenetre)
            fenetre.blit(Noc.direction, (Noc.x, Noc.y))
            pygame.display.flip()
            if niveau.structure[Noc.case_y][Noc.case_x] == 'ma6': #If going through a door
                    if carte == "n1": # if first map
                            fond = pygame.image.load(image_Interieur).convert()
                            niveau = Niveau("Maison") #Load the house sprites
                            niveau.generer()
                            niveau.afficher(fenetre)
                            fenetre.blit(fond,(0,0))

如果你需要更多的代码,就问。

我实现了从一个映射传递到另一个通过删除前一个列表,加载另一个,然后重新创建字符:

            if niveau.structure[Noc.case_y][Noc.case_x] == "up" :
                    if carte == "n1" :
                            fond = pygame.image.load(image_Map2).convert()
                            niveau = Niveau("n2")
                            niveau.reset()
                            niveau.generer()
                            niveau.afficher(fenetre)
                            Noc = Perso("image/Noc right.png", "image/Noc left.png","image/Noc face.png", "image/Noc back.png", niveau)
                            fenetre.blit(fond,(0,0))
                            carte = "n2"
            if niveau.structure[Noc.case_y][Noc.case_x] == "down" :
                    if carte == "n2" :
                            fond = pygame.image.load(image_Map1).convert()
                            niveau = Niveau("n1")
                            niveau.reset()
                            niveau.generer()
                            niveau.afficher(fenetre)
                            Noc = Perso("image/Noc right.png", "image/Noc left.png","image/Noc face.png", "image/Noc back.png", niveau)
                            fenetre.blit(fond,(0,0))
                            carte = "n1"

与这些函数:

class Niveau:
    def __init__(self, fichier):
            self.fichier = fichier
            self.structure = 0
    def reset(self):
            self.structure = []
    def generer(self):
            with open(self.fichier) as fichier:
                    self.structure = [ligne.split() for ligne in fichier]

但是我有另一个问题:我不能第二次更改地图。在第二张地图上,角色到达一个向下的情况,什么也没有发生。我试图把指令,当它是"下"一个"上"+"如果carte =="n2",但字符回到他的产卵位置…我哪里出错了?

最新更新