def play_guitar(self): # Maybe make some real noise? if isinstance(self.weapon, SlashSuperWeapon): return SuperSoundWave(self.position + Vector3(0., self.size, 0.), self.radius) elif isinstance(self.weapon, SlashWeapon): return NormalSoundWave(self.position + Vector3(0., self.size, 0.), self.radius)
def __init__(self, linear_max_speed, angular_max_speed, position, orientation, colors, size, mass, weapon, hit_force, hit_damage, max_acc, std_initial_force=None): self.max_speed = linear_max_speed self.max_rotation = angular_max_speed self.area = Rect((position.x, position.z), size * 2, size * 2) self.radius = sqrt(2 * (size**2)) self.height = size * 2 # Optional, color and stuff self.colors = colors self.size = size self.mass = mass self.hit_force = hit_force self.hit_damage = hit_damage # Kinematic data self.position = position self.sector_position = None self.orientation = orientation self.velocity = Vector3() self.rotation = 0. # Steering output self.acceleration = Vector3() self.behave_acceleration = Vector3() self.hitting_acceleration = Vector3() self.bullet_acceleration = Vector3() self.angular = 0. self.behave_angular = 0. if std_initial_force is None: self.std_acc_step = STANDARD_INITIAL_FORCE / self.mass else: self.std_acc_step = std_initial_force / self.mass self.max_acc = max_acc self.std_ang_step = .1 self.max_ang = 15. # Control options self.energy = 100 self.dying = False self.hitting = False self.shooting = False self.jumping = False self.jumping_initial_speed = 13. self.weapon = weapon
def execute(self, *args, **kwargs): steering = {} # Updates the wander orientation self.wander_orientation += random_binomial() * self.wander_rate orientation = self.wander_orientation + self.character.orientation target_circle = self.character.position + Vector3.from_orientation( self.character.orientation, self.wander_offset ) target_circle += Vector3.from_orientation(orientation, self.wander_radius) # Delegates to seek look_where_you_are_going(self.character) return seek(self.character, target_circle, *args, **kwargs)
def __init__(self, max_speed, max_rotation, name='', position=Vector3(), orientation=0., hearing_umbral=1., behavior_groups=[]): Character.__init__(self, max_speed, max_rotation, position, orientation, colors=[(126. / 255, 190. / 255, 228. / 255), (39. / 255, 107. / 255, 148. / 255)], size=1., mass=1., weapon=EnemyNormalWeapon(), hit_force=35., hit_damage=5., max_acc=48.) # Behaviors self.last_behavior = None self.behaviors = dict([(bg.name, bg) for bg in behavior_groups]) #self.image, self.rect = load_image('main_character.png') # Control self.name = name self.hearing_umbral = hearing_umbral self.obstacle_evading = False self.dizzy = None # will save a tuple of the form: (dizzy_begin_time, dizzy_duration) self.waypoint_taken = None self.not_so_smart = False
def __init__(self, max_speed, max_rotation, position=Vector3(), orientation=0.): Character.__init__(self, max_speed, max_rotation, position, orientation, colors=[(1., 155. / 255, 0.), (1., 85. / 255, 0.)], size=1.2, mass=1.2, weapon=SlashWeapon(), hit_force=100., hit_damage=20., max_acc=20.) #self.image, self.rect = load_image('main_character.png') self.behavior = Behavior(character=self, active=True, **LOOK_WHERE_YOU_ARE_GOING) # Control self.playing = False self.rock_bar = 0. self.super_powerfull = None self.steps = []
def die(self, on_stage=False): if self.dying: return # leave me alone, dying if on_stage: self.velocity += Vector3(0., -5., 0.) if hasattr(self, 'behaviors'): self.behaviors = {} self.dying = True
def __init__(self, level=None): # Set the games objects self.clock = pygame.time.Clock() self.main_character = Slash(20.,20.,position=Vector3(0.,0.,-1.), orientation=0.) self.enemies = [] self.characters = [self.main_character] self.projectiles = [] # projectiles pool self.sound_wave = None # sound wave 'pool' self.stage = Stage(STAGE_SIZE) # Control self.print_debug = False self.last_enemy_addition = 0
def stepping(self): current_time = pygame.time.get_ticks() if self.steps: step = self.steps[0] else: step = {'last_time': 0, 'wave': None} if current_time - step['last_time'] > STEP_FRAME: # create another step self.steps.append({ 'last_time': current_time, 'wave': StepSoundWave(self.position + Vector3(0., .5, 0.), self.radius) })
def shoot(self): """ Defines shooting. """ bullet_position = \ Vector3(self.weapon.size * sin(self.orientation) * cos(radians(self.weapon.orientation)), self.weapon.size * sin(radians(self.weapon.orientation)) + self.size, self.weapon.size * cos(self.orientation) * cos(radians(self.weapon.orientation))) bullet_velocity = bullet_position.copy() bullet_velocity.y -= self.size bullet_position += self.position bullet_velocity.set_length(self.weapon.shooting_force) bullet = self.weapon.bullet_class(position=bullet_position, velocity=bullet_velocity, owner=self) self.shooting = False return bullet
def check_for_bullets(self, game): for projectile in game.projectiles: distance = (self.position - projectile.position).length if distance < self.radius + projectile.radius: # Oh-oh, it hit me! self.energy -= projectile.damage # Lets give it up to slash, if he shoot if isinstance(projectile.owner, Slash) and \ not projectile.owner.is_kicking_asses: projectile.owner.rock_bar += projectile.damage / 2 self.bullet_acceleration = Vector3(projectile.velocity.x, 0., projectile.velocity.z) try: self.bullet_acceleration.set_length(projectile.hit_force / \ (self.mass * (self.energy / 100.))) except ZeroDivisionError: self.bullet_acceleration.set_length(projectile.hit_force / \ 0.000001) projectile.explode(game)
def shake(self): """ Makes the character shake of fear """ self.velocity.set_length(0) self.behave_acceleration = Vector3()
def reset_velocity(self, game, obj=None, wall=False): """ Solves tridimensional calculation of velocities after a collision. """ if wall: if (self.acceleration * self.mass).length > 3500.: self.die() self.acceleration += GRAVITY * 2 # Has to guess with which side are we hitting if game.stage.floor.area.collide_point(*self.area.center): # If my center is still on the stage if game.stage.floor.size - abs(self.area.center[0]) <= \ game.stage.floor.size - abs(self.area.center[1]): # We are closer to an horizontal edge self.velocity.x *= -1 self.hitting_acceleration.x *= -1 else: # We are closer to an vertical edge self.velocity.z *= -1 self.hitting_acceleration.z *= -1 else: # My center is out of the stage self.velocity *= -1 self.hitting_acceleration *= -1 return collision_axis = (self.position - obj.position).normalize() self_speed_x = self.velocity.dot(collision_axis) obj_speed_x = obj.velocity.dot(collision_axis) linear_momentum = self_speed_x * self.mass + obj_speed_x * obj.mass vel_reflection = self_speed_x - obj_speed_x # Find the elastic collision result # Find velocity components vectors according to the collision axis self_collision_vx = self.velocity.projection(collision_axis) self_collision_vy = self.velocity - self_collision_vx obj_collision_vx = obj.velocity.projection(collision_axis) obj_collision_vy = obj.velocity - obj_collision_vx # Resolve equation system. self_new_vx = (linear_momentum - obj.mass*vel_reflection) / \ (self.mass + obj.mass) obj_new_vx = self_speed_x - obj_speed_x + self_new_vx # Gets the vectors try: self_new_vx = collision_axis.copy().set_length(abs(self_new_vx)) * \ (self_new_vx/abs(self_new_vx)) except ZeroDivisionError: self_new_vx = Vector3() try: obj_new_vx = collision_axis.copy().set_length(abs(obj_new_vx)) * \ (obj_new_vx/abs(obj_new_vx)) except ZeroDivisionError: obj_new_vx = Vector3() # Set new velocities self.velocity = self_collision_vy + self_new_vx obj.velocity = obj_collision_vy + obj_new_vx # Check for hitting if self.hitting and hit_detection(self, obj): # If i'm really hitting the 'obj' character, we'll change its mass # to a fuzzy mass depending on its damage obj.energy -= self.hit_damage obj_hit_acc_length = obj.hitting_acceleration.length obj.hitting_acceleration = obj.velocity.copy() try: obj.hitting_acceleration.set_length(obj_hit_acc_length + \ self.hit_force / (obj.mass * (obj.energy / 100.))) except ZeroDivisionError: obj.hitting_acceleration.set_length(obj_hit_acc_length + \ self.hit_force / (obj.mass * 0.0001)) if hasattr(obj, 'hitting') and obj.hitting and hit_detection( obj, self): # Same as the upper if. self.energy -= obj.hit_damage try: hit_acc_result = obj.hit_force / (self.mass * (self.energy / 100.)) except ZeroDivisionError: hit_acc_result = obj.hit_force / (self.mass * 0.0001) self_hitted = True else: self_hitted = False hit_acc_length = self.hitting_acceleration.length self.hitting_acceleration += self.velocity if self_hitted: self.hitting_acceleration.set_length(hit_acc_length + hit_acc_result) else: self.hitting_acceleration.set_length(hit_acc_length)
def keymap_handler(game): pressed = pygame.key.get_pressed() # Game keyhandling # ================ if pressed[K_0]: game.print_debug = not game.print_debug # Main Character # ============== # Canon and hitting if pressed[K_h]: if game.main_character.hitting: return game.main_character.hitting = True # Canon and shooting if pressed[K_y]: if game.main_character.weapon.orientation < 90: game.main_character.weapon.orientation += 3 if pressed[K_u]: if game.main_character.weapon.orientation > 0: game.main_character.weapon.orientation -= 3 if pressed[K_n]: if game.main_character.weapon.shooting_force < 50: game.main_character.weapon.shooting_force += 2 if pressed[K_m]: if game.main_character.weapon.shooting_force > 0: game.main_character.weapon.shooting_force -= 2 if pressed[K_j]: if game.main_character.hitting: # If I'm hitting, i can't shoot return game.main_character.shooting = True # Playing guitar if pressed[K_k]: game.main_character.playing = True # Movement # First handle two keys pressed at the same time. if game.main_character.jumping: return if pressed[K_SPACE]: game.main_character.jump() return if pressed[K_d] and pressed[K_w]: game.main_character.accelerate( Vector3((game.main_character.std_acc_step * sin(pi / 4)), 0., -(game.main_character.std_acc_step * sin(pi / 4)))) return elif pressed[K_d] and pressed[K_s]: game.main_character.accelerate( Vector3((game.main_character.std_acc_step * sin(pi / 4)), 0., (game.main_character.std_acc_step * sin(pi / 4)))) return elif pressed[K_a] and pressed[K_w]: game.main_character.accelerate( Vector3(-(game.main_character.std_acc_step * sin(pi / 4)), 0., -(game.main_character.std_acc_step * sin(pi / 4)))) return elif pressed[K_a] and pressed[K_s]: game.main_character.accelerate( Vector3(-(game.main_character.std_acc_step * sin(pi / 4)), 0., (game.main_character.std_acc_step * sin(pi / 4)))) return if pressed[K_d]: game.main_character.accelerate( Vector3(game.main_character.std_acc_step, 0., 0.)) return elif pressed[K_a]: game.main_character.accelerate( Vector3(-game.main_character.std_acc_step, 0., 0.)) return if pressed[K_s]: game.main_character.accelerate( Vector3(0., 0., game.main_character.std_acc_step)) return if pressed[K_w]: game.main_character.accelerate( Vector3(0., 0., -game.main_character.std_acc_step)) return elif not any([pressed[K_w], pressed[K_s], pressed[K_a], pressed[K_d]]): game.main_character.behave_acceleration.length = 0 return
def vector3_from_orientation(orientation, length): """ Calculates velocity from a character's orientation in a 2D(1/2) space """ return Vector3(length * cos(orientation), 0., length * sin(orientation))