Ejemplo n.º 1
0
 def _init_foxes(self, nfoxes):
     self.world.foxes.clear()
     for i in range(nfoxes):
         while True:
             pos = self.world.random_point()
             if Fox._can_move_to(self, pos):
                 break
         fox = Fox(self, pos, Direction.DOWN)
         self.world.foxes.append(fox)
Ejemplo n.º 2
0
    def update(self):
        self.ticks += 1

        if self.ticks % 10 == 0:
            self.plants[Species.Carrot].append(
                Food(random.uniform(50, self.width - 50),
                     random.uniform(50, self.height - 50)))

        for spec in self.animals.keys():
            for animal in self.animals[spec]:
                animal.update(self)
                if animal.state == "dead":
                    self.animals[spec].remove(animal)
                elif animal.state == "reproduced":
                    if animal._species == Species.Rabbit:
                        baby = Rabbit(animal.coord.x, animal.coord.y)
                        baby.genes = Genes.combineGenes(
                            animal.genes, animal.genes)
                        self.animals[spec].append(baby)
                    elif animal._species == Species.Fox:
                        self.animals[spec].append(
                            Fox(animal.coord.x, animal.coord.y))

        for spec in self.plants.keys():
            for plant in self.plants[spec]:
                if not plant.isAvailable():
                    self.plants[spec].remove(plant)

        self.recordPopulations()
Ejemplo n.º 3
0
 def getFoxes(self):
     x = int(self.backgroundWidth / self.width)
     n = random.randint(2 * x, 4 * x)
     for i in range(n):
         self.foxes.append(
             Fox(self.width, self.height, self.skylineDict,
                 self.backgroundWidth))
Ejemplo n.º 4
0
 def create_actor(self, tag, pos):
     if tag == 'BUNNY':
         actor = Bunny(self)
         self.bunnies.append(actor)
     elif tag == 'FOX':
         actor = Fox(self)
         self.foxes.append(actor)
     else:
         return
     actor.set_group(self.actors)
     actor.set_pos(pos)
Ejemplo n.º 5
0
    def __init__(self, srn_sz: (float, float), clock: pygame.time.Clock,
                 screen: pygame.Surface, allCells):
        """
		Initializes the World

		Args:
			srn_sz ( (float, float) ): Screen size
			clock (pygame.time.Clock): pygame Clock
			screen (pygame.Surface): pygame Screen
		"""

        self.running = True

        self._clock = clock
        self.screen = screen

        self.runtime = 0
        self.runtime_checkpoint = 0

        self.size = srn_sz
        self.rabbits = []
        self.foxes = []
        self.food = []
        self.landcells = []
        self.shorecells = []
        self.watercells = []
        self.cells = allCells
        self._classify_terrain()
        for _ in range(20):

            rand_pos = random.choice(self.landcells)
            self.rabbits.append(
                Rabbit(self, (rand_pos.x, rand_pos.y),
                       self._random_speed()))  #2.5

        for _ in range(12):
            rand_pos = random.choice(self.landcells)
            self.foxes.append(
                Fox(self, (rand_pos.x, rand_pos.y),
                    self._random_speed()))  #3 self._random_pos()

        for _ in range(80):
            rand_pos = random.choice(self.landcells)
            self.food.append(Food(
                self, (rand_pos.x, rand_pos.y)))  #self._random_pos()

        self._update_screen()
Ejemplo n.º 6
0
def choose_pet(pets):
    """Choice of a particular pet.
    Argument is list of pet picture.
    Return specific pet that user chose.
    """
    check_create = True
    while check_create:
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONDOWN:
                if pets[0].collidepoint(event.pos) and pygame.mouse.get_pressed()[0]:
                    newpet_ = Cat()
                elif pets[1].collidepoint(event.pos) and pygame.mouse.get_pressed()[0]:
                    newpet_ = Fox()
                elif pets[2].collidepoint(event.pos) and pygame.mouse.get_pressed()[0]:
                    newpet_ = Stich()
                check_create = False
    return newpet_
def run():
    pygame.init()

    size = [700,700]
    screen = pygame.display.set_mode(size)

    simple = pygame.image.load('l_one.png')
    simple_rect = simple.get_rect()

    #create fox and wolf
    fox_avatar = Fox(screen)
    wolf_avatar = Wolf(screen)
    #speed
    clock = pygame.time.Clock()
    #world shift
    world_shift_hor = 0
    world_shift_vert = 0

    pygame.display.set_caption("Fox Dash:Avoid the Wolf for 30 seconds ")
    
    #start and create timer
    startTime = pygame.time.get_ticks()
    seconds = 0

    game_cont = True
    while game_cont == True:
        #show on screen
        screen.blit(simple, simple_rect)
        fox_avatar.blitme()
        wolf_avatar.blitme()
        #check movments
        events(fox_avatar)
        fox_avatar.update(screen)
        wolf_avatar.update(screen,fox_avatar)
        pygame.display.update()
        #collision detection
        if check(fox_avatar, wolf_avatar):
            print('Game over')
            game_cont = False
        #timer 30 seconds
        #restart timer
        if seconds >= 30:
            #startTime = pygame.time.get_ticks()
            #seconds = 0
            print("Round done, You are safe!")
            game_cont = False
        seconds=(pygame.time.get_ticks()-startTime)/1000
        #check to see if there is a collision
        ans = check(fox_avatar, wolf_avatar)
        #speed
        clock.tick(30)
        pygame.display.flip()
Ejemplo n.º 8
0
    def __init__(self, animal_start, plant_start, height, width):
        self.population_log = {}
        for k in self.animals.keys():
            self.population_log[k] = []

        self.height = height
        self.width = width
        Animal.HEIGHT = height
        Animal.WIDTH = width
        self.animals_alive = 123
        for i in range(animal_start):
            self.animals[Species.Rabbit].append(
                Rabbit(random.uniform(50, self.width - 50),
                       random.uniform(50, self.height - 50)))

        for i in range(plant_start):
            self.plants[Species.Carrot].append(
                Carrot(random.uniform(50, self.width - 50),
                       random.uniform(50, self.height - 50)))

        for i in range(2):
            self.animals[Species.Fox].append(
                Fox(random.uniform(50, self.width - 50),
                    random.uniform(50, self.height - 50)))
Ejemplo n.º 9
0
from fox import Fox

__all__ = ['fox']

f2 = Fox('judy', 10)

f2.cry('wang')


def func1():
    print('hello, world')
    return 'xxx'


func1()

#
Ejemplo n.º 10
0
def keyword_search():

    start = time()
    # web browser
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--incognito')
    prefs = {
        "profile.managed_default_content_settings.images": 2,
        "profile.default_content_settings.images": 2,
        "disk-cache-size": 4096
    }
    options.add_experimental_option("prefs", prefs)
    options.add_argument('--headless')
    driver = webdriver.Chrome(chrome_options=options)
    print(f'Browser creation took {(time() - start):.4f}s:')

    # setup args for site and keyword
    # get site to use, if none use all
    src = request.args.get('source')
    src = src.lower() if src is not None else src
    keyword = request.args.get('key')
    # make args for only some links

    sites = []
    # select th sites to use
    if src is None:
        sites.append(CNN(driver))
        sites.append(HuffPost(driver))
        sites.append(WashPost(driver))
        sites.append(Fox(driver))
        sites.append(WSJ(driver))
        sites.append(CBS(driver))
        sites.append(WashTimes(driver))
        sites.append(BiReport(driver))
        sites.append(InfoWars(driver))
        sites.append(NYT(driver))
        sites.append(NBC(driver))
        sites.append(CNS(driver))
    elif src == "cnn":
        sites.append(CNN(driver))
    elif src == "huffpost":
        sites.append(HuffPost(driver))
    elif src == "washpost":
        sites.append(WashPost(driver))
    elif src == "fox":
        sites.append(Fox(driver))
    elif src == "wsj":
        sites.append(WSJ(driver))
    elif src == "cbs":
        sites.append(CBS(driver))
    elif src == "washtimes":
        sites.append(WashTimes(driver))
    elif src == "bireport":
        sites.append(BiReport(driver))
    elif src == "infowars":
        sites.append(InfoWars(driver))
    elif src == "nyt":
        sites.append(NYT(driver))
    elif src == "nbc":
        sites.append(NBC(driver))
    elif src == "cns":
        sites.append(CNS(driver))

    data = {'data': []}
    for site in sites:
        site_time = time()
        # get the links and its source
        links = site.get_links(keyword)
        link_src = site.__class__.__name__
        for link in links:

            #edit title
            title = link[1]
            title = title.replace('\u2019', '\'')
            title = title.replace('\u2018', '\'')
            title = title.replace('\u201c', '"')
            title = title.replace('\u201d', '"')
            title = title.replace('\u2013', '-')
            title = title.replace('\u2014', '-')

            new_entry = {
                'src': link_src,
                'link': link[0],
                'title': title,
                'bias': site.bias_score
            }
            data['data'].append(new_entry)

        print(f'Reading "{link_src}" took {(time() - site_time):.4f}s')

    random.shuffle(data['data'])

    #json_str = json.dumps(data)
    response = jsonify(data)
    response.headers.add('Access-Control-Allow-Origin', '*')

    driver.quit()

    print(f'Total time for request took {(time() - start):.4f}s:')

    return response
Ejemplo n.º 11
0
from dog import Dog
from fox import Fox

dog_one = Dog('Tchoupi', 2)
fox_one = Fox('Rox', 7)

fox_one.life()
Ejemplo n.º 12
0
    def initialize_forest(self):
        """Adds initial organisms to the map."""

        directions = list(Direction)
        # Water map
        for pool_size in WATER_POOLS:
            rand_x = random.randint(0, self.width - 1)
            rand_y = random.randint(0, self.height - 1)
            while self.water_map[rand_x][rand_y]:
                rand_x = random.randint(0, self.width - 1)
                rand_y = random.randint(0, self.height - 1)
            water_pools_added = 0
            positions = [(rand_x, rand_y)]
            WATER_POOLS_POSITIONS.append((rand_x, rand_y))
            while water_pools_added < pool_size and positions:
                # Breadth first add water pools around
                x, y = positions.pop(0)
                if not self.water_map[x][y]:
                    water = Water(self, x, y)
                    self.water_map[x][y] = water
                    water_pools_added += 1
                    # Insert all neighbors
                    random.shuffle(
                        directions)  # shuffle for a bit random shapes
                    for dir in directions:
                        new_x = x + dir.value[0]
                        new_y = y + dir.value[1]
                        # Check if out of bounds
                        if new_x < 0 or new_x >= self.width or new_y < 0 or new_y >= self.height:
                            continue
                        if self.water_map[new_x][new_y]:
                            continue
                        positions.append((new_x, new_y))

        # Plant map
        for x in range(self.width):
            for y in range(self.height):
                # check if water
                if self.water_map[x][y]:
                    continue
                if random.random() <= TREE_PERCENTAGE:
                    tree = Tree(self, x, y)
                    self.plant_map[x][y] = tree
                    if random.random() <= HIVES_PER_TREE:
                        hive = Hive(self, x, y)
                        self.animal_map[x][y].append(hive)
                        bee_amount = random.randint(HIVE_BEE_MIN_AMOUNT,
                                                    HIVE_BEE_MAX_AMOUNT)
                        bee = Bee(self,
                                  x,
                                  y,
                                  hive=hive,
                                  scout=True,
                                  age=random.randint(0, 24 * 150))
                        hive.bees.append(bee)
                        self.animal_map[x][y].append(bee)
                        for _ in range(bee_amount):
                            bee = Bee(self,
                                      x,
                                      y,
                                      hive=hive,
                                      scout=False,
                                      age=random.randint(0, 24 * 150))
                            self.animal_map[x][y].append(bee)
                            hive.bees.append(bee)
                elif random.random() <= GRASS_INIT_PERCENTAGE:
                    grass = Grass(self, x, y, random.randint(-80, 100), None,
                                  self.get_initial_water_level(x, y))
                    self.plant_map[x][y] = grass
                else:
                    earth = Earth(self, x, y,
                                  self.get_initial_water_level(x, y))
                    self.plant_map[x][y] = earth

        # Flower map
        from organisms import Type
        for x in range(self.width):
            for y in range(self.height):
                if self.water_map[x][y]:
                    continue
                if random.random() <= FLOWER_PERCENTAGE:
                    if self.plant_map[x][y] and self.plant_map[x][
                            y].type == Type.TREE:
                        continue
                    for _ in range(random.randint(1, 4)):
                        flower = Flower(self,
                                        x,
                                        y,
                                        random.randint(-50, 100),
                                        nectar=random.randint(0, 100),
                                        has_seed=random.choice([True, False]))
                        self.flower_map[x][y].append(flower)

        # Animal map
        import numpy as np
        # Rabbits
        for _ in range(BURROW_AMOUNT):
            x = random.randint(0, self.width - 1)
            y = random.randint(0, self.height - 1)
            while self.water_map[x][y]:
                x = random.randint(0, self.width - 1)
                y = random.randint(0, self.height - 1)
            burrow = Burrow(self, x, y)
            self.animal_map[x][y].append(burrow)
            rabbit_amount = random.randint(BURROW_RABBIT_MIN_AMOUNT,
                                           BURROW_RABBIT_MAX_AMOUNT)
            for _ in range(rabbit_amount):
                dx = random.randint(-3, 3)
                dy = random.randint(-3, 3)

                if x + dx < 0 or x + dx >= self.width or y + dy < 0 or y + dy >= self.height:
                    continue

                if self.water_map[x + dx][y + dy]:
                    continue

                rabbit = Rabbit(self,
                                x + dx,
                                y + dy,
                                random.choice([True, False]),
                                adult=True,
                                burrow=burrow,
                                age=random.randint(24 * 30, 24 * 30 * 3),
                                reproduction_timer=random.randint(0, 24 * 6),
                                genetics_factor=np.random.normal(1, 0.1))
                self.animal_map[x + dx][y + dy].append(rabbit)

        # Foxes
        for _ in range(FOX_AMOUNT):
            x = random.randint(0, self.width - 1)
            y = random.randint(0, self.height - 1)
            while self.water_map[x][y]:
                x = random.randint(0, self.width - 1)
                y = random.randint(0, self.height - 1)
            fox = Fox(self,
                      x,
                      y,
                      random.choice([True, False]),
                      adult=True,
                      age=random.randint(24 * 30 * 2, 24 * 30 * 6),
                      genetics_factor=np.random.normal(1, 0.1))
            self.animal_map[x][y].append(fox)
Ejemplo n.º 13
0
 def getFoxSprites(self):
     i = Image.open(Fox.url)
     i = i.resize((int(self.width / 2), int(self.height / 2)),
                  Image.ANTIALIAS)
     foxSprites = Fox.getSprites(int(self.width / 2), int(self.height / 2))
     return (self.getSprites(i, foxSprites))