def update_fog(room, player_position, player_sight): player_x = player_position[0] player_y = player_position[1] for fog in room.fog_list: if abs(fog.center_x - player_x) <= player_sight * SPRITE_SIZE: if abs(fog.center_y - player_y) <= player_sight * SPRITE_SIZE: if fog.is_wall: room.visible_list.append(fog) room.fog_list.remove(fog) elif arcade.has_line_of_sight(player_position, fog.position, room.wall_list, player_sight * SPRITE_SIZE): room.visible_list.append(fog) room.fog_list.remove(fog) for visible in room.visible_list: if abs(visible.center_x - player_x) > player_sight * SPRITE_SIZE or abs( visible.center_y - player_y) > player_sight * SPRITE_SIZE: room.fog_list.append(visible) room.visible_list.remove(visible) elif not visible.is_wall: if not player_position == visible.position and not arcade.has_line_of_sight( player_position, visible.position, room.wall_list, player_sight * SPRITE_SIZE): room.fog_list.append(visible) room.visible_list.remove(visible)
def update(self, slayer_position, room, sprite): self.x = sprite.center_x self.y = sprite.center_y self.attack_cooldown -= 1 if arcade.has_line_of_sight( slayer_position, (self.x, self.y), room.wall_list) and not arcade.has_line_of_sight( slayer_position, (self.x, self.y), room.wall_list, 50): if slayer_position[0] - (self.x, self.y)[0] <= -6: sprite.change_x = -self.movespeed elif slayer_position[0] - (self.x, self.y)[0] >= 6: sprite.change_x = self.movespeed else: sprite.change_x = 0 if slayer_position[1] - (self.x, self.y)[1] <= -6: sprite.change_y = -self.movespeed elif slayer_position[1] - (self.x, self.y)[1] >= 6: sprite.change_y = self.movespeed else: sprite.change_y = 0 if arcade.has_line_of_sight(slayer_position, (self.x, self.y), room.wall_list, 50): if sprite.change_x != 0: sprite.change_x = 0 if sprite.change_y != 0: sprite.change_y = 0 if self.attack_cooldown <= 0: spawn_x = self.x spawn_y = self.y connect_vector = [ slayer_position[0] - spawn_x, slayer_position[1] - spawn_y ] connect_vector = connect_vector / LA.norm(connect_vector) angle = math.degrees(np.arccos(connect_vector)[0]) if slayer_position[1] < spawn_y: angle = -angle connect_vector = np.multiply(connect_vector, self.attack_range) spawn_vector = np.add(connect_vector, [spawn_x, spawn_y]) room_setup.add_enemy_attack(room, spawn_vector[0], spawn_vector[1], angle, self.damage) if self.type == GOBLIN: self.attack_cooldown = 20 sound_manager.play_sound(sound_manager.GOBLIN_ATTACK, .15) else: self.attack_cooldown = 45 sound_manager.play_sound(sound_manager.HOBGOBLIN_ATTACK, .15)
def on_draw(self): """ Render the screen. """ try: # This command has to happen before we start drawing arcade.start_render() # Draw all the sprites. self.player_list.draw() self.wall_list.draw() self.enemy_list.draw() for enemy in self.enemy_list: if arcade.has_line_of_sight(self.player.position, enemy.position, self.wall_list): color = arcade.color.RED else: color = arcade.color.WHITE arcade.draw_line(self.player.center_x, self.player.center_y, enemy.center_x, enemy.center_y, color, 2) except Exception as e: print(e)
def update(self): dest = None new_follow = None if (self.path_traversal_state == 'ATTACK' and self.window.player.center_x < self.range_x[1] and self.window.player.center_x > self.range_x[0] and self.window.player.center_y < self.range_y[1] and self.window.player.center_y > self.range_y[0] and self.window.player.visibility): dest = self.window.player.position new_follow = 'player' else: dest = (self.init_x, self.init_y) new_follow = 'init' if (self.path_traversal_state == 'ATTACK' and new_follow == self.follow and self.path != None and self.path_idx < len(self.path) and self.frame_counter < 600): if ((self.path != None and len(self.path) > 0) and ((dest == self.window.player.position and len(self.path) < 60) or dest == (self.init_x, self.init_y))): self.traverse_path() if (arcade.has_line_of_sight(self.position, self.window.player.position, self.window.wall_list)): self.range(self.window.player.center_x, self.window.player.center_y, self.window.view_left, self.window.view_bottom) self.path_traversal_state = 'WAIT' self.path_traversal_state_counter = 0 self.change_x = 0 self.change_y = 0 elif self.path_traversal_state_counter == 'WAIT': None else: self.path_idx = 1 self.frame_counter = 0 self.follow = new_follow self.path = arcade.astar_calculate_path( (self.center_x, self.center_y), dest, self.barrier_list, diagonal_movement=True) self.frame_counter += 1 self.path_traversal_state_counter += 1 if (self.path_traversal_state_counter >= 120 and self.path_traversal_state == 'WAIT'): self.path_traversal_state_counter = 0 self.path_traversal_state = 'ATTACK'
def searchPlayer(self): #When the enemy has line of sight with the player with the max distance being 300 pixels, the enemy is not being attacked and the player is not being attacked, they will move towards the player if arcade.has_line_of_sight(self.game.player_sprite.position, self.position, (self.game.invis_barriers), max_distance = 300) and not self.is_attacked and not self.game.player_sprite.is_attacked: if self.game.player_sprite.center_x < self.center_x: self.change_x = -1.75 else: self.change_x = 1.75 elif not self.is_attacked: if len(arcade.check_for_collision_with_list(self, self.game.invis_barriers)) > 0: self.change_x *= -1 elif self.center_x < self.left_boundary: self.change_x = 0.75 elif self.center_x > self.right_boundary: self.change_x = -0.75 #If the enemy is attacking the player the enemy would stop for a moment if self.is_attacking: self.change_x = 0
def on_update(self, delta_time): # рабочий цикл self.train.update() self.people_list.update() for guard in self.guards_list: distant = int( sqrt((guard.center_x - self.player_sprite.center_x)**2 + (guard.center_y - self.player_sprite.center_y)**2)) if (distant > 100 and distant < 150) and arcade.has_line_of_sight( guard.position, self.player_sprite.position, self.blocks, 200): guard.path = arcade.astar_calculate_path( guard.position, self.player_sprite.position, guard.barrier_list, diagonal_movement=False) else: guard.path = [] #self.player_sprite.update_angle(self.mouse_pos) #передаём координаты мыши персу если требуется # выстрел по нажитию левой кнопки мыши if self.mouse_pos['button'] == 1: self.shot(self.player_sprite) self.bullet_list.update() if self.paint_reils_way_flag: self.paint_reils_way(update=True) # собственно коллизии self.collision() # перезарядка self.recharge_move() self.time = time.time() self.mouse_pos['button'] = 0 # смена хитбокса раз в n*0.016 секунд, где n - колличество циклов системы if time.time() - self.time_for_collision >= 0.048: self.index = (self.index + 1) % 2 for thing in self.people_list: change_hit_box(thing, self.index) self.time_for_collision = time.time()
def test_line_of_sight(): player = arcade.Sprite( ":resources:images/animated_characters/female_person/femalePerson_idle.png" ) player.center_x = 0 player.center_y = 350 enemy = arcade.Sprite( ":resources:images/animated_characters/female_person/femalePerson_idle.png" ) enemy.center_x = 250 enemy.center_y = 350 wall_list = arcade.SpriteList(use_spatial_hash=True) result = arcade.has_line_of_sight(player.position, enemy.position, wall_list) assert result result = arcade.has_line_of_sight(player.position, enemy.position, wall_list, 2000) assert result result = arcade.has_line_of_sight(player.position, enemy.position, wall_list, 20) assert not result wall = arcade.Sprite(":resources:images/tiles/grassCenter.png") wall.center_x = 0 wall.center_y = 0 wall_list.append(wall) result = arcade.has_line_of_sight(player.position, enemy.position, wall_list) assert result wall.center_x = 100 wall.center_y = 350 result = arcade.has_line_of_sight(player.position, enemy.position, wall_list) assert not result wall.center_x = 100 wall.center_y = 450 result = arcade.has_line_of_sight(player.position, enemy.position, wall_list) assert result
def has_line_of_sight_with(self, entity: Entity, blocking_sprites: arcade.SpriteList) -> bool: """ Returns True is this class has line of site with another given entity, given a list of obstacles. Parameters ---------- entity: Entity The entity to check if if this entity has line of sight with. blocking_sprites: arcade.SpriteList The sprite list containing sprites which block line of sight. Returns ------- bool True if there is line of sight, False otherwise. """ return arcade.has_line_of_sight(self.position, entity.position, blocking_sprites, 200)
def on_update(self, delta_time: float): if self.music: position = self.music.get_stream_position() if position == 0.0: self.play_song() hang_test = [False, 0] self.frame_count += 1 self.player_sprite.current_background = self.window.background_color for item in self.primitive_list: self.primitive_list.remove(item) if self.w_pressed: jump_test = self.physics_engine.can_jump() if jump_test[0] and not self.jump_needs_reset: self.physics_engine.jump(12 + jump_test[1]) if self.sounds_enabled: arcade.play_sound(self.jump_sound, 0.01) self.jump_needs_reset = True self.hang_timer = 40 if self.space_pressed: hang_test = self.physics_engine.can_hang(self.hang_timer) if hang_test[0]: if self.hang_timer == 40: if self.sounds_enabled: arcade.play_sound(self.hang_sound, 0.05) self.hang_timer = self.physics_engine.hang(hang_test[1], self.hang_timer) if self.a_pressed and not self.d_pressed: if self.player_sprite.change_x > - C.MAX_SPEED: self.player_sprite.change_x -= 0.1 * self.player_sprite.speed_multiplier elif self.d_pressed and not self.a_pressed: if self.player_sprite.change_x < C.MAX_SPEED: self.player_sprite.change_x += 0.1 * self.player_sprite.speed_multiplier else: if self.player_sprite.change_x > C.FRICTION * self.player_sprite.speed_multiplier: self.player_sprite.change_x -= C.FRICTION * self.player_sprite.speed_multiplier elif self.player_sprite.change_x < - C.FRICTION * self.player_sprite.speed_multiplier: self.player_sprite.change_x += C.FRICTION * self.player_sprite.speed_multiplier else: self.player_sprite.change_x = 0 self.physics_engine.update(delta_time) if hang_test[0]: if self.player_sprite.change_x > 0: x_offset = self.player_sprite.change_x else: x_offset = 0 self.primitive_list.append(arcade.create_rectangle_filled( self.player_sprite.center_x + 20 + (self.hang_timer // 2) + x_offset, self.player_sprite.center_y + 30, self.hang_timer, 5, (255 - int(self.hang_timer * (255 / 40)), int(self.hang_timer * (255 / 40)), 0))) self.primitive_list.append(arcade.create_rectangle_outline( self.player_sprite.center_x + 40, self.player_sprite.center_y + 30, 40, 5, arcade.color.WHITE)) for enemy in self.ennemies: if arcade.has_line_of_sight(enemy.position, self.player_sprite.position, self.platforms): start_x = enemy.center_x start_y = enemy.center_y dest_x = self.player_sprite.center_x dest_y = self.player_sprite.center_y x_diff = dest_x - start_x y_diff = dest_y - start_y angle = math.atan2(y_diff, x_diff) enemy.angle = math.degrees(angle) - 90 # if self.frame_count % 30 == 0: # bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", 0.5) # bullet.center_x = start_x # bullet.center_y = start_y # bullet.angle = math.degrees(angle) # bullet.change_x = math.cos(angle) * C.BULLET_SPEED # bullet.change_y = math.sin(angle) * C.BULLET_SPEED # self.bullet_list.append(bullet) for bullet in self.bullet_list: if self.view_left > bullet.center_x or bullet.center_x > C.SCREEN_WIDTH + self.view_left or \ self.view_bottom > bullet.center_y or bullet.center_y > C.SCREEN_HEIGHT + self.view_bottom: bullet.remove_from_sprite_lists() self.bullet_list.update() changed = False # Scroll left left_boundary = self.view_left + C.LEFT_VIEWPORT_MARGIN if self.player_sprite.left < left_boundary: self.view_left -= left_boundary - self.player_sprite.left changed = True # Scroll right right_boundary = self.view_left + C.SCREEN_WIDTH - C.RIGHT_VIEWPORT_MARGIN if self.player_sprite.right > right_boundary: self.view_left += self.player_sprite.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + C.SCREEN_HEIGHT - C.TOP_VIEWPORT_MARGIN if self.player_sprite.top > top_boundary: self.view_bottom += self.player_sprite.top - top_boundary changed = True # Scroll down if changed: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) # Do the scrolling arcade.set_viewport(self.view_left, C.SCREEN_WIDTH + self.view_left, self.view_bottom, C.SCREEN_HEIGHT + self.view_bottom) if self.player_sprite.top >= self.platforms[0].center_y + 125 and self.platforms[ 0].left < self.player_sprite.center_x < self.platforms[0].right: if self.level_manager: if self.seed.lvl_id + 1 < len(self.level_manager.levels): self.seed = self.level_manager.levels[self.seed.lvl_id + 1] if self.seed.lvl_id == self.level_manager.current_level.lvl_id + 1: self.level_manager.current_level = self.level_manager.levels[self.seed.lvl_id] pickle_out = open(resource_path('data/current_level.save'), 'wb') pickle.dump(self.seed.lvl_id, pickle_out) pickle_out.close() self.setup() else: from main import Victory pickle_out = open(resource_path('data/current_level.save'), 'wb') pickle.dump(self.seed.lvl_id + 1, pickle_out) pickle_out.close() self.window.show_view(Victory(self.window)) else: self.seed.seed = None self.setup() if self.player_sprite.top <= self.view_bottom: self.setup() self.fps.tick()
def on_update(self, delta_time): """ Movement and game logic """ # move the player with the physics engine self.physics_engine.update() # Move the player self.player_list.update() # Update walls, used with moving platforms self.moving_plat_vertical_list.update() # Update the players animation self.player_list.update_animation() # for view port changed = False # if the player dies if self.player.hp <= 0: self.setup(self.map_change) arcade.play_sound(self.player_death_sound) # --- Manage Scrolling --- # Track if we need to change the viewport # --- Manage Combat Stuff for item in self.enemies_shoot_list: if (len(item.collides_with_list( self.wall_list)) > 0) or (arcade.get_distance( item.position[0], item.position[1], item.start_pos[0], item.start_pos[1]) > item.min_range): if item.does_stick: item.change_x = 0 item.change_y = 0 if random.randint(0, 100) < 40: self.pickup_list.append(item) self.enemies_shoot_list.remove(item) else: item.kill() else: item.kill() shot_list = arcade.check_for_collision_with_list( item, self.enemies_list) if arcade.check_for_collision(self.player, item): shot_list.append(self.player) for self.person in shot_list: arcade.play_sound(self.damage_taken_player_sound) self.person.hp -= item.damage item.kill() self.enemies_shoot_list.update() self.charge_list.update() if len(self.charge_list) == 0: MOVEMENT_SPEED = 5 else: MOVEMENT_SPEED = 2 for item in self.charge_list: if item.power <= 100: item.power += 3 else: item.power = 100 self.melee_list.update() for item in self.melee_list: item.center_x += self.player.change_x item.center_y += self.player.change_y item.life_span += 1 if item.life_span > item.attack_speed: item.kill() for bad_guy in self.enemies_list: slap_list = arcade.check_for_collision_with_list( bad_guy, self.melee_list) for item in slap_list: arcade.play_sound(self.damage_taken_enemy_sound) bad_guy.hp -= item.damage # bad_guy.kill() item.damage = 0 if bad_guy.hp <= 0: for drop in bad_guy.drop(): self.pickup_list.append(drop) arcade.play_sound(self.angry_peanut_death_sound) bad_guy.kill() # bad_guy.path = arcade.astar_calculate_path(bad_guy.position, self.player.position,self.barrier_list,False) bad_guy.chase(self.player.center_x, self.player.center_y) if arcade.check_for_collision(bad_guy, self.player): self.player.hp -= bad_guy.damage self.player.center_x += (self.player.center_x - bad_guy.center_x) self.player.center_y += (self.player.center_y - bad_guy.center_y) bump_list = arcade.check_for_collision_with_list( bad_guy, self.wall_list) if len(bump_list) > 0: bad_guy.center_x += (bad_guy.center_x - bump_list[0].center_x) bad_guy.center_y += (bad_guy.center_y - bump_list[0].center_y) self.frame_count += 1 if (self.frame_count) % (bad_guy.fire_rate * 60) == 0: if bad_guy.type == 'pion': bad_guy.has_shot = True if (bad_guy.has_shot == True): if arcade.has_line_of_sight(self.player.position, bad_guy.position, self.wall_list, 500): self.enemies_shoot_list.append( bad_guy.shoot(self.player)) bad_guy.has_shot = False elif bad_guy.type == 'boss': self.enemies_list.append(bad_guy.spawn()) pickup_hit_list = arcade.check_for_collision_with_list( self.player, self.pickup_list) for thing in pickup_hit_list: thing.kill() self.player.ammo += 1 # --- Logic for moving platforms --- for sprite in self.moving_plat_vertical_list: if sprite.boundary_top and sprite.top > sprite.boundary_top and sprite.change_y > 0: sprite.change_y *= -1 if sprite.boundary_bottom and sprite.bottom < sprite.boundary_bottom and sprite.change_y < 0: sprite.change_y *= -1 # --- Manage Scrolling --- # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.player.left < left_boundary: self.view_left -= left_boundary - self.player.left changed = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.player.right > right_boundary: self.view_left += self.player.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.player.top > top_boundary: self.view_bottom += self.player.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.player.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player.bottom changed = True if changed: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) # Do the scrolling arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom) for sprite in self.moving_plat_vertical_list: if arcade.check_for_collision(self.player, sprite) and \ self.player.change_y == 0: self.player.center_y = sprite.center_y # see if we touch any doors door_progress_hit_list = arcade.check_for_collision_with_list( self.player, self.doors_progress_list) if len(door_progress_hit_list) > 0: if self.map_change < 5: self.map_change += 1 else: self.map_change = 1 self.setup(self.map_change) self.view_left = 0 self.view_bottom = 0 changed = True # see if we touch any return doors door_return_hit_list = arcade.check_for_collision_with_list( self.player, self.doors_return_list) if len(door_return_hit_list) > 0: self.map_change -= 1 self.setup(self.map_change) self.view_left = 0 self.view_bottom = 0 changed = True # switches switch_hit_list = arcade.check_for_collision_with_list( self.player, self.switches_list) if len(switch_hit_list) > 0: for x in self.switch_blocks_list: x.remove_from_sprite_lists() arcade.play_sound(self.switch_sound) # keys and locked doors key_hit_list = arcade.check_for_collision_with_list( self.player, self.keys_list) if len(key_hit_list) > 0: for x in key_hit_list: x.kill() if len(self.locked_blocks_list) > 0: for y in self.locked_blocks_list: y.kill() for y in self.locked_blocks_list: y.kill() for y in self.locked_blocks_list: y.kill() for y in self.locked_blocks_list: y.kill() # hearts heart_hit_list = arcade.check_for_collision_with_list( self.player, self.hearts_list) if len(heart_hit_list) > 0: for x in heart_hit_list: x.kill() self.player.hp += 10 # breakable blocks # for block in self.breakable_blocks_list: # breakable_blocks_hit_list = arcade.check_for_collision_with_list(block, # self.melee_list) # for broken_block in breakable_blocks_hit_list: # broken_block.kill() for y in self.breakable_blocks_list: breakable_blocks_hit_list = arcade.check_for_collision_with_list( y, self.melee_list) if len(breakable_blocks_hit_list): y.kill() # dont touch dont_touch_hit_list = arcade.check_for_collision_with_list( self.player, self.dont_touch_list) if len(dont_touch_hit_list) > 0 and \ len(arcade.check_for_collision_with_list(self.player, self.moving_plat_vertical_list)) <= 0: self.setup(self.map_change) # Aiming self.player.view_position = [(self.player.center_x - self.view_left), (self.player.center_y - self.view_bottom)]
def on_update(self, delta_time): #move the player with the physics engine self.frame_count += 1 self.player_list.update_animation() self.enemy_list.update() self.physics_engine.update() self.projectile_list.update() self.charge_list.update() if len(self.enemy_list) < ENEMY_COUNT: self.enemy_list.append( enemy.Enemy(random.randint(30, 500), random.randint(30, 500))) for item in self.projectile_list: if (len(item.collides_with_list( self.wall_list)) > 0) or (arcade.get_distance( item.position[0], item.position[1], item.start_pos[0], item.start_pos[1]) > item.min_range): if item.does_stick: item.change_x = 0 item.change_y = 0 if random.randint(0, 100) < 40: self.pickup_list.append(item) self.projectile_list.remove(item) else: item.kill() else: item.kill() shot_list = arcade.check_for_collision_with_list( item, self.enemy_list) if arcade.check_for_collision(self.player_sprite, item): shot_list.append(self.player_sprite) for person in shot_list: person.hp -= item.damage item.kill() if len(self.charge_list) == 0: MOVEMENT_SPEED = 5 else: MOVEMENT_SPEED = 2 for item in self.charge_list: if item.power <= 100: item.power += 3 else: item.power = 100 self.melee_list.update() for item in self.melee_list: item.center_x += self.player_sprite.change_x item.center_y += self.player_sprite.change_y item.life_span += 1 if item.life_span > item.attack_speed: item.kill() if len(arcade.check_for_collision_with_list(item, self.wall_list)) > 0: item.kill() for bad_guy in self.enemy_list: slap_list = arcade.check_for_collision_with_list( bad_guy, self.melee_list) for item in slap_list: bad_guy.hp -= item.damage # bad_guy.kill() item.damage = 0 if bad_guy.hp <= 0: for drop in bad_guy.drop(): self.pickup_list.append(drop) bad_guy.kill() bad_guy.path = arcade.astar_calculate_path( bad_guy.position, self.player_sprite.position, self.barrier_list, False) try: bad_guy.chase(bad_guy.path[0][0], bad_guy.path[0][1]) except IndexError: self.player_sprite.hp -= bad_guy.damage except Exception as e: print(e) if (self.frame_count) % (bad_guy.fire_rate * 60) == 0: bad_guy.has_shot = True if (bad_guy.has_shot == True): if arcade.has_line_of_sight(self.player_sprite.position, bad_guy.position, self.wall_list, 500): self.projectile_list.append( bad_guy.shoot(self.player_sprite)) bad_guy.has_shot = False # print(bad_guy.path,"->", self.player_sprite.position) #calculate speed based on the keys pressed self.player_sprite.change_x = 0 self.player_sprite.change_y = 0 if self.up_pressed and not self.down_pressed: self.player_sprite.change_y = MOVEMENT_SPEED elif self.down_pressed and not self.up_pressed: self.player_sprite.change_y = -MOVEMENT_SPEED elif self.left_pressed and not self.right_pressed: self.player_sprite.change_x = -MOVEMENT_SPEED elif self.right_pressed and not self.left_pressed: self.player_sprite.change_x = MOVEMENT_SPEED #see if we hit any coins pickup_hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.pickup_list) #loop through each coin we hit (if any) and remove it for thing in pickup_hit_list: # #Remove the coin thing.remove_from_sprite_lists() # #play a sound # arcade.play_sound(self.collect_coin_sound) # #Add one to the score self.ammo += 1
def on_update(self, delta_time): """ Movement and game logic """ # move the player with the physics engine self.physics_engine.update() # Move the player self.player_list.update() # Update the players animation self.player_list.update_animation() # for view port changed = False # see if we touch any doors door_hit_list = arcade.check_for_collision_with_list( self.player, self.doors_progress_list) if len(door_hit_list) > 0: self.map_change += 1 self.setup(self.map_change) self.view_left = 0 self.view_bottom = 0 changed = True # --- Manage Scrolling --- # Track if we need to change the viewport # --- Manage Combat Stuff for item in self.enemies_shoot_list: if (len(item.collides_with_list( self.wall_list)) > 0) or (arcade.get_distance( item.position[0], item.position[1], item.start_pos[0], item.start_pos[1]) > item.min_range): if item.does_stick: item.change_x = 0 item.change_y = 0 if random.randint(0, 100) < 40: self.pickup_list.append(item) self.enemies_shoot_list.remove(item) else: item.kill() else: item.kill() shot_list = arcade.check_for_collision_with_list( item, self.enemy_list) if arcade.check_for_collision(self.player, item): shot_list.append(self.player) for person in shot_list: person.hp -= item.damage item.kill() if len(self.charge_list) == 0: MOVEMENT_SPEED = 5 else: MOVEMENT_SPEED = 2 for item in self.charge_list: if item.power <= 100: item.power += 3 else: item.power = 100 self.melee_list.update() for item in self.melee_list: item.center_x += self.player.change_x item.center_y += self.player.change_y item.life_span += 1 if item.life_span > item.attack_speed: item.kill() if len(arcade.check_for_collision_with_list(item, self.wall_list)) > 0: item.kill() for bad_guy in self.enemy_list: slap_list = arcade.check_for_collision_with_list( bad_guy, self.melee_list) for item in slap_list: bad_guy.hp -= item.damage # bad_guy.kill() item.damage = 0 if bad_guy.hp <= 0: for drop in bad_guy.drop(): self.pickup_list.append(drop) bad_guy.kill() # bad_guy.path = arcade.astar_calculate_path(bad_guy.position, self.player.position,self.barrier_list,False) bad_guy.chase(self.player.center_x, self.player.center_y) if arcade.check_for_collision(bad_guy, self.player): self.player.hp -= bad_guy.damage self.player.center_x += (self.player.center_x - bad_guy.center_x) self.player.center_y += (self.player.center_y - bad_guy.center_y) bump_list = arcade.check_for_collision_with_list( bad_guy, self.wall_list) if len(bump_list) > 0: bad_guy.center_x += (bad_guy.center_x - bump_list[0].center_x) bad_guy.center_y += (bad_guy.center_y - bump_list[0].center_y) if (self.frame_count) % (bad_guy.fire_rate * 60) == 0: bad_guy.has_shot = True if (bad_guy.has_shot == True): if arcade.has_line_of_sight(self.player.position, bad_guy.position, self.wall_list, 500): self.enemies_shoot_list.append( bad_guy.shoot(self.player)) bad_guy.has_shot = False # print(bad_guy.path,"->", self.player.position) #calculate speed based on the keys pressed self.player.change_x = 0 self.player.change_y = 0 if self.up_pressed and not self.down_pressed: self.player.change_y = MOVEMENT_SPEED elif self.down_pressed and not self.up_pressed: self.player.change_y = -MOVEMENT_SPEED elif self.left_pressed and not self.right_pressed: self.player.change_x = -MOVEMENT_SPEED elif self.right_pressed and not self.left_pressed: self.player.change_x = MOVEMENT_SPEED pickup_hit_list = arcade.check_for_collision_with_list( self.player, self.pickup_list) for thing in pickup_hit_list: thing.kill() self.player.ammo += 1 # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.player.left < left_boundary: self.view_left -= left_boundary - self.player.left changed = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.player.right > right_boundary: self.view_left += self.player.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.player.top > top_boundary: self.view_bottom += self.player.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.player.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player.bottom changed = True if changed: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) # Do the scrolling arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def on_update(self, delta_time: float): for engine in self.enemy_engines: engine.update() self.player_sprite.change_x = 0 self.player_sprite.change_y = 0 if self.up_pressed and not self.down_pressed: self.player_sprite.change_y = MOVEMENT_SPEED elif self.down_pressed and not self.up_pressed: self.player_sprite.change_y = -MOVEMENT_SPEED if self.left_pressed and not self.right_pressed: self.player_sprite.change_x = -MOVEMENT_SPEED elif self.right_pressed and not self.left_pressed: self.player_sprite.change_x = MOVEMENT_SPEED # self.player_list.update() if self.player_sprite.last_mouse_x and self.player_sprite.last_mouse_y: start_x = self.player_sprite.center_x start_y = self.player_sprite.center_y x_diff = self.player_sprite.last_mouse_x - start_x y_diff = self.player_sprite.last_mouse_y - start_y angle = math.atan2(y_diff, x_diff) self.player_sprite.angle = math.degrees(angle) donut_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.donut_list) if len(donut_hit_list) > 0: for donut in donut_hit_list: donut.remove_from_sprite_lists() self.score += 1 for bullet in self.bullet_list: wall_hit_list = arcade.check_for_collision_with_list(bullet, self.wall_list) enemy_hit_list = arcade.check_for_collision_with_list( bullet, self.enemy_list ) if len(wall_hit_list) > 0: bullet.remove_from_sprite_lists() if len(enemy_hit_list) > 0: bullet.remove_from_sprite_lists() for enemy in enemy_hit_list: enemy.remove_from_sprite_lists() # If bullet flies offscreen, remove it if ( bullet.bottom > SCREEN_WIDTH or bullet.top < 0 or bullet.right < 0 or bullet.left > SCREEN_WIDTH ): bullet.remove_from_sprite_lists() for bullet in self.enemy_bullet_list: wall_hit_list = arcade.check_for_collision_with_list(bullet, self.wall_list) donut_hit_list = arcade.check_for_collision_with_list(bullet, self.donut_list) player_hit = arcade.check_for_collision(bullet, self.player_sprite) if player_hit: self.player_sprite.remove_from_sprite_lists() view = GameOverView(self.window, Game, self.level + 1, self.score) self.window.show_view(view) if len(wall_hit_list) > 0: bullet.remove_from_sprite_lists() if len(donut_hit_list) > 0: bullet.remove_from_sprite_lists() for donut in donut_hit_list: donut.remove_from_sprite_lists() if ( bullet.bottom > SCREEN_WIDTH or bullet.top < 0 or bullet.right < 0 or bullet.left > SCREEN_WIDTH ): bullet.remove_from_sprite_lists() self.bullet_list.update() # self.wall_list.update() for enemy in self.enemy_list: if not arcade.has_line_of_sight( self.player_sprite.position, enemy.position, self.wall_list ): enemy.follow_sprite(self.player_sprite) else: enemy.stop() x_diff = self.player_sprite.center_x - enemy.center_x y_diff = self.player_sprite.center_y - enemy.center_y angle = math.atan2(y_diff, x_diff) enemy.angle = math.degrees(angle) odds = 200 adj_odds = int(odds * (1 / 60) / delta_time) if random.randrange(adj_odds) == 0: bullet = arcade.Sprite("assets/bullet.png", 0.08) bullet.center_x = enemy.center_x bullet.center_y = enemy.center_y bullet.angle = math.degrees(angle) bullet.change_x = math.cos(angle) * MOVEMENT_SPEED bullet.change_y = math.sin(angle) * MOVEMENT_SPEED self.enemy_bullet_list.append(bullet) self.enemy_list.update() self.physics_engine.update() self.enemy_bullet_list.update() if len(self.enemy_list) == 0: self.level += 1 self.setup(self.level, self.score) return super().on_update(delta_time)
def update(self): states = ['MELEE', 'WAIT_FOR_SHOOT', 'SHOOT', "WAIT_FOR_MELEE"] if (self.path_traversal_state == 'MELEE' and self.window.player.visibility): if (self.path != None and len(self.path) < 60): if (self.path_idx < len(self.path) and len(self.path) > 1): self.traverse_path() elif arcade.check_for_collision(self, self.window.player): # self.window.player.getDamaged(self.window.player.center_x,self.window.player.center_y) self.path_traversal_state = 'WAIT_FOR_SHOOT' self.path_traversal_state_counter = 0 self.change_x = 0 self.change_y = 0 else: self.path_traversal_state = 'WAIT_FOR_SHOOT' self.path_traversal_state_counter = 0 self.change_x = 0 self.change_y = 0 elif self.path_traversal_state == 'WAIT_FOR_SHOOT': None elif self.path_traversal_state == 'SHOOT' and self.window.player.visibility: if (self.path != None and len(self.path) < 60): if (self.path_idx < len(self.path) and len(self.path) > 1 and self.window.state > CUTSCENE_4): self.traverse_path() if (arcade.has_line_of_sight( (self.center_x, self.center_y), (self.window.player.center_x, self.window.player.center_y), self.window.wall_list)): self.range(self.window.player.center_x, self.window.player.center_y, self.window.view_left, self.window.view_bottom) self.path_traversal_state = 'WAIT_FOR_MELEE' self.path_traversal_state_counter = 0 self.change_x = 0 self.change_y = 0 elif arcade.check_for_collision(self, self.window.player): None self.path_traversal_state = 'WAIT_FOR_MELEE' self.path_traversal_state_counter = 0 self.change_x = 0 self.change_y = 0 else: self.path_traversal_state = 'WAIT_FOR_MELEE' self.path_traversal_state_counter = 0 self.change_x = 0 self.change_y = 0 elif (self.path_traversal_state == 'WAIT_FOR_MELEE'): None self.frame_counter += 1 self.path_traversal_state_counter += 1 if (self.path_traversal_state_counter >= 120): self.path_traversal_state_counter = 0 for i in range(4): if (self.path_traversal_state == states[i]): self.path_traversal_state = states[(i + 1) % 4] break if (self.frame_counter > 40 and self.path_traversal_state != states[1] or self.path_traversal_state != states[3]): self.frame_counter = 0 self.path = arcade.astar_calculate_path( self.position, (self.window.player.center_x, self.window.player.center_y), self.barrier_list, True) self.path_idx = 1
def can_see(self, object_postition, object_visibility, wall_list): has_sight = arcade.has_line_of_sight(self.position, object_postition, wall_list,self.skills['Perception'],5) if self.skills['Perception'] < object_visibility: has_sight = False return has_sight