def __init__(self, data_bus): self.endpoint = Endpoint(data_bus) self.phs = PhysicsSystem() self.em = EntityManager(self.phs) self.resources = ResourceStorage() self.scheduler = Scheduler() self.effects = EffectFactory() self.em.load_statics()
def test_add_remove_entity(): game.entity_manager = EntityManager() test_entity = ExampleEntity() assert len(game.entity_manager.entities) == 0 game.entity_manager.add_entity(test_entity) assert len(game.entity_manager.entities) == 1
def shooting(self, time_delta): if self.gun_state.is_shooting: if self.gun_state.shot_counter <= 0: self.gun_state.shot_counter = 1 / self.gun_state.shot_speed EntityManager().create_bullet(self.x, self.y, self.r, self) else: self.gun_state.shot_counter -= time_delta
def __init__(self, working_dir, test_only=False): self.debug = False self.test_only = test_only self.mouse_pos = (0, 0) self.tick = 0 self.timer = -1 self.running = True self._shutdown = False self.show_fps = False self.draw_bounding_boxes = False # Texture loading glEnable(GL_TEXTURE_2D) # just in case TextureManager.init(working_dir + "/res") TextureManager.load() # Game systems init self.batch = pyg.graphics.Batch() self.background_batch = pyg.graphics.Batch() self.foreground_batch = pyg.graphics.Batch() self.entity_manager = EntityManager() self.game_director = GameDirector(self) self.game_map = Map() self.ui = UI() self.game_state = GameState.MAIN_MENU if self.test_only: return # Game director init self.game_director.init(working_dir) # Game window self.window = GameWindow( self, self.game_director.stage.map_data["pixel_width"] + constants.UI_SIDE_PANEL_SIZE, self.game_director.stage.map_data["pixel_height"]) self.fps_display = pyg.window.FPSDisplay(self.window) # Map load # self.game_director.load_stage(1, self) # Game start pyg.clock.schedule_interval( self.update, 1 / constants.FPS) # Was creating inconsistent fps
def test_remove_entity(): game.entity_manager = EntityManager() test_entity = ExampleEntity() game.entity_manager.add_entity(test_entity) game.entity_manager.update_entities(game, 0) game.entity_manager.remove_entity(test_entity) assert len(game.entity_manager.entities) == 1 game.entity_manager.update_entities(game, 1) assert len(game.entity_manager.entities) == 0
def test_entity_updates(): game.entity_manager = EntityManager() test_entity = ExampleEntity() assert len(game.entity_manager.entities) == 0 game.entity_manager.add_entity(test_entity) assert len(game.entity_manager.entities) == 1 assert test_entity.update_count == 0 game.entity_manager.update_entities(game, 0) assert test_entity.update_count == 1
def cumulative_test(): game.entity_manager = EntityManager() test_entity = ExampleEntity() assert len(game.entity_manager.entities) == 0 game.entity_manager.add_entity(test_entity) assert len(game.entity_manager.entities) == 1 assert test_entity.update_count == 0 game.entity_manager.update_entities(game, 0) assert test_entity.update_count == 1 game.entity_manager.update_entities(game, 1) game.entity_manager.update_entities(game, 2) game.entity_manager.update_entities(game, 3) game.entity_manager.update_entities(game, 4) assert test_entity.update_count == 5 game.entity_manager.remove_entity(test_entity) assert len(game.entity_manager.entities) == 1 game.entity_manager.update_entities(game, 5) assert len(game.entity_manager.entities) == 0 and test_entity.update_count == 6
def on_dead(self): EntityManager().create_bonus(self.x, self.y)
class Game: def __init__(self, data_bus): self.endpoint = Endpoint(data_bus) self.phs = PhysicsSystem() self.em = EntityManager(self.phs) self.resources = ResourceStorage() self.scheduler = Scheduler() self.effects = EffectFactory() self.em.load_statics() def exec_step(self, time_delta): self.scheduler.exec_all() self.phs.exec_next(time_delta) self.phs.collision_computer() self.em.remove_all_dead() if self.em.bot_count < config.BOTS_COUNT: self.em.create_ship('bot') def get_state(self): return { 'entities': [pl.get_info() for pl in self.em.all()], 'effects': self.effects.get_effects() } def run(self): last = time.time() curr_step_players = {} while True: curr = time.time() delta = float((curr - last)) last = curr curr_step_players, new_players, expire_players = self.endpoint.scan_players( curr_step_players) for player in new_players: self.em.create_ship('player', player.get('player_id'), player.get('player_name')) for player in expire_players: self.em.remove_ship(player) for pl_id, pl_data in curr_step_players.items(): player_obj: SpaceShip = self.em.players.get(pl_id) if player_obj: pass self.scheduler.add(player_obj, SpaceShip.set_shooting, pl_data.get('shooting')) self.scheduler.add(player_obj, SpaceShip.set_moving, pl_data.get('angle', 0), pl_data.get('direction', 0)) else: self.em.remove_ship(pl_id) self.exec_step(delta) self.endpoint.send_data_to_player(self.get_state()) delay = config.RPS - (time.time() - curr) delay = 0 if delay < 0 else delay yield delay
class Game: """ Main game class. Houses game systems, handles the game window and the game-loop / game-state. """ def __init__(self, working_dir, test_only=False): self.debug = False self.test_only = test_only self.mouse_pos = (0, 0) self.tick = 0 self.timer = -1 self.running = True self._shutdown = False self.show_fps = False self.draw_bounding_boxes = False # Texture loading glEnable(GL_TEXTURE_2D) # just in case TextureManager.init(working_dir + "/res") TextureManager.load() # Game systems init self.batch = pyg.graphics.Batch() self.background_batch = pyg.graphics.Batch() self.foreground_batch = pyg.graphics.Batch() self.entity_manager = EntityManager() self.game_director = GameDirector(self) self.game_map = Map() self.ui = UI() self.game_state = GameState.MAIN_MENU if self.test_only: return # Game director init self.game_director.init(working_dir) # Game window self.window = GameWindow( self, self.game_director.stage.map_data["pixel_width"] + constants.UI_SIDE_PANEL_SIZE, self.game_director.stage.map_data["pixel_height"]) self.fps_display = pyg.window.FPSDisplay(self.window) # Map load # self.game_director.load_stage(1, self) # Game start pyg.clock.schedule_interval( self.update, 1 / constants.FPS) # Was creating inconsistent fps def main_menu(self): self.game_state = GameState.MAIN_MENU self.window.main_menu_label = pyglet.text.Label( 'Press <ENTER> to start', font_name='Monospaced', font_size=24, x=self.window.width // 2, y=self.window.height // 2, anchor_x='center', anchor_y='center') def start_stage(self, index: int): self.game_state = GameState.GAME self.entity_manager.reset() self.game_director.reset() self.game_director.load_stage(index, self) def stage_clear(self, stage: Stage): self.game_state = GameState.STAGE_CLEAR if (stage.index + 1) < len(self.game_director.stages): self.start_stage(stage.index + 1) else: self.game_state = GameState.GAME_FINISHED self.window.game_finished_label = pyglet.text.Label( 'All stages completed! <ENTER>', font_name='Monospaced', font_size=22, x=self.window.width // 2, y=self.window.height // 2, anchor_x='center', anchor_y='center') def game_over(self, stage: Stage): self.game_state = GameState.GAME_OVER if self.timer == -1: self.timer = constants.GAME_OVER_COOLDOWN if self.timer <= 0: self.timer = -1 self.main_menu() else: self.timer -= 1 def update(self, dt: float) -> None: """ Game loop update method. """ if self.game_state in [GameState.GAME, GameState.GAME_OVER]: if self.game_state == GameState.GAME: self.entity_manager.update_entities(self, self.tick) self.game_director.update(self) self.ui.update(self) self.tick += 1 def register_player(self, player: Player) -> Player: """ Registers players key handler to receive input. """ if not self.test_only: self.window.push_handlers(player) self.window.push_handlers(player.key_handler) return player def shutdown(self) -> None: """ Stops the game loop and exits the game. """ self._shutdown = True self.running = False self.window.close() @staticmethod def system_millis() -> float: return time.time() * 1000