예제 #1
0
def makeZombie(destination, count):
    global zombiePath, zombieLife, zombiePos, explode, chiliList, mapChanged, chiliNum, shovelNum, brickNum, loseGame
    if mapChanged:  #calculate shortestPath again if walls# change
        mapChanged = False
        Asearch = AStar.Astar(End, destination, blockList, mazeSize)
        print('*zombie is finding a new path')
        while Asearch.nextStep() == None:
            print('*there is no path from entrance to exit, try to create one')
            pygame.time.delay(2000)
            Asearch = AStar.Astar(End, destination, blockList, mazeSize)
        else:
            print('*a path is found')
        zombiePath = Asearch.shortestPath
    moveSpeed = 700
    eatSpeed = 1900
    zombieLife = 3
    zombiePos = End[:]
    print('zombie ' + str(count))
    for nextStp in zombiePath:
        if zombieLife < 1:
            break  #zombie is killed
        elif nextStp in [p[0:2] for p in chiliList]:  #chili blocks,eat it
            pygame.time.delay(eatSpeed)
            if zombieLife < 1:
                break  #zombie is killed
            else:
                for p in chiliList:
                    if nextStp == p[0:2]:
                        p[4] = 'die'
        while nextStp in blockList and zombieLife > 0:  #wall blocks
            pygame.time.delay(moveSpeed)
        zombiePos = nextStp  #move
        pygame.time.delay(moveSpeed)
    if explode:  #explode
        pygame.time.delay(200)  #explode animation time
        explode = False
        for x in range(-1, 2):
            for y in range(-1, 2):
                if 0 < x + zombiePos[0] < mazeSize[0] - 1 and 0 < y + zombiePos[
                        1] < mazeSize[1] - 1:  #wall cannot be removed
                    if [x + zombiePos[0],
                            y + zombiePos[1]] in blockList:  #remove walls
                        blockList.remove([x + zombiePos[0], y + zombiePos[1]])
                        mapChanged = True
                    elif [x + zombiePos[0], y + zombiePos[1]
                          ] in [p[0:2] for p in chiliList]:  #remove chilies
                        for p in chiliList:
                            if [x + zombiePos[0], y + zombiePos[1]] == p[0:2]:
                                p[4] = 'die'
    if zombieLife > 0:
        if chiliNum > 0: chiliNum -= 1
        if shovelNum > 0: shovelNum -= 1
        if brickNum > 0: brickNum -= 1
        if chiliNum == 0 and shovelNum == 0 and brickNum == 0:
            print('lose game')
            loseGame = True
            exit()
    makeZombie(destination, count + 1)  #new zombie
    def generatePath(self):
        print("LOOKING FOR SHORTER PATH")
        map_grid = self.grid.getMap()
        dest = self.grid.getDestiny()
        source = self.grid.getPose()

        # Note: The grid coordinates are inverted

        if map_grid[dest[1]][dest[0]] < 125:
            print("Clicked point not an empty space!")
            return 0
        else:
            print("Point selected has colour")
            map_copy = np.array(map_grid)
            dest_transpose = (dest[1], dest[0])
            source_transpose = (source[1], source[0])

            # Is value < 125 -> True ->  int -> 1
            map_copy = (map_copy < 125).astype(int)

            a = time.time()
            a_star = AStar.Astar(map_copy, source_transpose, dest_transpose)
            path = a_star.get_path()
            t = time.time() - a

            print("Shortest Path found in: %f Seconds" % t)

            self.grid.setPathFinded()
            for node in path:
                self.grid.setPathVal(node[1], node[0], 1)
        pass