def __init__(self, screen_size=None, full_screen=True, log_fps=False, fps_limit=60): if not GameManager.__inited: pygame.init() self.running = True if not screen_size: Debug.log_error("Screen size not specified", "GameManager") self.running = False return GameManager.__inited = True flags = pygame.HWSURFACE | pygame.DOUBLEBUF if full_screen: flags = flags | pygame.FULLSCREEN Screen.new_screen(screen_size, flags) self.delta_time = 1 self.scene_manager = Core.scene.SceneManager.SceneManager() self.input_manager = Input() self.pygame_clock = pygame.time.Clock() # type: pygame self.pygame_clock.tick() self.joystick_manager = Joysticks() self.log_fps = log_fps random.seed(time.time()) self.fps_cap = fps_limit
def sprite(self, value): # type: (Sprite) -> None if not isinstance(value, Sprite): Debug.log_error("sprite parameter not Sprite object", self) return self.__sprite = value self.update_sprite()
def start(self): self.transform = self.game_object.get_component( Transform) # type: Transform if not self.transform: Debug.log_error("Transform not found, disabling", self) self.enabled = False self.update_sprite()
def start(self): self.sprite_renderer = self.game_object.get_component( SpriteRenderer) # type: SpriteRenderer if not self.sprite_renderer: Debug.log_error("SpriteRenderer not found, disabling", self) self.enabled = False self.__update_font()
def __update_font(self): if not isinstance(self.__font, str) or not isinstance( self.__font_size, int): return if not os.path.isfile(self.__font): Debug.log_error("Font {} not found".format(self.__font), self) return self.__font_object = pygame.font.Font(self.__font, self.__font_size) self.__update_sprite()
def get_joystick_axis(self, joy_number, axis): if joy_number < 0 or joy_number >= self.joystick_manager.joystick_count: Debug.log_error( "Requested joystick_axis from joystick {}, out of range". format(joy_number), "Input Manager") return 0 if axis not in self.joystick_axis[joy_number].keys(): return 0 return self.joystick_axis[joy_number][axis]
def get_joystick_button(self, joy_number, button): if joy_number < 0 or joy_number >= self.joystick_manager.joystick_count: Debug.log_error( "Requested joystick_axis from joystick {}, out of range". format(joy_number), "Input Manager") return False if button not in self.joystick_dict[joy_number].keys(): return False return self.joystick_dict[joy_number][button]
def scale(self, value): # type: (Vector2D) -> None if not isinstance(value, Vector2D): Debug.log_warning( "Scale is not Vector2D, is of type {}".format( type(value).__name__), self) return self.__scale = value.copy() self.__scale.x = max(self.__scale.x, 0) self.__scale.y = max(self.__scale.y, 0)
def get_joystick_button_up(self, joy_number, button): if joy_number < 0 or joy_number >= self.joystick_manager.joystick_count: Debug.log_error( "Requested joystick_button from joystick {}, out of range". format(joy_number), "Input Manager") return False if button not in self.joystick_dict[joy_number].keys( ) or button not in self.joystick_updated[joy_number]: return False is_button_down = self.joystick_dict[joy_number][button] return not is_button_down
def from_path(path, convert_alpha=True): # type: (str, bool) -> Sprite if not os.path.isfile(path): Debug.log_error("Sprite {} not found".format(path), "Sprite Object") return None if convert_alpha: surface = pygame.image.load(path).convert_alpha() else: surface = pygame.image.load(path).convert() return Sprite(surface=surface)
def on_draw(self, camera): # type: (Camera) -> None if not isinstance(self.__sprite, Sprite): Debug.log_warning("sprite parameter not Sprite object", self) return # self.update_sprite(camera) if self.update_on_draw: self.update_sprite() # Position in the world sprite_world_position = self.sprite_world_position # type: Vector2D # Position of the screen in the world screen_in_world_position = camera.screen_in_world_position # type: Vector2D # Position of the sprite relative to the screen sprite_in_screen_position = sprite_world_position sprite_in_screen_position.relative_to(screen_in_world_position) render_area = [0, 0, self.sprite_size.x, self.sprite_size.y] # We do not render what isn't visible if sprite_in_screen_position.x < 0: render_area[0] = min(-sprite_in_screen_position.x, self.sprite_size.x) sprite_in_screen_position.x += render_area[0] if sprite_in_screen_position.y < 0: render_area[1] = min(-sprite_in_screen_position.y, self.sprite_size.y) sprite_in_screen_position.y += render_area[1] if sprite_in_screen_position.x + ( self.sprite_size.x - render_area[0]) > camera.render_size[0]: render_area[2] = max( camera.render_size[0] - sprite_in_screen_position.x, 0) if sprite_in_screen_position.y + ( self.sprite_size.y - render_area[1]) > camera.render_size[1]: render_area[3] = max( camera.render_size[1] - sprite_in_screen_position.y, 0) sprite_in_screen_position += Vector2D(0, 0, lst=camera.render_pos) if render_area[0] == render_area[2] or render_area[1] == render_area[3]: return Screen.screen().blit(self.__sprite_transformed, sprite_in_screen_position.list(), render_area)
def main_loop(self): while self.running: events = pygame.event.get() self.input_manager.update_events(events) for event in events: if event.type == pygame.QUIT: self.exit() self.delta_time = float(self.pygame_clock.tick_busy_loop(self.fps_cap)) / (10 ** 3) if self.log_fps: Debug.log("FPS: {}".format(1.0/self.delta_time), "GameManager") # Debug.log("Dt: {}".format(self.delta_time), "GameManager") self.scene_manager.main_loop() pygame.display.flip()
def __init__(self, size=None, flags=pygame.HWSURFACE | pygame.SRCALPHA, surface=None): # type: (Vector2D, int, pygame.Surface) -> None if size is None and surface is None: Debug.log_error("No size and no surface", "Sprite Object") return if size is not None: self.sprite_surface = pygame.Surface(size.list(), flags=flags) self.sprite_size = size.copy() else: self.sprite_surface = surface self.sprite_size = Vector2D(surface.get_width(), surface.get_height())
def start(self): # To move the player we need a Transform component # We can get it using the GameObject.get_component method self.transform = self.game_object.get_component(Transform) # The GameObject.get_component method returns None if the component wasn't found, so we can check that if not self.transform: # If the Transform component wasn't found, we'll debug an error and disable this component # The first parameter is the message # The second parameter is the origin, which we'll put self # Note: The origin can be a Component object or a string. If the origin is a Component, # the Debug class will format it so that it contains the game object name and the component # were the log came from Debug.log_error("Transform component not found, disabling", self) # We disable this component so that it isn't run self.enabled = False # We debug that start is complete Debug.log("Start complete", self)
def sprite_world_position(self): world_position = self.transform.world_position # Next comments were used to take into account camera rotation and camera scale # However that meant that we had to update the sprite on every frame what is very resource expensive, # so we decided not to rotate nor scale the camera # world_position *= camera.transform.world_scale # world_position.relative_to(camera.transform.world_position) # world_position.rotate(-camera.transform.world_rotation) # world_position.make_global(camera.transform.world_position) if self.align_x == "center": world_position.x -= self.sprite_size.x / 2 elif self.align_x == "left": world_position.x -= self.sprite_size.x elif self.align_x == "right": world_position.x -= 0 else: Debug.log_warning( "Alignment x is {}, which is not recognised. Using center". format(self.align_x), self) world_position.x -= self.sprite_size.x / 2 if self.align_y == "center": world_position.y -= self.sprite_size.y / 2 elif self.align_y == "top": world_position.y -= self.sprite_size.y elif self.align_y == "bottom": world_position.y -= 0 else: Debug.log_warning( "Alignment y is {}, which is not recognised. Using center". format(self.align_y), self) world_position.y -= self.sprite_size.y / 2 return world_position
def update_events(self, events): self.key_updated = [] self.mouse_updated = [] for joy_number in self.joystick_updated.keys(): self.joystick_updated[joy_number] = [] for event in events: if event.type == pygame.KEYDOWN: key = event.key self.key_down_dict[key] = True self.key_updated.append(key) elif event.type == pygame.KEYUP: key = event.key self.key_down_dict[key] = False self.key_updated.append(key) elif event.type == pygame.MOUSEBUTTONDOWN: button = event.button self.mouse_down_dict[button] = True self.mouse_updated.append(button) elif event.type == pygame.MOUSEBUTTONUP: button = event.button self.mouse_down_dict[button] = False self.mouse_updated.append(button) elif event.type == pygame.JOYBUTTONDOWN: button = event.button joy_id = event.joy if joy_id not in self.joystick_manager.joy_order: Debug.log_warning("Joy {} event, but not in joy_order", "Input Manager") continue joy_number = self.joystick_manager.joy_order.index(joy_id) self.joystick_dict[joy_number][button] = True self.joystick_updated[joy_number].append(button) elif event.type == pygame.JOYBUTTONUP: button = event.button joy_id = event.joy if joy_id not in self.joystick_manager.joy_order: Debug.log_warning("Joy {} event, but not in joy_order", "Input Manager") continue joy_number = self.joystick_manager.joy_order.index(joy_id) self.joystick_dict[joy_number][button] = False self.joystick_updated[joy_number].append(button) elif event.type == pygame.JOYAXISMOTION: joy_id = event.joy axis = event.axis if joy_id not in self.joystick_manager.joy_order: Debug.log_warning("Joy {} event, but not in joy_order", "Input Manager") continue joy_number = self.joystick_manager.joy_order.index(joy_id) if axis not in self.joystick_axis[joy_number].keys(): self.joystick_axis[joy_number][axis] = 0 self.joystick_axis[joy_number][axis] = event.value