def on_key_press(self, key: int, modifiers: int): """ Puts the current key in the set of keys that are being held. You will need to add things here to handle firing the bullet. """ if self.ship.alive: self.held_keys.add(key) # Fire death blossom if (key == arcade.key.SPACE and modifiers == arcade.key.MOD_SHIFT and self.score >= DEATH_BLOSSOM_REQUIRED_SCORE): death_blossom = DeathBlossom(Point(x=self.ship.center.x, y=self.ship.center.y), Velocity(dx=self.ship.velocity.dx, dy=self.ship.velocity.dy), angle=self.ship.angle) self.bullets.append(death_blossom) self.score -= DEATH_BLOSSOM_REQUIRED_SCORE # Fire bullet elif key == arcade.key.SPACE: # TODO: Fire the bullet here! bullet = Bullet(Point(x=self.ship.center.x, y=self.ship.center.y), Velocity(dx=self.ship.velocity.dx, dy=self.ship.velocity.dy), angle=self.ship.angle) self.bullets.append(bullet)
def __init__(self, point): self.point = point self.velocity = Velocity(0, 0, 0) self.states = [self.__repr__()] self.orbit_time = None self.initial_point = point.copy() self.initial_velocity = Velocity(0, 0, 0) self.do_x = True self.do_y = True self.do_z = True
def __init__(self): self.center = Point() self.velocity = Velocity() self.center.x = BALL_RADIUS self.center.y = random.uniform(BALL_RADIUS, SCREEN_HEIGHT - BALL_RADIUS) self.velocity.dx = random.uniform(1, 5) self.velocity.dy = random.uniform(1, 5)
def __init__(self, screen_width, screen_height): """ Initialize a BigRock :param screen_width: int :param screen_height: int """ # Define acceptable range for big rocks to start (far enough away from the ship that # the ship is not instantly killed) acceptable_start_x = [ x for x in range(screen_width) if not screen_width / 3 < x < 2 * screen_width / 3 ] acceptable_start_y = [ y for y in range(screen_height) if not screen_height / 3 < y < 2 * screen_height / 3 ] super().__init__( center=Point(x=random.choice(acceptable_start_x), y=random.choice(acceptable_start_y)), velocity=Velocity( dx=math.cos(random.uniform(0, 2 * math.pi)) * BIG_ROCK_SPEED, dy=math.sin(random.uniform(0, 2 * math.pi)) * BIG_ROCK_SPEED, da=BIG_ROCK_SPIN), radius=BIG_ROCK_RADIUS)
def __init__(self): self.x = PLAYER_X self.y = PLAYER_Y self.width = PLAYER_WIDTH self.height = PLAYER_HEIGHT self.color = RED self.velocity = Velocity()
def __init__(self): self.x = BALL_X self.y = BALL_Y self.width = BALL_SIZE self.height = BALL_SIZE self.color = RED self.velocity = Velocity()
def break_apart(self): """ Define what happens when something collides with a BigRock :return: a list of smaller Rock objects """ debris1 = MediumRock( Point(self.center.x, self.center.y), Velocity(self.velocity.dx, self.velocity.dy + 2, MEDIUM_ROCK_SPIN)) debris2 = MediumRock( Point(self.center.x, self.center.y), Velocity(self.velocity.dx, self.velocity.dy - 2, MEDIUM_ROCK_SPIN)) debris3 = SmallRock( Point(self.center.x, self.center.y), Velocity(self.velocity.dx + 5, self.velocity.dy, SMALL_ROCK_SPIN)) self.kill() return [debris1, debris2, debris3]
def break_apart(self): """ Define what happens when something collides with a MediumRock object :return: a list of smaller Rock objects """ debris1 = SmallRock( Point(self.center.x, self.center.y), Velocity(self.velocity.dx + 1.5, self.velocity.dy + 1.5, SMALL_ROCK_SPIN)) debris2 = SmallRock( Point(self.center.x, self.center.y), Velocity(self.velocity.dx - 1.5, self.velocity.dy - 1.5, SMALL_ROCK_SPIN)) self.kill() return [debris1, debris2]
def __init__(self): """ Initializes all essential variables for a flying object. """ self.center = Point() self.velocity = Velocity() self.speed = 0 self.radius = 0 self.angle = 90
def __init__(self): self.center = Point() self.velocity = Velocity() self.alive = True self.img = "" self.radius = 0.0 self.angle = 0.0 self.speed = 0.0 self.direction = 0.0 self.alpha = 255
def __init__(self): self.center = Point() # initially 0 for x and y self.velocity = Velocity() # initially 0 for dx and dy self.alive = True # always start being alive self.height = 0 # figure height self.width = 0 # figure width self.texture = arcade.load_texture("images/playerShip1_orange.png") # import the texture self.rotation = 0 # how much the figure is rotation self.alpha = 1000 # will not be transparent self.radius = 0
def __init__(self): super().__init__() if random.random() * 100 % 2 > 1: # so center will have an x = 0 and velocity will need to be x + self.center = Point(0, random.random() * constants.SCREEN_HEIGHT) self.velocity = Velocity(2.0, 2.0) else: # so center will have an x = SCREEN_WIDTH and velocity will need to be x - self.center = Point(constants.SCREEN_WIDTH, random.random() * constants.SCREEN_HEIGHT) self.velocity = Velocity(-2.0, 2.0) if random.random() * 100 % 2 > 1: self.texture = arcade.load_texture(constants.PATH_IMAGES + 'alien_ship_green-sm.png') else: self.texture = arcade.load_texture(constants.PATH_IMAGES + 'alien_ship_green.png') self.radius = self.texture.width // 2 * 0.75 self.angle = 0
def __init__(self,center=None): self.alive = True self.velocity = Velocity() self.isWrapX = False self.isWrapY = False if center == None: self.center = Point() else: self.center = center.copy()
def __init__(self, x=0, y=0, dx=1, dy=1): """ Initialize flying object :param x: :param y: :param dx: :param dy: """ self.center = Point(x, y) self.velocity = Velocity(dx, dy)
def __init__(self): #create a new ball with a point at the center and a velocity self.center = Point() self.velocity = Velocity() #set x to the balls radius so that it appears on the screen self.center.x = BALL_RADIUS #set y to a random number that is between the balls radius and the highest point subtract the radius self.center.y = random.uniform(BALL_RADIUS, SCREEN_HEIGHT - BALL_RADIUS) #assign the ball a random velocity self.velocity.dx = random.uniform(0.25, 5) self.velocity.dy = random.uniform(0.25, 5)
def __init__(self, angle=0.0, velocity=Velocity(0, 0)): super().__init__() self._angle = angle self.dx = velocity.dx self.dy = velocity.dy self.focal_point = Point() # the momentum in the rotation self.delta_angle = 0 # drag represents the inertia decay of movements. if this is # set to AngularVelocity.MAX_VIABILITY, rotation will have no inertia. self._drag = .85 # percentage by which drag decays from it's maximum each cycle. self.decay = 0.05
def __init__(self): # while Flyer should be considered abstract, this lays out # the framework here. See notes above. self.name = self.__class__.__name__ self.center = Point() self.velocity = Velocity() self.radius = 1 self.alive = True # all flyers have a color. Here to back up any that don't specify self.color = arcade.color.PINK self.angle = 0 # loading an empty image for this. Re-evaluate if performance hit. self.texture = None # arcade.load_texture(constants.PATH_IMAGES + 'stub.png')
def __init__(self, startPoint=None, startVelocity=None): super().__init__() #arcade image self.texture = None #arcade.load_texture(None) self.spin = 0 if startPoint is None: self.center = Point(random.random() * constants.SCREEN_WIDTH, random.random() * constants.SCREEN_HEIGHT) else: self.center = Point(startPoint.x, startPoint.y) if startVelocity is not None: self.velocity = Velocity(startVelocity.dx, startVelocity.dy) self.angle = random.random() * 360 self.points = 0 self._damage = 10
def __init__(self, center=Point(), velocity=Velocity(), radius=10, angle=0): """ Initialize flying object :param center: Point object :param velocity: Velocity object :param radius: int :param angle: int in degrees """ self._center = center self._velocity = velocity self._radius = radius self._angle = angle self._alive = True
def __init__(self, center=Point(), velocity=Velocity(), radius=10, angle=0): """ Initialize a flying rock :param center: :param velocity: :param radius: :param angle: """ super().__init__(center=center, velocity=velocity, radius=radius, angle=angle) self.spin = 0
def __init__(self, screen, amp=0, rad=0, angle=0, pos=(0, 0), radius=10, width=0, max_speed=20, color=(0, 0, 0)): Velocity.__init__(self, amp, rad, angle, max_speed) self.ball_color = color self.ball_acc = Velocity(0.3, angle=90) # 小球加速度 self.centerx = pos[0] self.centery = pos[1] self.radius = radius self.width = width self.screen = screen
def __init__(self, image): # Status self.center = Point() self.velocity = Velocity() self.alive = True # Movement self.speed = 1 self.angle = 1.0 self.direction = 1 # Dimensions/View self.img = image self.texture = arcade.load_texture(self.img) self.height = self.texture.height self.width = self.texture.width self.radius = SHIP_RADIUS
def fire(self, target): """ Returns an AlienBullet with a trajectory towards the target's' current location. """ angle = math.atan2(target.center.y - self.center.y, target.center.x - self.center.x) angle = angle * 180 / math.pi #convert to degrees # put the origin point of the bullet far enough # away it does not photon itself. radius_adjust = self.texture.height / 2 * 1.25 laser_barrel_end = Point(radius_adjust * Velocity.cosine(angle), radius_adjust * Velocity.sine(angle)) laser_barrel_end = laser_barrel_end + self.center p = Point(laser_barrel_end.x, laser_barrel_end.y) v = Velocity(self.velocity.dx, self.velocity.dy) return AlienBullet(p, angle, v)
def __init__(self): self.center = Point(390, 50) self.velocity = Velocity(0, 5)
def cpso(wf_words: list): """ cpso算法 :param wf_words: 工作流单词数 :return: None """ # 初始化工作流 fitness_res = [] particle_res = [] wfs = [] for word_cnt in wf_words: wfs.append(WorkFlow(word_cnt)) # 初始化资源 resources = [[] for _ in range(2)] resources[0] = [] resources[1] = [] # resources[0].append(Translator(speed=400)) # resources[0].append(Translator(speed=400)) resources[0].append(Translator(speed=400, name="翻译1")) resources[0].append(Translator(speed=400, name="翻译2")) resources[0].append(Translator(speed=400, name="翻译3")) resources[0].append(Translator(speed=500, name="翻译4")) # resources[0].append(Translator(speed=450)) # resources[0].append(Translator(speed=500)) # resources[0].append(Translator(speed=600)) # resources[0].append(Translator(speed=700)) resources[1].append(Auditor(speed=800, name="审核1")) resources[1].append(Auditor(speed=800, name="审核2")) # resources[1].append(Auditor(speed=800)) # resources[2].append(Auditor(speed=600)) # resources[2].append(Auditor(speed=700)) # resources[2].append(Auditor(speed=800)) # resources[1].append(Auditor(speed=800)) # 初始化粒子数 particles = [Particle(random.sample(wfs, len(wfs))) for _ in range(N)] # for _particle in particles: # print(_particle) # return # 初始化速度集合 velocities = [Velocity(len(wf_words), True, int(len(wf_words) * 0.3)) for _ in range(N)] # for _v in velocities: # _v.print_rf() # return # print(id(particles)) # print(id(particles[0])) # particles_copy = copy.deepcopy(particles) # print(id(particles_copy[0])) # print(len(particles)) # print(particles[0]) # 局部最优解 local_optimal_solution = copy.deepcopy(particles) # local_optimal_solution_value = [None for _ in range(len(wfs))] # 全局最优解 global_optimal_solution = None # global_optimal_solution_value = None # 迭代 for i in range(L): for j in range(len(particles)): _particle = particles[j] # 当前粒子适应度 _res = fitness(_particle.wf_seq, resources) # print('FITNESS: ', _res) if local_optimal_solution[j].fitness is None or local_optimal_solution[j].fitness > _res: # 更新局部最优解 local_optimal_solution[j] = copy.deepcopy(_particle) local_optimal_solution[j].fitness = _res if global_optimal_solution is None or (global_optimal_solution.fitness is not None and global_optimal_solution.fitness > _res): # 更新全局最优解 global_optimal_solution = copy.deepcopy(_particle) global_optimal_solution.fitness = _res # global_optimal_solution_value = _res # print(_res) # 使用速度集合更新位置 if isinstance(_particle, Particle): # print('Before update: ', _particle) # print('Velocity: ', velocities[j]) _particle.update(velocities[j]) # print('After update: ', _particle) # 更新速度 for _i in range(len(velocities)): # print('Before update velocity: ', velocities[_i]) _v = velocities[_i] _v.update(particles[_i], local_optimal_solution[_i], global_optimal_solution, get_w(i)) # print('After update velocity: ', velocities[_i]) print(velocities[0]) # print(particles[0]) # 打印全局最优解 # print('全局最优解: ', global_optimal_solution) fitness_res.append(global_optimal_solution.fitness) particle_res.append(global_optimal_solution) # print(global_optimal_solution) # for _v in velocities: print(_v) # for f in fitness_res: # print(f) for f in sorted(list(set(fitness_res)), reverse=True): print(f) print(global_optimal_solution) for _resource_li in resources: for _resource in _resource_li: _resource.print_tasks()
# Dictionary for holding positions positions = { 'current_marker': -1, 'current_us': -1, 'current_enc': -1, 'current_cam': -1, 'current_moving_avg_us': -1, 'current_complementary': -1, 'current_kalman': -1 } # A Velocity class object for holding and updating velocities velocities = Velocity({ 'us': 0, 'enc': 0, 'cam': 0, 'moving_avg_us': 0, 'complementary': 0, 'kalman': 0 }) ############################################ # Task 2: Implement moving average filter # ############################################ my_list = [] def moving_average(pos): global my_list # Fill in the function. limit = 10 my_list.append(pos)
def __init__(self): self.center = Point() self.velocity = Velocity() self.radius = 0.0 self.alive = True self.angle = 0
def save_fig(file_name,save_name, x, y): plt.figure() plt.title(save_name) plt.ylim([0, 200]) plt.plot(x, y) plt.savefig('./20180903_{}/{}.png'.format(save_name, file_name)) videos = glob.glob('./20180903/*') list_result = [] output_list = [] with open('./bgr_list_20180903.csv', 'a') as f: writer = csv.writer(f) for i, file in enumerate(videos): file_path = file print (file_path) file_name = file_path[2:17] print (file_name) source = Velocity(file_path) x, y, bgr_list = source.manage() # save_fig(file_name[9:15], 'amplitude', x, y) # save_fig(file_name[9:15], 'wave', list(range(len(bgr_list))), bgr_list) list_result += y output_list.append(bgr_list) writer.writerow(output_list) output_list.clear() # plt.figure() # plt.hist(list_result, bins=256) # plt.savefig('./histgram.png')
def get_random_velocity(self, negative_limit, positive_limit): random.seed() dx = random.uniform(negative_limit, positive_limit) dy = random.uniform(negative_limit, positive_limit) return Velocity(dx, dy)
def __init__(self, starting_point): self.alive = True self.velocity = Velocity(0, 0) self.center = starting_point