def use_weapon(self, player): # self is da gun current_time = now() bullet_position_vector = get_vector_to_position( player.mouse_position, self.window_position_center) time_since_fire = current_time - player.last_fire_time shot = [] if time_since_fire >= self.delay and self.ammo > 0 and not player.is_reloading: #player shot self.calculate_end_of_barrel(player) player.is_shooting = True player.last_fire_time = current_time self.ammo -= 1 for a in range(1, self.shoots_this_many_at_a_time + 1): shot.append( PredatorMissile( self.end_of_barrel, self, player, euclid.Vector2( (bullet_position_vector.x + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity, (bullet_position_vector.y + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity))) return shot return None
def use_weapon(self, player): # self is da gun current_time = now() bullet_position_vector = get_vector_to_position(player.mouse_position, self.window_position_center) time_since_fire = current_time - player.last_fire_time shot = [] if time_since_fire >= self.delay and self.ammo > 0 and not player.is_reloading: #player shot self.calculate_end_of_barrel(player) player.is_shooting = True player.last_fire_time = current_time self.ammo -= 1 for a in range(1, self.shoots_this_many_at_a_time + 1): shot.append(Projectile( self.end_of_barrel, self, player, euclid.Vector2((bullet_position_vector.x + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity, (bullet_position_vector.y + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity))) return shot return None
def animate_movement(self, left_to_right=True): current_time = now() time_to_switch = (current_time - self.last_image_change_time) > self.run_time reset = False if time_to_switch: #left_to_right will either display the images #going forward or backwards if left_to_right: self.use_image_at += 1 if self.use_image_at >= len(self.image_list): self.use_image_at = 0 reset = True if self.function_on_reset: self.function_on_reset() else: # running backwards self.use_image_at -= 1 if self.use_image_at <= -1: self.use_image_at = len(self.image_list) - 1 reset = True if self.function_on_reset: self.function_on_reset() self.last_image_change_time = current_time self.owner.image = self.image_list[self.use_image_at] return reset
def on_coffee_started(self): if not self.pill_handle and self.is_armed(TRIGGER1): self.log('Coffee is brewing, start pills reminder cycle') self.pill_handle = self.run_every( self.reminder, globals.now() + datetime.timedelta(seconds=1), self.repeat_every) else: self.log('Coffee is brewing, but reminders are switched off')
def dash(self, velocity): if self.is_grappled(): self.velocity.x = velocity # make it so you move when you dash from a grapple else: self.velocity.x += velocity # let the players velocity go past their max speed self.last_dash_time = now() self.is_dashing = True self.is_reloading = False # dashing cancels reloading self.grapple_projectile.is_alive = False # get rid of last grapple
def reload(self): current_gun = self.weapons[IS_EQUIPPED] #check if player can reload if self.time_since_reload() > current_gun.reload_time and current_gun.remaining_ammo != 0 and\ current_gun.ammo != current_gun.magazine_size: #now player is reloading and player can't shoot self.last_reload_time = now() current_gun.is_charging = False self.is_reloading = True self.update_ammo_hud()
def special_equipment_logic(self, user_input): if user_input[GRAPPLE]: grapple = self.weapons[GRAPPLING_HOOK].use_weapon(self) if grapple is not None: self.last_grapple_time = now() self.add_list_to_world(grapple, "projectiles") # make chain projectiles if self.grapple_projectile.is_alive: links = self.grapple_projectile.make_chain() self.add_list_to_world(links, "projectiles")
def use_weapon(self, player): """starts the charge""" current_time = now() time_since_fire = current_time - player.last_fire_time # check if the player is starting a charge or not if self.ammo > 0 and time_since_fire >= DOUBLE_TAP_MIN and not self.is_charging: self.is_charging = True self.last_charge_time = current_time player.last_fire_time = current_time return None
def use_weapon(self, player): """starts the charge""" current_time = now() time_since_fire = current_time - player.last_fire_time # check if the player is starting a charge or not if self.ammo > 0 and time_since_fire >= DOUBLE_TAP_MIN and not self.is_charging: self.is_charging = True self.last_charge_time = current_time player.last_fire_time = current_time return None
def start_animation_constant_size(self, scale_width, scale_height, loop=False, reruns=0): self.start_time = now() self.is_animating = True self.loop = loop self.reruns = reruns self.scale_width = scale_width self.scale_height = scale_height self.scale_flag = True
def right_logic(self, user_input): if user_input[RIGHT]: if user_input[DASH_RIGHT] and self.can_dash() and not self.is_dashing: self.dash(-self.dash_acceleration.x) elif self.can_jump: # on the ground self.walk(-self.acceleration.x) else: # in the air self.walk(-self.in_air_acceleration) if not self.is_moving_right: # player just turned around self.turn_around() self.last_right_time = now()
def left_logic(self, user_input): if user_input[LEFT]: if user_input[DASH_LEFT] and self.can_dash() and not self.is_dashing: self.dash(self.dash_acceleration.x) elif self.can_jump: self.walk(self.acceleration.x) else: self.walk(self.in_air_acceleration) if self.is_moving_right: # player just turned around self.turn_around() self.last_left_time = now()
def use_weapon(self, player): current_time = now() bullet_position_vector = get_vector_to_position(player.mouse_position, player.window_position_center) time_since_fire = current_time - player.last_grapple_time if time_since_fire >= self.delay and self.ammo > 0: #player shot player.last_grapple_time = current_time self.ammo -= 1 player.grapple_projectile.is_alive = False # get rid of last grapple grapple_projectile = Grapple(player.get_center(), self, player, euclid.Vector2((bullet_position_vector.x * self.bullet_velocity), (bullet_position_vector.y * self.bullet_velocity))) player.grapple_projectile = grapple_projectile # new grapple return [grapple_projectile] return None
def updateuser_input(self): cur_time = now() if self.user_input[LEFT]: time = cur_time - self.left_time if self.delta_time * 2.0 <= time <= self.double_tap_max and self.right_time < self.left_time: self.user_input[DASH_LEFT] = True else: self.user_input[DASH_LEFT] = False self.left_time = cur_time if self.user_input[RIGHT]: time = cur_time - self.right_time if self.delta_time * 2.0 <= time <= self.double_tap_max and self.left_time < self.right_time: self.user_input[DASH_RIGHT] = True else: self.user_input[DASH_RIGHT] = False self.right_time = cur_time
def updateuser_input(self): cur_time = now() if self.user_input[LEFT]: time = cur_time - self.left_time if self.delta_time * 2.0 <= time <= self.double_tap_max and self.right_time < self.left_time: self.user_input[DASH_LEFT] = True else: self.user_input[DASH_LEFT] = False self.left_time = cur_time if self.user_input[RIGHT]: time = cur_time - self.right_time if self.delta_time * 2.0 <= time <= self.double_tap_max and self.left_time < self.right_time: self.user_input[DASH_RIGHT] = True else: self.user_input[DASH_RIGHT] = False self.right_time = cur_time
def use_weapon(self, player): current_time = now() bullet_position_vector = get_vector_to_position( player.mouse_position, player.window_position_center) time_since_fire = current_time - player.last_grapple_time if time_since_fire >= self.delay and self.ammo > 0: #player shot player.last_grapple_time = current_time self.ammo -= 1 player.grapple_projectile.is_alive = False # get rid of last grapple grapple_projectile = Grapple( player.get_center(), self, player, euclid.Vector2( (bullet_position_vector.x * self.bullet_velocity), (bullet_position_vector.y * self.bullet_velocity))) player.grapple_projectile = grapple_projectile # new grapple return [grapple_projectile] return None
def shoot_unpressed(self, player): bullet_position_vector = get_vector_to_position( player.mouse_position, self.window_position_center) shot = [] if self.ammo > 0 and not player.is_reloading: #player shot self.calculate_end_of_barrel(player) self.is_charging = False player.is_shooting = True player.last_fire_time = now() self.ammo -= 1 # set attributes to charged mode if not (now() - self.last_charge_time) * 15 < 10: self.bullet_size_x = ((now() - self.last_charge_time) * 15) self.bullet_size_y = ((now() - self.last_charge_time) * 15) self.damage = ((now() - self.last_charge_time) * 15) self.bullet_range = ((now() - self.last_charge_time) * 15) for a in range(1, self.shoots_this_many_at_a_time + 1): shot.append( Projectile( self.end_of_barrel, self, player, euclid.Vector2( (bullet_position_vector.x + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity, (bullet_position_vector.y + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity))) # set attributes back to normal self.bullet_size_x = self.original_bullet_size_x self.bullet_size_y = self.original_bullet_size_y self.bullet_range = self.original_bullet_range self.damage = self.original_damage return shot
def shoot_unpressed(self, player): bullet_position_vector = get_vector_to_position(player.mouse_position, self.window_position_center) shot = [] if self.ammo > 0 and not player.is_reloading: #player shot self.calculate_end_of_barrel(player) self.is_charging = False player.is_shooting = True player.last_fire_time = now() self.ammo -= 1 # set attributes to charged mode if not (now() - self.last_charge_time) * 15 < 10: self.bullet_size_x = ((now() - self.last_charge_time) * 15) self.bullet_size_y = ((now() - self.last_charge_time) * 15) self.damage = ((now() - self.last_charge_time) * 15) self.bullet_range = ((now() - self.last_charge_time) * 15) for a in range(1, self.shoots_this_many_at_a_time + 1): shot.append(Projectile( self.end_of_barrel, self, player, euclid.Vector2((bullet_position_vector.x + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity, (bullet_position_vector.y + random.uniform(-self.accuracy, self.accuracy)) * self.bullet_velocity))) # set attributes back to normal self.bullet_size_x = self.original_bullet_size_x self.bullet_size_y = self.original_bullet_size_y self.bullet_range = self.original_bullet_range self.damage = self.original_damage return shot
def initialize(self): self.location = self.args['location'] self.handle = self.run_every(self.my_callback, globals.now(), 15*60) self.errors = self.get_app('errors')
from tqdm import tqdm from functions import matyas_func, rastrigin_func, levi_func from optimization.abc import ABC from optimization.eba import EBA from optimization.pso import PSO from globals import X_RESEARCH, now, time_limit import matplotlib.pyplot as plt fun = matyas_func plt.yscale('log') x = X_RESEARCH # --some-computation-- start = now() pso = PSO(stop_func=lambda i: now() - start < time_limit) pso_res = pso.optimize(fun, x) # pso.optimize(x) start = now() abc = ABC(stop_func=lambda i: now() - start < time_limit) abc_res = abc.optimize(fun, x) # ba.optimize(x) start = now() eba = EBA(stop_func=lambda i: now() - start < time_limit) eba_res = eba.optimize(fun, x) print("PSO") plt.plot(pso_res, label="PSO") print("ABC")
def time_since_reload(self): return now() - self.last_reload_time
def dash(self, velocity): self.velocity.x += velocity # let the players velocity go past their max speed self.last_dash_time = now() self.is_dashing = True self.is_reloading = False # dashing cancels reloading
def not_walking(self, delta_time): return now() - self.last_left_time > delta_time and now() - self.last_right_time > delta_time
from tqdm import tqdm from functions import matyas_func, rastrigin_func, levi_func from optimization.abc import ABC from optimization.eba import EBA from optimization.pso import PSO from globals import X_RESEARCH, now, time_limit import matplotlib.pyplot as plt import numpy as np fun = matyas_func plt.yscale('log') x = X_RESEARCH res = [] for val in np.linspace(0, 1, 10, endpoint=True): semi_res = [] for i in range(10): start = now() alg = ABC(stop_func=lambda i: now() - start < time_limit) best = alg.optimize(fun, x)[-1] semi_res.append(best) res.append(np.mean(semi_res)) plt.plot(res) plt.show()
def fire_alarm(self): if not self.alarm_handle: self.l.debug('Alarm started') self.alarm_handle = self.run_every(self.alarm, globals.now(), 15)
def can_dash(self): return now() - self.last_dash_time > self.dash_delay
def initialize(self): #self.handle = self.run_in(self.my_callback, 5) self.listen_state(self.on_masha_door_changed, "binary_sensor.door_masha") #self.handle = self.run_hourly(self.my_callback, None) self.run_every(self.my_callback, globals.now(), 5 * 60)
def initialize(self): self.path = self.args['shelf_path'] self.run_every(self.update_all_entities, globals.now(), self.args['update_period']) self.listen_state(self.state_handler)
def initialize(self): self.token = self.args['token'] self.handle = self.run_hourly(self.get_tasks, globals.now()) self.set_state("sensor.todoist_morning_message", state='?')