Exemple #1
0
    def __init__(self):
        trees = list()
        caves = list()
        ponds = list()
        herbivores = list()
        carnivores = list()

        spaces = list()

        shared_queue = queue.Queue(maxsize=1)

        rows = [n for n in range(0, 17, 1)]
        cols = [n for n in range(0, 31, 1)]

        pygame.init()
        surface = pygame.display.set_mode((1280, 720))
        pygame.display.set_caption('Rumiany Wschód')

        surface.fill(Board.GREEN)
        
        self.redraw_lines(rows, cols, surface)

        for row in rows:
            for col in cols:
                spaces.append((col, row))

        coords = random.sample(spaces, Board.TREE_NUMBER+Board.CAVE_NUMBER+Board.POND_NUMBER)

        for i in range(Board.TREE_NUMBER):
            a, b = coords[i]
            trees.append(Tree(a, b))
        for i in range(Board.TREE_NUMBER, Board.CAVE_NUMBER + Board.TREE_NUMBER):
            a, b = coords[i]
            caves.append(Cave(a, b))
        for i in range(Board.CAVE_NUMBER + Board.TREE_NUMBER,
                       Board.POND_NUMBER + Board.CAVE_NUMBER + Board.TREE_NUMBER):
            a, b = coords[i]
            ponds.append(Pond(a, b))

        self.redraw_resources(surface, coords)

        #Occupancy map
        occupancy_map = self.init_map(rows, cols, spaces, coords)
        shared_queue.put(occupancy_map)

        #Creating the herbivore threads
        herbivores = [Herbivore(i, i, shared_queue, ponds, trees, caves) for i in range(8)]

        #Creating the carnivore threads
        carnivores = [Carnivore(i+6, i+6, shared_queue, ponds, herbivores, carnivores) for i in range(2)]

        for i in herbivores:
            i.herbivores = herbivores

        for i in carnivores:
            i.carnivores = carnivores

        for i in herbivores:
            i.start()

        for i in carnivores:
            i.start()


        while True:
            
            #herbivore reproduction
            for i in herbivores:
                if i.alive:     
                    a, b = i.get_position() 
                    if i.ready_to_reproduce:
                        new_herbivore = Herbivore(a, b, shared_queue, ponds, trees, caves)
                        i.ready_to_reproduce = False

                        herbivores.append(new_herbivore)
                        new_herbivore.start()
                else:
                    herbivores.remove(i)


            for i in herbivores:
                i.herbivores = herbivores

            for i in carnivores:
                if i.alive:
                    a, b = i.get_position()
                    if i.ready_to_reproduce:
                        new_carnivore = Carnivore(a, b, shared_queue, ponds, herbivores, carnivores)
                        i.ready_to_reproduce = False

                        carnivores.append(new_carnivore)
                        new_carnivore.start()
                else:
                    carnivores.remove(i)


            for i in carnivores:
                i.carnivores = carnivores

            # occupancy_map = shared_queue.get(True)
            # occupancy_map = self.init_map(rows, cols, spaces, coords) 
            # shared_queue.put(occupancy_map)

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()

                    #Killing animals before exit
                    for i in herbivores:
                        i.alive = False
                    for i in carnivores:
                        i.alive = False
                    sys.exit()

            #Redrawing everything
            surface.fill(Board.GREEN)
            self.redraw_lines(rows, cols, surface)
            self.redraw_resources(surface, coords)
            self.redraw_herbivores(surface, herbivores)
            self.redraw_carnivores(surface, carnivores)
            pygame.display.update()