def __init__(self, state_size, action_size, random_seed): """Initialize an Agent object. Params ====== state_size (int): dimension of each state action_size (int): dimension of each action random_seed (int): random seed """ self.state_size = state_size self.action_size = action_size self.seed = random.seed(random_seed) # Actor Network (w/ Target Network) self.actor_local = Actor(state_size, action_size, random_seed).to(device) self.actor_target = Actor(state_size, action_size, random_seed).to(device) self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=LR_ACTOR) # Critic Network (w/ Target Network) self.critic_local = Critic(state_size, action_size, random_seed).to(device) self.critic_target = Critic(state_size, action_size, random_seed).to(device) self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=LR_CRITIC, weight_decay=WEIGHT_DECAY) # Noise process self.noise = OUNoise(action_size, random_seed) # Replay memory self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, random_seed)
def __init__(self, state_size, action_size): self.epsilon = 0.8 self.state_size = state_size self.action_size = action_size # Actor (Policy) Model self.actor_local = Actor(self.state_size, self.action_size) self.actor_target = Actor(self.state_size, self.action_size) # Critic (Value) Model self.critic_local = Critic(self.state_size, self.action_size) self.critic_target = Critic(self.state_size, self.action_size) # Initialize target model parameters with local model parameters self.critic_target.model.set_weights( self.critic_local.model.get_weights()) self.actor_target.model.set_weights( self.actor_local.model.get_weights()) # Noise process # self.exploration_mu = 0 # self.exploration_theta = 0.15 # self.exploration_sigma = 0.2 # self.noise = OUNoise(self.action_size, self.exploration_mu, self.exploration_theta, self.exploration_sigma) # Replay memory self.buffer_size = 20000 self.batch_size = 64 self.memory = ReplayBuffer(self.buffer_size, self.batch_size) # Algorithm parameters self.gamma = 0.95 # discount factor self.tau = 0.002 # for soft update of target parameters self.stats = np.array([])
def __init__(self, image, laser, position): Actor.__init__(self, image, position, 100) self._laser = laser self._rotationSpeed = 100 #Degrees per second self._fireDurationSec = 0
def update(self, elapsedTimeSec): Actor.update(self, elapsedTimeSec) # Handle mouse rotation mousePos = pygame.mouse.get_pos() angleToMouse = (Vec2d(mousePos) - self.getPosition()).get_angle() - 90 self._rotate(angleToMouse) # Handle keyboard movement pressedKeys = pygame.key.get_pressed() self._velocity = Vec2d(0, 0) if pressedKeys[K_w]: self._velocity.y -= 1.0 if pressedKeys[K_s]: self._velocity.y += 1.0 if pressedKeys[K_a]: self._velocity.x -= 1.0 if pressedKeys[K_d]: self._velocity.x += 1.0 self._velocity = self._truncate(self._velocity * self._maxVelocity, self._maxVelocity) self._updatePosition() # Handle firing laser if self._fireDurationSec > 0: self._laser.fire(self.rect.center, self._rotation) self._fireDurationSec -= elapsedTimeSec else: self._laser.stop()
def shot(self): self.anim.play(3, 0) self.shot_sound.play() if Player2.this.check_take_damage(self.mouse_pos) is False: tem_pos = cp.copy(self.mouse_pos) # tem_pos[1] -= 150 Actor.take_damage_shortest_point(tem_pos)
def __init__(self, main): # transition from another state State.__init__(self,main) self.loadPlayer() self.hud = Actor(IMG_HUD,-1) self.hud2 = Actor(IMG_HUD2) self.hud.setPos(32,HEIGHT/2) self.hud2.setPos(WIDTH-32,HEIGHT/2) GameState.guiGroup.add(self.hud) GameState.guiGroup.add(self.hud2) self.health = 7 self.hudHearts = [] self.hudHeartsHalf = Actor(IMG_HEART2,-1) self.hudSlot = [None]*3 self.wl = WorldLoader('new.world') self.background = TerrainLayer("0_0.map") self.currentMap = "0_0.map" for i in range(0,3): self.hudSlot[i] = Actor(IMG_SLOT,-1) self.hudSlot[i].setPos(50,120+i*120) self.guiGroup.add(self.hudSlot[i]) self.updateHudHealth() pygame.mixer.init() filename = "worldAmbient.mp3" path = os.path.join(util.GAME_SOUNDS, filename) path = util.filepath(path) pygame.mixer.music.load(path) pygame.mixer.music.play()
def parseActorsAndMovies(): Actor.unpickleActors() if(not Actor.total): print "Reading all actor data from dataset..." inputFile = open("../dataset/actor_movies.txt") lineString = inputFile.readline() while(lineString): actor_name = "" parsedLineArray = lineString.split("\t\t") for i in range(0,len(parsedLineArray)): if(i == 0): actor_name = parsedLineArray[0].strip() actor = Actor(actor_name) else: if(parsedLineArray[i].strip() != ""): (movieTitle,movieYear) = Movie.separateTitleAndYear(parsedLineArray[i].strip()) movieActedIn = Movie.hashByTitle[movieTitle] #Set actor's movie list actor.movieList.append(movieActedIn) #Update actor's movieVector for current movie actor.moviesVector[0][movieActedIn.id] = 1 #Append current movie's actor list with current actor movieActedIn.actors.append(actor) lineString = inputFile.readline() print actor #Append actor's moviesVector to the actorMovieMatrix if(Actor.total == 1): Actor.actorMovieAdjacencyMatrix = np.append(np.zeros((0,Movie.total)),actor.moviesVector,axis=0) else: Actor.actorMovieAdjacencyMatrix = np.append(Actor.actorMovieAdjacencyMatrix,actor.moviesVector,axis=0) Actor.pickleActors()
def act(self): print(f'Iteration {Actor.Iteration(self)}: {Actor.getID(self)}') conditions = self.getGrowthCondition() temp = self.getWorld().getTemp(self.getQuadrant()) if temp <= conditions[2] and temp >= conditions[1]: self.__dStrength *= conditions[0] Actor.nextIteration(self)
def __init__(self, eventQueue, commentatorQueue): Thread.__init__(self) self.eventQueue = eventQueue self.commentatorQueue = commentatorQueue self.commentators = [] self.commentators.append(Actor("Rivington the 4th", "Brian", 0)) self.commentators.append( Actor( random.choice([ "Robo shocks", "the queen of twitch", "Your mom", "Pissed off", "undefined" ]), "Salli", 1)) riv_bot = self.commentators[0] salli_bot = self.commentators[1] self.reset() with open("Config.json") as configFile: config = json.load(configFile) self.numberTh = [ "first", "second", "third", "forth", "fifth", "sixth", "seventh", "eigth", "ninth", "tenth", "eleventh" ]
def __init__(self, name=None): if name is None: name = "registrar" Actor.__init__(self, name) return
def setUpClass(cls): cls.world_one = World(7, 9) cls.world_two = World(10, 15) cls.actor_one = Actor() cls.actor_two = Actor() cls.actor_three = Actor() cls.world_two.addObject(cls.actor_two, 5, 10)
def simulate(num_epochs:int): ''' runs our simulation with 2 actors :param num_epochs: the number of iterations we want to run :type num_epochs: int :return: None ''' data = {"actor1_money":[], "actor2_money":[], "actor1_choice":[], "actor2_choice":[]} player1 = Actor(avaliable_funds = 10, lower_threshhold=5, betrayal_rate=.03) player2 = Actor(avaliable_funds = 10, lower_threshhold=5, betrayal_rate=.90) for i in range(num_epochs): choice1 = player1.make_choice() choice2 = player2.make_choice() processChoice(choice1, choice2, player1, player2) data["actor1_money"].append(player1.avaliable_funds) data["actor2_money"].append(player2.avaliable_funds) data["actor1_choice"].append(choice1) data["actor2_choice"].append(choice2) player1.update() player2.update() df = pd.DataFrame(data) print(df) plt.plot(range(0,num_epochs), df['actor1_money']) plt.plot(range(0,num_epochs), df['actor2_money']) plt.show() plt.clf() counts1 = df.groupby(['actor1_choice'])['actor1_choice'].count() counts2 = df.groupby(['actor2_choice'])['actor2_choice'].count() print(counts2)
def __init__(self, state_size, action_size, seed=0): '''Initlize the Agent. Parameters ---------- state_size : int The dimension of each state action_size : int The dimension of each action seed : int The random seed used to generate random numbers. ''' self.state_size = state_size self.action_size = action_size random.seed(seed) #actor gives the best action for given state self.actor_local = Actor(state_size, action_size, seed).to(device) self.actor_target = Actor(state_size, action_size, seed).to(device) #evaluates the action self.critic_local = Critic(state_size, action_size, seed).to(device) self.critic_target = Critic(state_size, action_size, seed).to(device) self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=ACTOR_LEARNING_RATE) self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=CRITIC_LEARNING_RATE, weight_decay=WEIGHT_DECAY) #Replay Memory self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, seed) #Noise self.noise = OUNoise(action_size,seed) self.t_step = 0
def __init__(self, gamma, memory, s, a, tau, learningRate=1e-3, criticpath=None, actorpath=None): self.gamma = gamma self.memory = ReplayMemory(memory) self.actor = Actor(state=s, actions=a) self.critic = Critic(state=s, actions=a) if (not (criticpath == None)): self.critic.load_state_dict(torch.load(criticpath)) if (not (actorpath == None)): self.actor.load_state_dict(torch.load(actorpath)) self.targetActor = Actor(state=s, actions=a) self.targetActor.load_state_dict(self.actor.state_dict()) self.targetCritic = Critic(state=s, actions=a) self.targetCritic.load_state_dict(self.critic.state_dict()) self.tau = tau self.actorOptimizer = optim.Adam(self.actor.parameters(), learningRate) self.criticOptimizer = optim.Adam(self.critic.parameters(), learningRate) #more a dimensionality thing self.state = s self.action = a self.OUarray = np.zeros((1000, self.action), dtype="f") self.step = 0
def makeDefault(self, meshSrc="Empty"): """Create an Actor with a default set of components, and specified mesh.""" actor = Actor(self.renderer) actor.components['Mesh'] = Mesh.getMesh(meshSrc) # NOTE Meshes are currently shared, therefore not linked to individual actors actor.components['Transform'] = Transform(actor=actor) actor.components['Material'] = Material(actor=actor) return actor
def flush_cache(): """Flush all object caches. This function does *not* save the contents of the caches, or invalidate existing objects. Only use this function if you are holding no first-class game objects, and do not wish to keep any unsaved changes.""" Actor.flush_cache() Item.flush_cache() Location.flush_cache()
def __init__(self, tree): Actor.__init__(self, "An apple", 0x147514) self.ignoreBlocking = True self.lifetime = 0 self.tree = tree self.edible = True self.hungerValue = 10 self.canPass = True
def __init__(self): tf.reset_default_graph() self.sess = tf.Session() self.actor = Actor(self.sess, \ n_features=Config.PLAYER_DIMENSION*(Config.DEFENDER_COUNT+Config.INTRUDER_COUNT), \ lr=Config.LEARNING_RATE_START, action_bound=[-math.pi, math.pi]) self.critic = Critic(self.sess, \ n_features=Config.PLAYER_DIMENSION*(Config.DEFENDER_COUNT+Config.INTRUDER_COUNT), \ lr=Config.LEARNING_RATE_START) self.sess.run(tf.global_variables_initializer())
def __init__(self, x_position, y_position): Actor.__init__(self) self.image = self.spritesheet[0] self.state = GhoulStates.DROPPING self.xvector = 0 (width, height) = self.image.get_size() left = x_position - width / 2 top = y_position - height / 2 self.rect = pygame.Rect(left, top, width, height)
def __init__(self, env, batchSize = 10, bufferSize = 100, gamma = 0.98, actorLR = 1e-4, criticLR = 1e-3, maxSteps = 200, targetUpdate = 1e-3, epsilon = 1, decay = 0.99, rewardScale = 1e-3, logFile = 'run.log'): self.env = env self.gamma = gamma self.batchSize = batchSize self.bufferSize = bufferSize self.maxSteps = maxSteps + 1 self.rewardScale = rewardScale self.epsilon = epsilon self.decay = decay # Useful helpers. self.actionDim = self.env.action_space.shape[0] self.stateDim = self.env.observation_space.shape[0] self.featureDim = self.actionDim + self.stateDim self.minAction = self.env.action_space.low self.maxAction = self.env.action_space.high # For scaling output action values. self.actionBiasZeroOne = self.minAction self.actionScaleZeroOne = self.maxAction - self.minAction self.actionBiasTanH = (self.maxAction + self.minAction) / 2.0 self.actionScaleTanH = self.maxAction - self.actionBiasTanH # Initialize noise process. self.noise = OUNoise(self.actionDim) # Initialize replay buffer. self.buffer = ReplayBuffer(self.bufferSize) # Initialize logging. logging.basicConfig(filename = logFile, level = logging.INFO, format = '[%(asctime)s] %(message)s', datefmt = '%m/%d/%Y %I:%M:%S %p') logging.info('Initializing DRPG agent with passed settings.') # Tensorflow GPU optimization. config = tf.ConfigProto() # GPU fix? config.gpu_options.allow_growth = True self.sess = tf.Session(config = config) from keras import backend as K K.set_session(self.sess) # Make actor network (creates target model internally). self.actor = Actor(self.sess, self.maxSteps, self.featureDim, self.actionDim, self.batchSize, targetUpdate, actorLR, self.actionScaleTanH, self.actionBiasTanH) # Make critic network (creates target model internally). self.critic = Critic(self.sess, self.maxSteps, self.featureDim, self.actionDim, self.batchSize, targetUpdate, actorLR)
def __init__(self, state_size, action_size, num_agents): """ Params ====== state_size (int): dimension of each state action_size (int): dimension of each action num_agents (int): number of agents in the environment """ random_seed = 10.0 self.state_size = state_size self.action_size = action_size self.random_seed = random.seed(random_seed) self.num_agents = num_agents # Replay memory self.memory = ReplayBuf(action_size, BUFFER_SIZE, BATCH_SIZE, self.random_seed) # Actor Networks self.actor_local = Actor(state_size, action_size, random_seed).to(device) self.actor_target = Actor(state_size, action_size, random_seed).to(device) self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=LR_ACTOR) # Make sure the Actor Target Network has the same weight values as the Local Network for target, local in zip(self.actor_target.parameters(), self.actor_local.parameters()): target.data.copy_(local.data) # Critic Network (w/ Target Network) self.critic_local = Critic(state_size * num_agents, action_size * num_agents, random_seed).to(device) self.critic_target = Critic(state_size * num_agents, action_size * num_agents, random_seed).to(device) self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=LR_CRITIC, weight_decay=WEIGHT_DECAY) """ self.critic_local = Critic(state_size, action_size, random_seed).to(device) self.critic_target = Critic(state_size, action_size, random_seed).to(device) self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=LR_CRITIC, weight_decay=WEIGHT_DECAY) """ # Make sure the Critic Target Network has the same weight values as the Local Network for target, local in zip(self.critic_target.parameters(), self.critic_local.parameters()): target.data.copy_(local.data) self.noise = Ornstein_Uhlenbeck_Noise(action_size, random_seed)
def __init__(self, game, **kwargs): Actor.__init__(self, game, **kwargs) '''self.height = h self.width = w self.x = _x self.y = _y''' self.Disabled = False self.Absolute = True self.focused = False self.Collision = False self.priority = -1
def start(self): characters = [ Character(State.NEUTRAL, count, self._result_accumulator) for count in xrange(NUM_CHARACTERS) ] actor = Actor(self._result_accumulator) actor.register_characters(characters) for character in characters: character.set_actor(actor) for character in characters: character.start() self._seed(characters)
def __init__(self, name, colour = 0x000000): Actor.__init__(self, name, colour) # How many blocks in front can we see? self.los = 5 # Whats our field of view? self.fov = 120 # Ignore these self.path = None self.direction = (1, 0) self.FOVLines = []
def load_weights(self, option=None): if (option == None): self.trained = Actor(self.state_size, self.action_size, self.action_low, self.action_high, self.actor_lr, self.network) self.trained.model.load_weights('model_weights.h5') else: self.trained = Actor(self.state_size, self.action_size, self.action_low, self.action_high, self.actor_lr, self.network) self.trained.model.load_weights('weights-best.hdf5') print(self.trained.model.summary())
def __init__(self, transactionXmlNode, simulation, tid=None, ppid=None, entitiesXmlNode=None, actor=None, entities=None, xcontext=None): super().__init__(sim=simulation) self.simulation = simulation try: self.entitiesXmlNode = entitiesXmlNode if entitiesXmlNode is not None else XmlSource( ) self.template = transactionXmlNode.get("id") self.pid = self.simulation.getTId() self.id = tid if tid is not None else self.pid self.ppid = ppid if actor is not None: self.actor = actor else: path, base = transactionXmlNode.getWithBase("actor") if path is not None: self.actor = Actor(self.simulation, xmlLoader(path, base=base), extraProperties=True) else: self.actor = Actor(self.simulation, XmlSource()) self.startTime = None if xcontext is None: self.xcontext = XValueContext( lambda: self.simulation.now() - self.startTime) else: self.xcontext = xcontext self.t = self.xcontext.t path, base = transactionXmlNode.getWithBase("entities") if path is not None: self.entitiesXmlNode.append(xmlLoader(path, base=base)) if entities is None: self.factory = EntityFactory(entitiesXmlNode) self.entities = populateEntities(self.factory, self, transactionXmlNode) else: for entity in entities: entity.setTransaction(self) self.entities = entities except Exception as e: print(e) traceback.print_exc(file=sys.stderr)
def main(): world = World(7, 9) print("Number of objects in cell(4,4) = %d" % world.addObject(Actor(), 4, 4)) print("Number of objects in cell(2,3) = %d" % world.addObject(Actor(), 2, 3)) print("Number of objects in cell(4,4) = %d" % world.addObject(Actor(), 4, 4)) print("Number of objects in cell(4,4) = %d" % world.addObject(Actor(), 4, 4)) print(world) print("%r" % world)
def load_weights(self, load_from): checkpoint = torch.load(load_from) critic_params_and_state_dict = checkpoint[ 'critic_params_and_state_dict'] actor_params_and_state_dict = checkpoint['actor_params_and_state_dict'] self.actor = Actor(actor_params_and_state_dict['actor_params']) self.actor.load_state_dict(actor_params_and_state_dict['state_dict']) self.critic = Critic(critic_params_and_state_dict['critic_params']) self.critic.load_state_dict(critic_params_and_state_dict['state_dict']) return self
def import_actors(db): """ Imports Actors, creates object and adds to queue :param db: db connection :return: queue of actors """ actors = db['actors'].find({'FAILED': False}) return_q = Queue() for a in actors: created_a = Actor(a['id'], a['DIRECTOR']) created_a.import_fields(a) return_q.put(created_a) return return_q
def addObject(self, obj: Actor, x: int, y: int) -> int: if obj is None: raise NameError('The object is null') if x < 0 or x >= self.__width or y < 0 or y >= self.__height: raise ValueError('x or y is not in the valid range') if not any(value is None for value in self.__grid[y][x]): raise SyntaxError( f'Max number of objects are already in cell [{x}, {y}]') index = self.__grid[y][x].index(None) self.__grid[y][x][index] = obj self.__objCounter += 1 obj.addedToWorld(self) obj.setLocation(x, y) return index + 1
def __init__(self, env, sess, LEARNING_RATE_ACTOR, LEARNING_RATE_CRITIC, NET_SIZE, MEMORY_LEN, REWARD_DISCOUNT, BATCH_SIZE, TAU, EXPLORATION_STEPS, VERBOSE, LOG_DIR_TF): self.env = env self.sess = sess self.observation_space = self.env.observation_space.shape[0] self.action_space = self.env.action_space.shape[0] self.REWARD_DISCOUNT = REWARD_DISCOUNT self.TAU = TAU self.BATCH_SIZE = BATCH_SIZE self.noise_state = np.zeros(self.action_space) self.EXPLORATION_STEPS = EXPLORATION_STEPS self.VERBOSE = VERBOSE self.LOG_DIR_TF = LOG_DIR_TF #check if action_space is symmetric if all(env.action_space.high == abs(env.action_space.low)): action_scale = env.action_space.high else: raise ActionSpaceNotSymmetricException self.actor = Actor(self.sess, self.observation_space, self.action_space, LEARNING_RATE_ACTOR, NET_SIZE, TAU, action_scale) self.critic = Critic(self.sess, self.observation_space, self.action_space, LEARNING_RATE_CRITIC, NET_SIZE, TAU) actor_network_variables = self.actor.network.get_variables() critic_q_net_variables = self.critic.q_net.get_variables() self.actor_target_update = self.actor.target_network.update_variables( actor_network_variables) self.critic_target_update = self.critic.target_q_net.update_variables( critic_q_net_variables) self.reward_pl = tf.placeholder(tf.float32, [None, 1], name='Reward_PL') self.done_pl = tf.placeholder(tf.bool, [None, 1], name='Done_PL') self.labels = tf.where( self.done_pl, self.reward_pl, self.reward_pl + tf.multiply(self.REWARD_DISCOUNT, self.critic.target_prediction)) #self.replay_memory = ReplayMemory(MEMORY_LEN, BATCH_SIZE) self.replay_memory = ReplayMemory(MEMORY_LEN, BATCH_SIZE, self.observation_space, self.action_space) self.log_reward_pl = tf.placeholder(tf.float32, name='Reward_log_pl') self.reward_f = tf.add(0.0, self.log_reward_pl) tf.summary.scalar('reward', self.reward_f) init = tf.global_variables_initializer() self.sess.run(init) self.sess.run(self.actor.network.copy_to(self.actor.target_network)) self.sess.run(self.critic.q_net.copy_to(self.critic.target_q_net)) self.writer = tf.summary.FileWriter(self.LOG_DIR_TF, self.sess.graph) self.merged = tf.summary.merge_all()
def __init__(self, env, sess): # Environment self.n_state = env.observation_space.shape[0] self.n_action = env.action_space.shape[0] # Neural Networks self.sess = sess self.actor = Actor(self.sess, self.n_state, self.n_action) self.critic = Critic(self.sess, self.n_state, self.n_action) # Replay Buffer self.replay_buffer = ReplayBuffer(BUFFER_SIZE) # Ornstein-Uhlenbeck Noise self.exploration_noise = OUNoise(self.n_action)
def test_exceptions(self): with self.assertRaises(NameError): self.world_one.addObject(None, 6, 5) with self.assertRaises(ValueError): self.world_one.addObject(Actor(), 10, 5) with self.assertRaises(ValueError): self.world_one.addObject(Actor(), -1, 5) with self.assertRaises(ValueError): self.world_one.addObject(Actor(), 6, 10) with self.assertRaises(ValueError): self.world_one.addObject(Actor(), 10, -1) with self.assertRaises(SyntaxError): for ind in range(6): self.world_one.addObject(Actor(), 6, 5)
def __init__(self, name, cash): Actor.__init__(self, cash, name) self.location_on_board = 0 self.sum_of_numbers_rolled_on_dice = 0 self.consecutive_doubles_counter = 0 self.list_of_numbers_rolled = [] self.locations_visited_by_player = [] self.just_visiting_jail = True self.get_out_of_jail_free_card = False # Initially, player does not have the "get out of jail free" card. self.total_number_of_houses_owned = 0 # Initially player owns 0 houses self.total_number_of_hotels_owned = 0 # Initially player owns 0 hotels self.game_board = None self.collectSalary = False self.bank = Bank.Instance() self.playAnotherTurn = False
def __init__(self): Actor.__init__(self) self.weapon = None self.armor = None self.accessory = None self.eqItem = [] # player can equip up to 4 usable items to use in battle self.inv_Ar = [] # inventory self.attacks_Ar = [] # associated array for attack string names and attack power values self.currentInput = "" self.currentProb1 = 0 self.currentProb2 = 0 self.currentAnswer = 0 self.fractionSum = 0 self.akhal = 0
def __init__(self, x_position, y_position): Actor.__init__(self) self.image = self.spritesheet[0] self.state = GhoulStates.DROPPING self.xvector = 0 self.sprite_loop = lsm.create({ 0: Ghoul.spritesheet[0], 15: Ghoul.spritesheet[1], 30: lsm.end }) (width, height) = self.image.get_size() left = x_position - width / 2 top = y_position - height / 2 self.rect = pygame.Rect(left, top, width, height)
def __init__(self,x,y,movespeed): Actor.__init__(self, x, y, movespeed) #self.up() #self.setheading(random.randrange(360)) # self.down() self.newHead = None self.velocity = Vec2D(0, 0) self.vlimit = 2 self.repulsionforce = 2 self.minNieghborDist = 100 self.neighbordist = 1000 self.neighbors = [] SchoolingActor.swarm.append(self)
def __init__(self, main): # transition from another state super(TitleState, self).__init__(main) self.titleScreen = Actor(IMG_TITLE_SCREEN) self.btnStart = Actor(IMG_LABEL_START) self.btnStart2 = Actor(IMG_LABEL_START2) self.btnStart.setPos(config.WIDTH/2, config.HEIGHT - 300) self.btnStart2.setPos(config.WIDTH/2, config.HEIGHT - 300) self.tick = 0 self.tickInterval = 60 TitleState.titleGroup.add(self.titleScreen) TitleState.btnStartGroup.add(self.btnStart) '''TODO: FIX MUSIC
def __init__(self, xpos, ypos): Actor.__init__(self) self.image1 = pygame.image.load("assets/bat/bat1.png") self.image2 = pygame.image.load("assets/bat/bat2.png") self.image3 = pygame.image.load("assets/bat/bat3.png") self.image = self.image1 self.hitboxoffset = 0 self.rect = pygame.Rect(xpos+self.hitboxoffset-30/2, ypos-30/2, 30, 50) self.swoop = 120 self.swoop_frame = self.swoop self.swoop_velocity = 5 self.swoop_decay = .1 self.velocity = 0 self.xvector = 0 self.yvector = 0 self.__name__ = "Bat"
def parseMonsters(self): monsterlist = [] monsters = self._root.find('monsters') if monsters is not None: monsterfile = defres(monsters.get('file', 'monsters.mon')) monroot = _safeopen(monsterfile).getroot() monsters = monsters.findall('monster') else: monsters = [] for monster in monsters: id = monster.get('id', 'octorok') m = None m = monroot.find(id) if m is None: raise ValueError('could not find monster with id \''+id+'\' in '+str(monroot)) hp = m.find('hp') if hp is None: hp = 8 else: hp = int(hp.text) atk = m.find('attack') if atk is None: atk = 8 else: atk = int(atk.text) pattern = None speed = 0 aimov = m.find('ai') if aimov is not None: aimov = aimov.find('movement') if aimov is not None: speed = aimov.find('speed') pattern = aimov.find('pattern') if speed is not 0: options = speed.findall('option') if options is not None: speed = int(options[random.randint(0,len(options)-1)].text) if pattern is not None: pattern = pattern.text a = Actor(int(monster.get('x', '0')), int(monster.get('y', '0')), speed, hp, atk, pygame.Rect(0, 0, Tile.SIZE, Tile.SIZE), True, _sprites(m), None, pattern) aabbnode = m.find('aabb') caabbnode = m.find('collaabb') haabbnode = m.find('hitaabb') if aabbnode is not None: a.setaabb(_parserect(aabbnode.find('rect'))) else: if caabbnode is not None: a.setcollaabb(_parserect(caabbnode.find('rect'))) if haabbnode is not None: a.sethitaabb(_parserect(haabbnode.find('rect'))) monsterlist.append(a) return monsterlist
def tick(self, world, tick): # Move first if self.path: node = self.path.next() if node: self.setPos((node[0], node[1])) else: self.path = None self.onPathFinish() # Check LOS insight = self.getFOV(world) if insight: for cell in insight: for obj in insight[cell]: self.onSight(cell, obj) Actor.tick(self, world, tick)
def __init__(self, xpos, ypos): Actor.__init__(self) self.image = pygame.image.load("assets/simon/stand.png") self.hitboxoffset = 56 self.rect = pygame.Rect(xpos+self.hitboxoffset, ypos, 32, 61) self.maxhealth = 7 self.health = self.maxhealth self.is_jumping = False self.is_climbing = False self.is_falling = False self.is_attacking = False self.left_jump = False self.right_jump = False self.is_standing = True self.is_big_toss = False self.invul = False self.invul_frame = -1 self.max_invul_frames = 120 self.inputs = [] self.attack = pygame.Rect(0, 0, 0, 0) self.attack_frame = -1 self.attack_size = (50, 15) self.move = 2 self.jump_velocity = 8.0 self.jump_decay = .25 self.velocity = 0 self.sjmod = 1 self.climb_index = -1 self.spritesheet = {} os.chdir("assets/simon") for files in os.listdir("."): if files.endswith(".png"): self.spritesheet[files] = (pygame.image.load (files).convert_alpha()) os.chdir("../..")
def __init__(self, actorJSON): Actor.__init__(self, actorJSON) attributes = { 'spawnTime' : datetime.datetime, 'pluralName' : '', 'is_perm' : False, 'wanderRate' : 0 } for key in attributes.keys(): if self.attributes.has_key(key) == False: self.attributes[key] = attributes[key] if self.attributes['is_perm']: self.attributes['adjective'] = '' else: self.attributes['adjective'] = (lambda char: ((char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u') and 'an') or 'a')(self.attributes['name'].lower()[0]) self.addEventHandlerByNameWithAdjusters('Actor.EventHandlers.NPC.GameTickedHandler', None)
def __init__(self, main): # transition from another state State.__init__(self,main) self.titleScreen = Actor(IMG_TITLE_SCREEN) self.btnStart = Actor(IMG_LABEL_START) self.btnStart2 = Actor(IMG_LABEL_START2) self.btnStart.setPos(WIDTH/2,HEIGHT-300) self.btnStart2.setPos(WIDTH/2,HEIGHT-300) self.tick = 0 self.tickInterval = 60 self.ready = False TitleState.titleGroup.add(self.titleScreen) TitleState.btnStartGroup.add(self.btnStart) pygame.mixer.init() pygame.mixer.music.load("../data/sounds/godspeed.mid") pygame.mixer.music.play()
def __init__(self, id): Actor.__init__(self) self.weakness=None self.eqItems_Ar = [] #equipped items self.attacks_Ar = [] #associated array for attack string names and attack power values self.eqItem_Ar = [] self.inv_Ar = [] self.attacks_Ar = [] self.sprite=pygame.sprite.Sprite() self.place=0 #load image based on type later self.name=ENEMY[id]['name'] self.sprite.image=pygame.image.load(CHAR_PATH + ENEMY[id]['img']) self.HP = ENEMY[id]['hp'] self.MHP=ENEMY[id]['hp'] self.ATT = ENEMY[id]['att'] self.weakness=ENEMY[id]['weak'] self.sprite.rect=(200,200,50,300)
def __init__(self, id): Actor.__init__(self) #id = 'a' self.weakness=None self.eqItems_Ar = [] #equipped items self.attacks_Ar = [] #associated array for attack string names and attack power values self.eqItem_Ar = [] self.inv_Ar = [] self.attacks_Ar = [] col,row,width,height = ENEMY[id]['sprite'] self.sprite = DynamicDrawableObject( Spritesheet(CHAR_PATH + ENEMY[id]['img'] ).img_extract(col,row,width,height) , "", 24) self.place=0 self.alive = True #load image based on type later self.name=ENEMY[id]['name'] #self.sprite.image=pygame.image.load(CHAR_PATH + ENEMY[id]['img']) self.HP = ENEMY[id]['hp'] self.MHP=ENEMY[id]['hp'] self.ATT = ENEMY[id]['att'] self.weakness=ENEMY[id]['weak']
def setup(self): pygame.init() pygame.display.set_caption("Map Search Test - David Tyler (2013)") pygame.event.set_allowed([QUIT, KEYDOWN]) screen = pygame.display.set_mode((1280, 960)) # TODO: Config options self.clock = pygame.time.Clock() # self.tile_map = MapLoader().loadMap("../res/maps/map.txt") self.tile_map = MazeMapCreator.create_maze(48, 48) # self.tile_map = CityMapCreator.create_city(48, 48) self.actor = Actor(self.tile_map, pygame.time.get_ticks()) self.tile_screen_converter = TileScreenConverter(20, 20) self.renderer = Renderer(screen, self.tile_screen_converter)
class Game: def __init__(self): self.renderer = None self.clock = None def setup(self): pygame.init() pygame.display.set_caption("Map Search Test - David Tyler (2013)") pygame.event.set_allowed([QUIT, KEYDOWN]) screen = pygame.display.set_mode((1280, 960)) # TODO: Config options self.clock = pygame.time.Clock() # self.tile_map = MapLoader().loadMap("../res/maps/map.txt") self.tile_map = MazeMapCreator.create_maze(48, 48) # self.tile_map = CityMapCreator.create_city(48, 48) self.actor = Actor(self.tile_map, pygame.time.get_ticks()) self.tile_screen_converter = TileScreenConverter(20, 20) self.renderer = Renderer(screen, self.tile_screen_converter) def run(self): if self.renderer != None and self.clock != None: while True: self.clock.tick(30) # TODO: Move event handling for event in pygame.event.get(): if event.type == QUIT: self.exit_game() elif event.type == MOUSEBUTTONDOWN and event.button == 1: x, y = self.tile_screen_converter.screen_to_tile(event.pos[0], event.pos[1]) self.actor.set_goal(x, y) self.actor.update(pygame.time.get_ticks()) self.renderer.draw_game(self.tile_map, self.actor) def exit_game(self, rc=0): pygame.quit() sys.exit(rc)
def __init__(self,player): Actor.__init__(self) self.player=player self.weapon=None self.armor=None self.accessory=None self.eqItem=[] #player can equip up to 4 usable items to use in battle self.inv_Ar = [] #inventory self.attacks_Ar = [] #associated array for attack string names and attack power values self.currentInput="" self.currentProb1=0 self.currentProb2=0 self.currentAnswer=0 self.fractionSum=0 self.akhal=0 amulet=get_item('1') calculator=get_item('s') emptyItem=None self.eqItem=[emptyItem,emptyItem,emptyItem,emptyItem] self.inv_Ar=[amulet,calculator]
def __init__(self, scale=cube_scale, actor=None): Component.__init__(self, actor) Trackable.__init__(self) self.scale = scale # Scale vertices of base cube, specify edges, and initialize list of markers self.vertices = cube_vertices * self.scale self.vertex_colors = cube_vertex_colors self.vertex_scale = 0.3 * self.scale # NOTE for rendering only, depends on 3D model self.edges = cube_edges self.edge_scale = 0.1 * self.scale # NOTE for rendering only, depends on 3D model self.edge_color = np.float32([0.8, 0.7, 0.5]) # NOTE for rendering only # TODO make some of these parameters come from XML # NOTE Mark generated child actors (corners and edges) as transient, to prevent them from being exported in XML # Add spheres at cube corners (vertices) with appropriate color; also add color markers for vertex, colorName in zip(self.vertices, self.vertex_colors): vertexActor = Actor(self.actor.renderer, isTransient=True) vertexActor.components['Transform'] = Transform(translation=vertex, scale=self.vertex_scale, actor=vertexActor) vertexActor.components['Material'] = Material(color=colors_by_name[colorName], actor=vertexActor) vertexActor.components['Mesh'] = Mesh.getMesh(src="SmallSphere.obj", actor=vertexActor) self.actor.children.append(vertexActor) marker = ColorMarker(self, colorName) marker.worldPos = vertex self.markers.append(marker) # Add edges for u, v in self.edges: if u < len(self.vertices) and v < len(self.vertices) and self.vertices[u] is not None and self.vertices[v] is not None: # sanity check midPoint = (self.vertices[u] + self.vertices[v]) / 2.0 diff = self.vertices[v] - self.vertices[u] mag = np.linalg.norm(diff, ord=2) xy_mag = hypot(diff[0], diff[1]) #zx_mag = hypot(diff[2], diff[0]) rotation = np.degrees(np.float32([atan2(diff[1], diff[0]), acos(diff[1] / mag), 0])) if (mag != 0 and xy_mag != 0) else np.float32([0.0, 0.0, 0.0]) #print "u: ", self.vertices[u], ", v: ", self.vertices[v], ", v-u: ", diff, ", mag: ", mag, ", rot:", rotation edgeActor = Actor(self.actor.renderer, isTransient=True) edgeActor.components['Transform'] = Transform(translation=midPoint, rotation=rotation, scale=self.edge_scale, actor=edgeActor) edgeActor.components['Material'] = Material(color=self.edge_color, actor=edgeActor) edgeActor.components['Mesh'] = Mesh.getMesh(src="CubeEdge.obj", actor=edgeActor) # TODO fix Z-fighting issue and use CubeEdge_cylinder.obj self.actor.children.append(edgeActor)
def readXML(self, filename): self.logger.info("Parsing file: {}".format(filename)) xmlTree = ET.parse(filename) rootElement = xmlTree.getroot() #print "Scene.readXML(): Source XML tree:-" # [debug] #ET.dump(rootElement) # [debug] # Root element must be a <scene>, containing one or more <Actor> elements # TODO Define shared resources (shaders, meshes, etc.) in a <defs> section (and then actors in <actors> or <group> sections?) if rootElement.tag == 'scene': for subElement in rootElement: if subElement.tag == 'Actor': actor = Actor.fromXMLElement(subElement, self.actorFactory) self.actors.append(actor) # include in scene hierarchy if actor.id is not None: self.actorsById[actor.id] = actor for child in actor.children: if child.id is not None: self.actorsById[child.id] = child # grab child actors as well else: self.logger.warn("Bad XML: Root must be a <scene> element!")
def kill(self): self.tree.doIn(15, "replenish", self.cell) Actor.kill(self)
def __init__(self, name): Actor.__init__(self, name) return
def setPos(self, cell): r = Actor.setPos(self, cell) # Spawn fruit for c in self.world.surroundingCells(cell): self.doIn(random.randint(2, 15), "replenish", c) return r