def createNewOrganism(self, code, posX, posY, world): if (code == 0 or code == 'F'): return Fox(posX, posY, world) elif (code == 1 or code == 'W'): return Wolf(posX, posY, world) elif (code == 2 or code == 'H'): return Human(posX, posY, world) elif (code == 3 or code == 'A'): return Antelope(posX, posY, world) elif (code == 4 or code == 'C'): return CyberSheep(posX, posY, world) elif (code == 5 or code == 'S'): return Sheep(posX, posY, world) elif (code == 6 or code == 'T'): return Turtle(posX, posY, world) elif (code == 7 or code == 'b'): return Belladonna(posX, posY, world) elif (code == 8 or code == 's'): return HeracleumSosnowkyi(posX, posY, world) elif (code == 9 or code == 'o'): return Sonchus(posX, posY, world) elif (code == 10 or code == 'u'): return Guarana(posX, posY, world) elif (code == 11 or code == 'g'): return Grass(posX, posY, world) else: return None
def randSpawn(self): index = 0 isHuman = False for i in range(0, self.height): for j in range(0, self.width): chance = randint(0, 99) if chance < 15: uni = randint(0, 10) if uni == 0: self.organisms[index] = Sheep(self, Point(i, j)) elif uni == 1: self.organisms[index] = Wolf(self, Point(i, j)) elif uni == 2: self.organisms[index] = Fox(self, Point(i, j)) elif uni == 3: self.organisms[index] = Turtle(self, Point(i, j)) elif uni == 4: self.organisms[index] = Antelope(self, Point(i, j)) elif uni == 5: self.organisms[index] = Cybersheep(self, Point(i, j)) elif uni == 6: self.organisms[index] = Grass(self, Point(i, j)) elif uni == 7: self.organisms[index] = Dandelion(self, Point(i, j)) elif uni == 8: self.organisms[index] = Belladonna(self, Point(i, j)) elif uni == 9: self.organisms[index] = Hogweed(self, Point(i, j)) index += 1 elif not isHuman: self.organisms[index] = Human(self, Point(i, j)) isHuman = True index += 1
def calc_distance(w: Wolf.Wolf, s: Sheep.Sheep): logger.debug("Wywołana metoda calc_distance z parametrami: w=" + w.__repr__() + ", s=" + s.__repr__()) logger.info("Obliczono odległość d=" + str(sqrt((w.x - s.x)**2 + (w.y - s.y)**2))) logger.debug("Wyjście z metody calc_distance") return sqrt((w.x - s.x)**2 + (w.y - s.y)**2)
def create_organism(self, organism_creator, x, y): organism_position = Position(x, y) if 50 - organism_creator < 2: self.add_organism_to_world(Antelope(self, organism_position)) elif 50 - organism_creator < 4: self.add_organism_to_world( SosnowskyBorscht(self, organism_position)) elif 50 - organism_creator < 6: self.add_organism_to_world(Guarana(self, organism_position)) elif 50 - organism_creator < 8: self.add_organism_to_world(Fox(self, organism_position)) elif 50 - organism_creator < 9: self.add_organism_to_world(Thistle(self, organism_position)) elif 50 - organism_creator < 13: self.add_organism_to_world(Sheep(self, organism_position)) elif 50 - organism_creator < 17: self.add_organism_to_world(Grass(self, organism_position)) elif 50 - organism_creator < 19: self.add_organism_to_world(WolfBerries(self, organism_position)) elif 50 - organism_creator < 22: self.add_organism_to_world(Wolf(self, organism_position)) elif 50 - organism_creator < 24: self.add_organism_to_world(Turtle(self, organism_position)) elif 50 - organism_creator < 25: self.add_organism_to_world(CyberSheep(self, organism_position))
def create_organism_by_name(self, organism_species, x, y, cooldown=-1): organism_position = Position(x, y) if organism_species == "Antelope": self.add_organism_to_world(Antelope(self, organism_position)) if organism_species == "SosnowskyBorscht": self.add_organism_to_world( SosnowskyBorscht(self, organism_position)) if organism_species == "Guarana": self.add_organism_to_world(Guarana(self, organism_position)) if organism_species == "Fox": self.add_organism_to_world(Fox(self, organism_position)) if organism_species == "Thistle": self.add_organism_to_world(Thistle(self, organism_position)) if organism_species == "Sheep": self.add_organism_to_world(Sheep(self, organism_position)) if organism_species == "Grass": self.add_organism_to_world(Grass(self, organism_position)) if organism_species == "WolfBerries": self.add_organism_to_world(WolfBerries(self, organism_position)) if organism_species == "Wolf": self.add_organism_to_world(Wolf(self, organism_position)) if organism_species == "Turtle": self.add_organism_to_world(Turtle(self, organism_position)) if organism_species == "CyberSheep": self.add_organism_to_world(CyberSheep(self, organism_position)) if organism_species == "Human": self.create_human(organism_position, cooldown)
def setup(sheep_no, init_pos_lim): fw.write_to_log( f"The function setup(sheep_no, init_pos_lim) invoked with arguments sheep_no={sheep_no}, init_pos_lim={init_pos_lim}, log_level={log_level}", logging.DEBUG, log_level) sheep_list = [ Sheep(init_pos_lim, sheep_move_dist, x, log_level) for x in range(sheep_no) ] wolf = Wolf(wolf_move_dist, log_level) return sheep_list, wolf
def loadFile(self): self.organisms = None self.allocOrganisms() savefile = open("savefile.wsf", "r") index = 0 ptr = 0 string = savefile.read().split(' ') while True: if string[ptr] == "END": break typee = string[ptr] ptr += 1 power = int(string[ptr]) ptr += 1 age = int(string[ptr]) ptr += 1 x = int(string[ptr]) ptr += 1 y = int(string[ptr]) ptr += 1 position = Point(x, y) if typee == "Grass": self.organisms[index] = Grass(self, position) elif typee == "Dandelion": self.organisms[index] = Dandelion(self, position) elif typee == "Guarana": self.organisms[index] = Guarana(self, position) elif typee == "Belladonna": self.organisms[index] = Belladonna(self, position) elif typee == "Hogweed": self.organisms[index] = Hogweed(self, position) elif typee == "Wolf": self.organisms[index] = Wolf(self, position) elif typee == "Sheep": self.organisms[index] = Sheep(self, position) elif typee == "Fox": self.organisms[index] = Fox(self, position) elif typee == "Turtle": self.organisms[index] = Turtle(self, position) elif typee == "Antelope": self.organisms[index] = Antelope(self, position) elif typee == "Human": self.organisms[index] = Human(self, position) elif typee == "Cybersheep": self.organisms[index] = Cybersheep(self, position) self.organisms[index].power = power self.organisms[index].age = age index += 1 if savefile is not None: savefile.close()
def simulate(self): for i in range(0, self.sheep_count): self.sheep_list.append(Sheep(i, self.sheep_speed, self.limit)) while self.turn <= self.turns and self.sheep_left_check(): for sheep in self.sheep_list: sheep.update() self.wolf.update() self.display() FileOperations.append_to_csv([self.turn, self.count_alive_sheep()]) FileOperations.append_dictionary_to_list( self.list_with_dictionaries, self.create_dictionary()) self.turn += 1 FileOperations.create_json(self.list_with_dictionaries)
def Create(self): switcher = { 0 : lambda : Antelope(), 1 : lambda : CyberSheep(), 2 : lambda : Fox(), 3 : lambda : Sheep(), 4 : lambda : Turtle(), 5 : lambda : Wolf(), 6 : lambda : Belladonna(), 7 : lambda : Dandelion(), 8 : lambda : Grass(), 9 : lambda : Guarana(), 10: lambda : HeracleumSosnowskyi(), } func = switcher.get(self.value, None) return func()
def move_wolf(self, sheep: Sheep): Simulate.logger.debug( 'Wywołana metoda move_wolf z klasy Wolf z parametrami: self=' + self.__repr__() + ', sheep=' + sheep.__repr__()) vector = (sheep.x - self.x, sheep.y - self.y) tmp_x = vector[0] * Simulate.wolf_move_dist / Simulate.calc_distance( self, sheep) tmp_y = vector[1] * Simulate.wolf_move_dist / Simulate.calc_distance( self, sheep) Simulate.logger.info('Ustawienie pozycji wilka: początkowa pozycja: ' + self.__repr__()) self.x += tmp_x self.y += tmp_y self.x = round(self.x, 3) self.y = round(self.y, 3) Simulate.logger.info('Ustawienie pozycji wilka: końcowa pozycja: ' + self.__repr__()) Simulate.logger.debug('Wyjście z metody move_wolf z klasy Wolf')
def CreateString(s): switcher = { "Antelope" : lambda : Antelope(), "CyberSheep" : lambda : CyberSheep(), "Fox" : lambda : Fox(), "Sheep" : lambda : Sheep(), "Turtle" : lambda : Turtle(), "Wolf" : lambda : Wolf(), "Belladonna" : lambda : Belladonna(), "Dandelion" : lambda : Dandelion(), "Grass" : lambda : Grass(), "Guarana" : lambda : Guarana(), "Heracleum" : lambda : HeracleumSosnowskyi(), "Player" : lambda : Human() } func = switcher.get(s, None) return func()
def CreateChar(t): switcher = { 'A' : lambda : Antelope(), 'C' : lambda : CyberSheep(), 'F' : lambda : Fox(), 'S' : lambda : Sheep(), 'T' : lambda : Turtle(), 'W' : lambda : Wolf(), 'B' : lambda : Belladonna(), 'D' : lambda : Dandelion(), 'G' : lambda : Grass(), 'U' : lambda : Guarana(), 'H' : lambda : HeracleumSosnowskyi(), 'P' : lambda : Human() } func = switcher.get(t, None) return func()
def generate_sheep(self): # does the initial placement of the sheep herd_start = self.map.herd_start_polygon tot_pos = np.zeros(2) for i in range(self.map.sheep_n): while True: x = herd_start.x_min + np.random.rand() * (herd_start.x_max - herd_start.x_min)/1 y = herd_start.y_min + np.random.rand() * (herd_start.y_max - herd_start.y_min)/1 new_sheep_pos = np.array([x, y]) neighboring_squares = self.get_neighboring_squares(new_sheep_pos) correct_spawn = True if herd_start.contain_point(new_sheep_pos): for current_square in neighboring_squares: for sheep in current_square: if np.linalg.norm(sheep.pos - new_sheep_pos) < self.map.sheep_r * 2: correct_spawn = False else: correct_spawn = False if correct_spawn: new_sheep_vel = np.zeros(2) if np.random.rand() < 0.4: # only some sheep get an initial velocity new_sheep_vel = np.random.normal(0, self.map.sheep_v_max/10, 2) new_sheep = Sheep(self.map, new_sheep_pos, new_sheep_vel) self.get_square(new_sheep_pos).append(new_sheep) self.all_sheep.append(new_sheep) tot_pos += new_sheep_pos break self.sheep_middle_point = tot_pos / len(self.all_sheep) hej = 1
myDog = Dog(playerPos, Constants.DOG_SPEED, Constants.DOGSHEEP_SIZE) #sheepHorizontalSeparation = Constants.WORLD_WIDTH / 11 #sheepVerticalSeparation = Constants.WORLD_HEIGHT / 11 ##sheepList = [Sheep(Vector2(100, 100), Constants.SHEEP_SPEED, Constants.DOGSHEEP_SIZE)] sheepList = [] for x in range(0, 1): xCoord = randint(Constants.DOGSHEEP_SIZE.x, Constants.WORLD_WIDTH - Constants.DOGSHEEP_SIZE.x) yCoord = randint(Constants.DOGSHEEP_SIZE.y, Constants.WORLD_HEIGHT - Constants.DOGSHEEP_SIZE.y) sheepPosition = Vector2(xCoord, yCoord) sheepList.append(Sheep(sheepPosition, Constants.SHEEP_SPEED, Constants.DOGSHEEP_SIZE)) #for x in range(1, 11): # xCoord = x * sheepHorizontalSeparation # for y in range(1, 11): # yCoord = y * sheepVerticalSeparation # sheepPosition = Vector2(xCoord, yCoord) # sheepList.append(Sheep(sheepPosition, Constants.SHEEP_SPEED, Constants.DOGSHEEP_SIZE)) # Setup the gates and obstacles buildGates(graph) buildObstacles(graph) while not done:
from Environment import Environment from Sheep import Sheep from Dog import Dog import statistics import numpy as np if __name__ == '__main__': count_values = [] for curr_ptr in range(100): count = 0 env = Environment() sheep = Sheep() dogs = Dog() print("Sheep location {} {}".format(env.rowSheep, env.colSheep)) print("Dog1 location {} {}".format(env.rowDog1, env.colDog1)) print("Dog2 location {} {}".format(env.rowDog2, env.colDog2)) diag1, diag2 = dogs.get_diagonal_for_dogs(env) while not (diag1[0] == env.rowDog1 and diag1[1] == env.colDog1 and diag2[0] == env.rowDog2 and diag2[1] == env.colDog2): dogs.move_to_dest([env.rowDog1, env.colDog1], diag1, env) dogs.move_to_dest([env.rowDog2, env.colDog2], diag2, env) sheep.move(env) count += 1 print( "---------------------------------------Dogs are at the diagonal") while dogs.manhattan_dist([env.rowSheep, env.colSheep], [env.rowDog1, env.colDog1]) > 0:
from Position import Position from Sheep import Sheep from Grass import Grass if __name__ == '__main__': grass = Grass(position=Position(10, 10)) sheep = Sheep(position=Position(-10, -10)) for i in range(0, 10): print('-- Iteration {0} --'.format(i)) grass.move() print(grass) sheep.move() print(sheep)
sheepSurface = pygame.image.load("sheep.png") dogSurface = pygame.image.load("collie.png") player = Dog(Vector(Constants.WORLD_WIDTH / 2, Constants.WORLD_HEIGHT / 2), Constants.DOG_SIZE, Constants.PLAYER_SPEED, dogSurface) enemyList = [] for i in range(100): #enemy = Enemy(Vector(random.randint(0, Constants.WORLD_WIDTH), random.randint(0, Constants.WORLD_HEIGHT)), Constants.ENEMY_SIZE, Constants.ENEMY_SPEED, (0,255,0)) #enemyHunter = EnemyHunter(Vector(random.randint(0, Constants.WORLD_WIDTH), random.randint(0, Constants.WORLD_HEIGHT)), Constants.ENEMY_SIZE, Constants.ENEMY_SPEED, (255,0,255)) #enemyList.append(enemy) #enemyList.append(enemyHunter) sheep = Sheep( Vector(random.randint(0, Constants.WORLD_WIDTH), random.randint(0, Constants.WORLD_HEIGHT)), Constants.SHEEP_SIZE, Constants.ENEMY_SPEED, sheepSurface) enemyList.append(sheep) Sheep.sheepList = enemyList #TAGEVENT = pygame.USEREVENT tagBack = False #pygame.time.set_timer(TAGEVENT, 1000) while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True #if event.type == TAGEVENT: #for enemy in enemyList:
screen = pygame.display.set_mode((Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT)) done = False clock = pygame.time.Clock() # Game clock # Dog data pos = pos = Vector(random.randint(0, Constants.WORLD_WIDTH - Constants.SHEEP_WIDTH), random.randint(0, Constants.WORLD_HEIGHT - Constants.SHEEP_HEIGHT)) dogSurface = pygame.image.load('collie.png') dog = Dog(pos, Constants.DOG_MOVE_SPEED, dogSurface) # Sheep data sheeps = [] sheepSurface = pygame.image.load('sheep.png') for i in range(100): pos = Vector(random.randint(0, Constants.WORLD_WIDTH - Constants.SHEEP_WIDTH), random.randint(0, Constants.WORLD_HEIGHT - Constants.SHEEP_HEIGHT)) sheep = Sheep(pos, Constants.SHEEP_MOVE_SPEED, sheepSurface) sheeps.append(sheep) # Run in a loop while not done: # Check for game exit for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Set toggles elif event.type == pygame.KEYDOWN: # UI toggles if event.key == pygame.K_1:
def left_click_callback(self, event): s = Sheep(10, event.x, event.y) self.wolf.sheep_list.append(s) self.render_sheep()
# Setup the graph graph = Graph() # Setup the gates and obstacles buildGates(graph) buildObstacles(graph) # Setup the dog pos = Vector(Constants.WORLD_WIDTH * .5, Constants.WORLD_HEIGHT * .5) dog = Dog(pos, Constants.DOG_MOVE_SPEED, Constants.DOG_ANGULAR_SPEED, dogImage) # Setup the sheep (only 1 for now...) herd = [] pos = Vector(randrange(int(bounds.x * .4), int(bounds.x * .6)), randrange(int(bounds.y * .6), int(bounds.y * .8))) sheep = Sheep(pos, Constants.SHEEP_MOVE_SPEED, Constants.SHEEP_ANGULAR_SPEED, sheepImage) herd.append(sheep) # While the user has not selected quit hasQuit = False while not hasQuit: # Clear the screen screen.fill(Constants.BACKGROUND_COLOR) # Process all in-game events for event in pygame.event.get(): if event.type == pygame.QUIT \ or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): hasQuit = True # Change graph search type
# This is a sample Python script. from Goat import Goat from Sex import Sex from Sheep import Sheep from datetime import datetime, date # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. # Press the green button in the gutter to run the script. if __name__ == '__main__': lamb = Sheep(123, "12 03 2019", Sex.Male) print(lamb.rfid()) print(lamb.type()) print(lamb.dob()) lamb.set_date_of_death(datetime.now().strftime("%d %m %Y")) print(lamb.age()) print(lamb.survival_days()) produce = lamb.produce() for p in produce: print(p) print("\n----------------------------\n") goat1 = Goat(234, "6 2 2017", Sex.Female) goat1.set_genetics("Boer") goat2 = Goat(374, "1 8 2018", Sex.Male) goat2.set_genetics("Black Bengal") goat3 = Goat(567, "3 1 2020", Sex.Male) print(goat3.rfid()) print(goat3.type())
#Make the Graph graph = Graph() #draw graph/obstacles buildGates(graph) buildObstacles(graph) #Make the Player dog = Dog(Constants.POSITION, Constants.PLAYER_MAX_SPEED, Constants.GET_SIZE, Constants.COLOR, dogImage) #Make the Sheep sheep_list = [] sheep = Sheep( Vector(random.randrange(int(bounds.x * .4), int(bounds.x * .6)), random.randrange(int(bounds.y * .6), int(bounds.y * .8))), Constants.SHEEP_SPEED, Constants.SHEEP_ANGULAR_SPEED, Vector(Constants.DOG_WIDTH, Constants.DOG_HEIGHT), (0, 255, 0), Constants.MAXTIME, sheepImage) sheep_list.append(sheep) '''for x in range(Constants.SHEEP_AMOUNT): sheep = Sheep(Vector(random.uniform(32,Constants.WORLD_WIDTH-32), random.uniform(32, Constants.WORLD_HEIGHT-32)),Constants.SHEEP_MAX_SPEED, Constants.GET_SIZE, Constants.SHEEP_COLOR, Constants.MAXTIME, sheepSurface) sheep_list.append(sheep) ''' clock = pygame.time.Clock() done = False while not done: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: Constants.sheep_vel_line = not Constants.sheep_vel_line