def _on_level_cleared_timer_timeout(self): for ghost_entity_id in ["blinky", "pinky", "inky", "clyde"]: game.set_entity_visibility(entity_id=ghost_entity_id, visible=False) game.play_animation(entity_id="level", animation_name="level_completed") game.start_timer(timer_id=self.level_cleared_transtion_timer_id)
def _on_pacman_power_pellete_eaten(self): if self.state == State.FRIGHTENED: self.frightened_flash = False game.stop_timer(timer_id=self.frightened_timer_id) game.stop_timer(timer_id=self.frightened_flash_timer_id) game.start_timer(timer_id=self.frightened_timer_id) elif self.state != State.DEFEATED_TRAVELING_TO_HOUSE: self.state = State.FRIGHTENED
def _on_level_reset_timer_timeout(self): if global_obj.player_stats.lives > 0: self.reset_entity_positions() game.start_timer(timer_id=self.show_entities_timer_id, time=0.1) self.level_reset() game.start_timer(timer_id=self.start_timer_id, time=0.5) else: global_obj.game_state = GameState.GAME_OVER game.change_to_scene(file_path="./scenes/transition.json")
def __create__(self): self.life_timer_id = f"{self.entity_id}_life_timer" self.collected = False game.create_timer(timer_id=self.life_timer_id, time=10.0) game.subscribe_to_signal( source_id=self.life_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_fruit_timer_timeout", ) game.start_timer(self.life_timer_id, time=10.0)
def _on_pacman_lose_life_start_timer_timeout(self): for ghost_entity_id in ["blinky", "pinky", "inky", "clyde"]: game.set_entity_visibility(entity_id=ghost_entity_id, visible=False) pacman_entity = scene_tree.get_entity("pacman") pacman_entity.lose_life() game.start_timer(timer_id=self.level_reset_timer_id) game.play_sound(sound_id="pacman-death") if global_obj.player_stats.lives <= 0: game.create_label_entity(entity_id="game_over_label", text="GAME OVER", position=(147, 318), font_id="pacman-pixel", layer=5, fixed=False, wrap_length=600, color=(255, 0, 0))
def state(self, value): self.previous_state = self._state self._state = value if self._state == State.CHASE: if self.previous_state == State.FRIGHTENED: game.resume_timer(timer_id=self.chase_timer_id) else: self.chase_state_counter += 1 if self.chase_state_counter < 4: game.start_timer(timer_id=self.chase_timer_id) else: game.start_timer(timer_id=self.chase_timer_id, time=1000) if self.previous_state != State.LEAVE_HOUSE and self.previous_state != State.IN_HOUSE and not global_obj.level_grid.has_turn_space(self.position.x, self.position.y): self.flip_direction() elif self._state == State.SCATTER: if self.previous_state == State.FRIGHTENED: game.resume_timer(timer_id=self.scatter_timer_id) else: self.scatter_state_counter += 1 scatter_start_time = self.scatter_timer_wait_time if self.scatter_state_counter >= 3: scatter_start_time -= 2.0 game.start_timer( timer_id=self.scatter_timer_id, time=scatter_start_time ) if self.previous_state != State.LEAVE_HOUSE and self.previous_state != State.IN_HOUSE and not global_obj.level_grid.has_turn_space(self.position.x, self.position.y): self.flip_direction() elif self._state == State.FRIGHTENED: if self.previous_state == State.CHASE: game.pause_timer(self.chase_timer_id) elif self.previous_state == State.SCATTER: game.pause_timer(self.scatter_timer_id) self.frightened_flash = False game.start_timer(timer_id=self.frightened_timer_id) if not global_obj.level_grid.has_turn_space(self.position.x, self.position.y): self.flip_direction() self.target_tile = self.determine_target_tile()
def _on_frightened_timer_timeout(self): self.frightened_flash = True game.start_timer(timer_id=self.frightened_flash_timer_id)
def _on_pacman_level_cleared(self): global_obj.game_paused = True global_obj.game_state = GameState.LEVEL_COMPLETE game.start_timer(timer_id=self.level_cleared_timer_id) game.stop_music()
def _on_pacman_lose_life(self): global_obj.game_paused = True global_obj.game_state = GameState.PACMAN_LOSE_LIFE game.start_timer(timer_id=self.pacman_lose_life_start_timer_id) game.stop_music()
def _on_pacman_power_pellete_eaten(self): game.stop_music() game.play_music(music_id="power-pellete") game.start_timer(timer_id=self.ghost_frightened_timer_id)
def __create__(self): self.power_pellete_ids = [] self.power_pelletes_visible = True self.one_up_visible = True self.start_timer_id = "start_timer" self.level_reset_timer_id = "level_reset_timer" self.level_cleared_timer_id = "level_cleared_timer" self.level_cleared_transtion_timer_id = "level_cleared_transtion_timer" power_pellete_flash_timer_id = "power_pellete_timer" self.pacman_lose_life_start_timer_id = "pacman_lose_life_start_timer" self.ghost_frightened_timer_id = "ghost_frightened_timer" self.show_entities_timer_id = "show_entities_timer" # Start Timer game.create_timer(timer_id=self.start_timer_id, time=4.2, loops=False, start_on_creation=False) game.subscribe_to_signal( source_id=self.start_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_start_timer_timeout", ) # Level Reset Timer game.create_timer( timer_id=self.level_reset_timer_id, time=2.0, loops=False, start_on_creation=False, ) game.subscribe_to_signal( source_id=self.level_reset_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_level_reset_timer_timeout", ) # Level Cleared Timer game.create_timer( timer_id=self.level_cleared_timer_id, time=1.5, loops=False, start_on_creation=False, ) game.subscribe_to_signal( source_id=self.level_cleared_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_level_cleared_timer_timeout", ) # Level Cleared Transtion Timer game.create_timer( timer_id=self.level_cleared_transtion_timer_id, time=2.0, loops=False, start_on_creation=False, ) game.subscribe_to_signal( source_id=self.level_cleared_transtion_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_level_cleared_transition_timer_timeout", ) # Pellete Flash Timer game.create_timer( timer_id=power_pellete_flash_timer_id, time=0.2, loops=True, start_on_creation=True, ) game.subscribe_to_signal( source_id=power_pellete_flash_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_power_pellete_flash_timer_timeout", ) # Pacman Lose Life Start Timer game.create_timer( timer_id=self.pacman_lose_life_start_timer_id, time=1.0, loops=False, start_on_creation=False, ) game.subscribe_to_signal( source_id=self.pacman_lose_life_start_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_pacman_lose_life_start_timer_timeout", ) # 1UP Flash Timer one_up_flash_timer_id = "one_up_flash_timer" game.create_timer(timer_id=one_up_flash_timer_id, time=0.4, loops=True, start_on_creation=True) game.subscribe_to_signal( source_id=one_up_flash_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_one_up_flash_timer_timeout", ) # Ghost Frightened Timer game.create_timer( timer_id=self.ghost_frightened_timer_id, time=7.0, loops=False, start_on_creation=False, ) game.subscribe_to_signal( source_id=self.ghost_frightened_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_ghost_frightened_timer_timeout", ) # Show Entities Timer game.create_timer( timer_id=self.show_entities_timer_id, time=2.0, loops=False, start_on_creation=False, ) game.subscribe_to_signal( source_id=self.show_entities_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_show_entities_timer_timeout", ) # Lose Life Signal pacman_entity = scene_tree.get_entity("pacman") game.subscribe_to_signal( source_id=pacman_entity.entity_id, signal_id="lose_life", subscriber_entity_id=self.entity_id, function_name="_on_pacman_lose_life", ) # Power Pellet game.subscribe_to_signal( source_id=pacman_entity.entity_id, signal_id="power_pellete_eaten", subscriber_entity_id=self.entity_id, function_name="_on_pacman_power_pellete_eaten", ) # Level Cleared Signal game.subscribe_to_signal( source_id=pacman_entity.entity_id, signal_id="level_cleared", subscriber_entity_id=self.entity_id, function_name="_on_pacman_level_cleared", ) self.create_level_pellets() self.set_all_entities_visibility(False) self.reset_entity_positions() global_obj.player_stats.force_update_ui() if global_obj.game_state == GameState.BEGIN_GAME: global_obj.player_stats.force_update_ui() global_obj.game_state = GameState.PLAYING game.start_timer(timer_id=self.start_timer_id, time=4.2) game.start_timer(timer_id=self.show_entities_timer_id, time=2.0) game.play_sound(sound_id="pacman-begin") global_obj.player_stats.pelletes = 0 elif global_obj.game_state == GameState.LEVEL_COMPLETE: global_obj.player_stats.level += 1 global_obj.player_stats.force_update_ui() global_obj.game_state = GameState.PLAYING game.start_timer(timer_id=self.start_timer_id, time=1.0) self._on_show_entities_timer_timeout() global_obj.player_stats.pelletes = 0
def __process__(self, delta_time): if global_obj.game_started and not global_obj.game_paused: collided_entity_id, collider_tag = game.check_entity_collision( entity_id=self.entity_id, offset_position=(0, 0)) if collided_entity_id: if collider_tag == "pellete": game.delete_entity(entity_id=collided_entity_id) global_obj.player_stats.score += 10 global_obj.player_stats.pelletes += 1 self.pellete_counter -= 1 game.play_sound(sound_id="pacman-chomp") elif collider_tag == "power_pellete": game.delete_entity(entity_id=collided_entity_id) global_obj.player_stats.score += 50 global_obj.player_stats.pelletes += 1 self.pellete_counter -= 1 self.ghost_eaten_score = 0 game.emit_signal( entity_id=self.entity_id, signal_id=self.power_pellete_eaten_signal_id, ) elif collider_tag == "enemy": collided_ghost = scene_tree.get_entity(collided_entity_id) if collided_ghost.state == State.FRIGHTENED: if self.ghost_eaten_score == 0: self.ghost_eaten_score = 200 else: self.ghost_eaten_score *= 2 global_obj.player_stats.score += self.ghost_eaten_score collided_ghost.eaten(self.ghost_eaten_score) game.start_timer(timer_id=self.eaten_freeze_timer_id) global_obj.game_paused = True game.play_sound(sound_id="pacman-eat-ghost") elif (collided_ghost.state == State.CHASE or collided_ghost.state == State.SCATTER): game.stop_animation(entity_id=self.entity_id) game.emit_signal(entity_id=self.entity_id, signal_id=self.lose_life_signal_id) elif collider_tag == "fruit": fruit_entity = scene_tree.get_entity(collided_entity_id) if not fruit_entity.collected: global_obj.player_stats.score += global_obj.player_stats.get_fruit_score( collided_entity_id) fruit_entity.eaten() # Input - determines direction pacman wants to turn if game.is_action_pressed(action="left"): self.direction_to_turn = Direction.left elif game.is_action_pressed(action="right"): self.direction_to_turn = Direction.right elif game.is_action_pressed(action="up"): self.direction_to_turn = Direction.up elif game.is_action_pressed(action="down"): self.direction_to_turn = Direction.down # Determine speed if game.has_timer_stopped(timer_id=self.turn_speed_up_timer_id): self.speed = 100 else: self.speed = 150 self.accumulated_delta += delta_time * self.speed while self.accumulated_delta >= 1.0: self.accumulated_delta -= 1.0 self.move(self.velocity, 1.0) self.update_animations() # Play movement sound if abs(self.velocity.x) > 0 or abs(self.velocity.y) > 0: if game.has_timer_stopped(timer_id=self.chomp_timer_id): pass # game.start_timer(timer_id=self.chomp_timer_id) else: self.accumulated_delta = 0.0 elif (global_obj.game_paused and global_obj.game_state == GameState.PACMAN_LOSE_LIFE): pass else: game.stop_animation(entity_id=self.entity_id) if game.is_action_just_pressed(action="exit_game"): game.quit()
def move(self, velocity, speed): # Determine if direction should change if self.direction_to_turn != self.direction: can_turn = ((self.direction_to_turn == Direction.left and self.direction == Direction.right) or (self.direction_to_turn == Direction.right and self.direction == Direction.left) or (self.direction_to_turn == Direction.up and self.direction == Direction.down) or (self.direction_to_turn == Direction.down and self.direction == Direction.up)) if can_turn or global_obj.level_grid.has_turn_space( int(self.position.x), int(self.position.y)): grid_space_x = int( (self.position.x + self.offset.x - global_obj.level_grid.board_position.x) / 16) grid_space_y = int( (self.position.y + self.offset.y - global_obj.level_grid.board_position.y) / 16) grid_space = global_obj.level_grid.get(grid_space_x, grid_space_y) turn_space_offset_x = 0 turn_space_offset_y = 0 if self.direction_to_turn == Direction.left: turn_space_offset_x = -1 elif self.direction_to_turn == Direction.right: turn_space_offset_x = 1 elif self.direction_to_turn == Direction.up: turn_space_offset_y = -1 elif self.direction_to_turn == Direction.down: turn_space_offset_y = 1 turn_space = global_obj.level_grid.get( grid_space_x + turn_space_offset_x, grid_space_y + turn_space_offset_y, ) if (grid_space == LevelGrid.VALID_SPACE and turn_space == LevelGrid.VALID_SPACE): self.direction = self.direction_to_turn game.start_timer(timer_id=self.turn_speed_up_timer_id) # Other Movement # Determine velocity from direction and speed velocity.x = 0 velocity.y = 0 direction_offset_x = 0 direction_offset_y = 0 if self.direction == Direction.left: velocity.x -= speed direction_offset_x = -1 elif self.direction == Direction.right: velocity.x += speed direction_offset_x = 1 elif self.direction == Direction.up: velocity.y -= speed direction_offset_y = -1 elif self.direction == Direction.down: velocity.y += speed direction_offset_y = 1 # Movement grid_space_x = int((self.position.x + velocity.x + self.offset.x - global_obj.level_grid.board_position.x) / 16) grid_space_y = int((self.position.y + velocity.y + self.offset.y - global_obj.level_grid.board_position.y) / 16) grid_space = global_obj.level_grid.get(grid_space_x, grid_space_y) at_turn_space_wall = (global_obj.level_grid.has_turn_space( int(self.position.x), int(self.position.y)) and global_obj.level_grid.get( grid_space_x + direction_offset_x, grid_space_y + direction_offset_y) == LevelGrid.INVALID_SPACE) if grid_space == LevelGrid.VALID_SPACE and not at_turn_space_wall: if not global_obj.level_grid.has_loop_around_space( grid_space_x, grid_space_y): # Update position self.position.x += velocity.x self.position.y += velocity.y self.on_stop_animation_frame = False else: # Left if grid_space_x < 0: self.position.x = (global_obj.level_grid.board_position.x + (LevelGrid.RIGHT_LOOP_SPACE[0] * 16) - 16) # Right else: self.position.x = (global_obj.level_grid.board_position.x + (LevelGrid.LEFT_LOOP_SPACE[0] * 16) + 16) else: velocity.x = 0 velocity.y = 0
def eaten(self): game.play_animation(entity_id=self.entity_id, animation_name="score") game.stop_timer(timer_id=self.life_timer_id) game.start_timer(timer_id=self.life_timer_id, time=1.0) self.collected = True game.play_sound(sound_id="pacman-eat-fruit")
def __create__(self): # Vars self.speed = 100 # Play Music game.stop_music() game.pause_music() game.resume_music() game.play_music(music_id="dd-music") # Create Text Label Entity text_label_entity_id = "test_text" game.create_label_entity( entity_id=text_label_entity_id, text="Test", position=(100, 100), font_id="charriot", layer=2, fixed=False, wrap_length=800, color=(100, 100, 100), ) # Update Label Text game.update_label_entity_text(entity_id=text_label_entity_id, text="New Text!") # Create sprite entity helicopter_entity_id = "helicopter" game.create_sprite_entity( entity_id=helicopter_entity_id, texture_id="chopper", position=(400, 400), layer=3, width=32, height=32, clickable=False, fixed=False, collider_tag="", # Emtpy as default set to add collider ) # Setup signal for animation finished game.subscribe_to_signal( source_id=f"{helicopter_entity_id}_animation", signal_id="animation_finished", subscriber_entity_id=self.entity_id, function_name="_on_helicopter_animation_finished", ) # Setup signal for animation frame changed game.subscribe_to_signal( source_id=f"{helicopter_entity_id}_animation", signal_id="frame_changed", subscriber_entity_id=self.entity_id, function_name="_on_helicopter_frame_changed", ) # Delete sprite entity (after creation) delete_entity_id = "delete_entity" game.create_sprite_entity( entity_id=delete_entity_id, texture_id="chopper", position=(100, 100), layer=3, width=32, height=32, clickable=False, fixed=False, ) game.delete_entity(entity_id=delete_entity_id) # Create timer self.counted_seconds = 0 self.count_timer_id = "count_timer" game.create_timer( timer_id=self.count_timer_id, time=1.0, loops=True, start_on_creation=True ) # Stop Timer game.stop_timer(self.count_timer_id) # Start Timer game.start_timer(self.count_timer_id) # Connect signal game.subscribe_to_signal( source_id=self.count_timer_id, signal_id="timeout", subscriber_entity_id=self.entity_id, function_name="_on_test_timer_timeout", )