class MenuPrincipal(object): def __init__(self, _gestor_estados): self.fondo = cargar_img("fondos/intro") self.temporizador_parpadeo = Timer(250) self.parpadear = True self.gestor_estados = _gestor_estados def dibujar(self, _pantalla): _pantalla.blit(self.fondo, (0, 0)) if self.parpadear: py.draw.rect(_pantalla, (6, 6, 6), (122, 164, 253, 24)) def actualizar(self, _dt): self.temporizador_parpadeo.update(_dt) if self.temporizador_parpadeo.tick: self.parpadear = not self.parpadear self.iniciar_juego() def iniciar_juego(self): key = py.key.get_pressed() if key[py.K_RETURN]: self.gestor_estados.estado_actual = 1
def __init__(self, _x, _y, _nombre): self.bounds = Rect(_x, _y, 0, 0) self.animaciones = GestorAnimaciones() self.bounds.width, self.bounds.height = self.animaciones.añadir_animacion( _nombre, self.animaciones.cargar_frames("powerups/" + _nombre, 6), 200, 0) self.temporizador_movimiento = Timer(10) self.tipo = _nombre
class Animation(object): """It represents an animation on game. Parameters: _name -- Animation name _frames -- Animation frames _speed -- Speed (MS) which last each animation frame""" def __init__(self, _name, _frames, _speed): #Received by parameter self.name = _name self.frames = _frames self._speed = _speed self.timer = Timer(_speed) #Inner data self.current_frame = 0 self.width = self.frames[0].get_width() self.height = self.frames[0].get_height() self.ended = False def render(self, _screen, _bounds, _facing_left = False): """Draw the animation on the screen, on the position passed by parameter. Parameter: _screen -- Screen on which the frames will be drawn. _bounds -- Position where the image will be drawn. _facing_left -- Bool to indicate if the image will be rotated to the left. Default value = False""" _screen.blit(py.transform.flip(self.frames[self.current_frame], _facing_left, False), _bounds) def update(self, _dt): """Update the animation. Parameters: _dt -- Time in milliseconds, since this method was executed for the last time.""" self.timer.update(_dt) # Each time the animation's internal timer emits a pulse # you advance the list of frames. If there are no more frames within # the list, it goes back to the beginning. if self.timer.tick: self.current_frame += 1 if self.current_frame > len(self.frames) - 1: self.current_frame = len(self.frames) - 1 self.ended = True def stop(self): """Stops the internal timer of the animation and resets the counter of frames in the animation to start again from frame zero.""" self.timer.stop() self.current_frame = 0 def reset_animation(self): self.current_frame = 0 self.ended = False
def __init__(self, _states_manager): self.start_game = Timer(1000) self.logo = cargar_img("menu/logo_intro") self.levels = cargar_img("menu/levels_tag") self.selector = cargar_img("menu/selector") self.confirm_start_game = False self.blink_selection = Timer(200) self.blink_enabled = False self.states_manager = _states_manager
def __init__(self): random.seed() self.enemies = Array() self.gfx = [ cargar_img("enemies/enemy_1", 2), cargar_img("enemies/enemy_3", 2), cargar_img("enemies/enemy_2", 2), cargar_img("enemies/bonus", 2) ] self.timer = Timer(2000)
def __init__(self, _name, _frames, _speed): #Received by parameter self.name = _name self.frames = _frames self._speed = _speed self.timer = Timer(_speed) #Inner data self.current_frame = 0 self.width = self.frames[0].get_width() self.height = self.frames[0].get_height() self.ended = False
def __init__(self): """Constructor""" self.__inimator = Animator() self.__bounds = Rect(250, 0, 0, 0) self.__bounds.size = self.__inimator.add_animation( "moving", 500, "peach/peach", 2, 2) self.__bounds.bottom = 79 # Elementos necesarios para mostrar el mensaje de HELP cada cierto tiempo self.__timer = Timer(550) self.__help = load_img("misc/help", 2) self.__showHelp = True
class MainMenu(object): def __init__(self, _states_manager): self.start_game = Timer(1000) self.logo = cargar_img("menu/logo_intro") self.levels = cargar_img("menu/levels_tag") self.selector = cargar_img("menu/selector") self.confirm_start_game = False self.blink_selection = Timer(200) self.blink_enabled = False self.states_manager = _states_manager def update(self, _dt): key = py.key.get_pressed() if key[py.K_RETURN]: self.confirm_start_game = True if self.confirm_start_game: self.start_game.update(_dt) self.blink_selection.update(_dt) if self.start_game.tick: self.states_manager.change_state(1) if self.blink_selection.tick: self.blink_enabled = not self.blink_enabled def render(self, _screen): _screen.blit(self.logo, (WIDTH // 2 - self.logo.get_width() // 2, 50)) _screen.blit(self.levels, (WIDTH // 2 - self.levels.get_width() // 2, 200)) _screen.blit(self.selector, (WIDTH // 2 - self.selector.get_width() * 3, 200)) if self.blink_enabled: py.draw.rect( _screen, (0, 0, 0), (WIDTH // 2 - self.levels.get_width() // 2, 200, self.levels.get_width(), self.levels.get_height() // 2))
class PowerUp(object): def __init__(self, _x, _y, _nombre): self.bounds = Rect(_x, _y, 0, 0) self.animaciones = GestorAnimaciones() self.bounds.width, self.bounds.height = self.animaciones.añadir_animacion( _nombre, self.animaciones.cargar_frames("powerups/" + _nombre, 6), 200, 0) self.temporizador_movimiento = Timer(10) self.tipo = _nombre def dibujar(self, _pantalla): self.animaciones.render(_pantalla, self.bounds) def actualizar(self, _dt): self.animaciones.update(_dt) self.temporizador_movimiento.update(_dt) self.mover() def mover(self): if self.temporizador_movimiento.tick: self.bounds.move_ip(0, 1)
class Peach(object): """Objeto que representa a la princesa Peach""" def __init__(self): """Constructor""" self.__inimator = Animator() self.__bounds = Rect(250, 0, 0, 0) self.__bounds.size = self.__inimator.add_animation( "moving", 500, "peach/peach", 2, 2) self.__bounds.bottom = 79 # Elementos necesarios para mostrar el mensaje de HELP cada cierto tiempo self.__timer = Timer(550) self.__help = load_img("misc/help", 2) self.__showHelp = True def update(self, _dt): """Actualiza todos los elementos del objeto. Parámetros: _dt -- Tiempo en milisegundos que ha transcurrido desde la última vez que se llamó el método.""" self.__inimator.update(_dt) self.__timer.update(_dt) # Muestra u oculta el MSG de HELP cada vez que el temporizador emite un # pulso. if self.__timer.getTick(): self.__showHelp = not self.__showHelp def render(self, _screen): """Dibuja todos las partes que componen el objeto en la pantalla. Parámetros: _screen -- Superficie donde se dibujarán los elementos.""" self.__inimator.render(_screen, self.__bounds) # Dibuja o no el mensaje de HELP en función del valor de la bandera # 'showHelp'. if self.__showHelp: _screen.blit(self.__help, (290, 25))
class Enemies(object): def __init__(self): random.seed() self.enemies = Array() self.gfx = [ cargar_img("enemies/enemy_1", 2), cargar_img("enemies/enemy_3", 2), cargar_img("enemies/enemy_2", 2), cargar_img("enemies/bonus", 2) ] self.timer = Timer(2000) def update(self, _dt, _player, _UI): self.enemies.update(_dt) self.timer.update(_dt) if _player.alive: self.generate_enemy() self.attack(_player, _UI) else: self.enemies.array = [] self.remove_trash() def render(self, _screen): self.enemies.render(_screen) def generate_enemy(self): if self.timer.tick: enemy_type = random.randrange(len(self.gfx)) self.enemies.add_element( Enemy(random.randrange(170, 300), self.gfx[enemy_type], enemy_type)) self.timer.delay = random.randrange(2000, 3600, 100) def attack(self, _player, _UI): for element in self.enemies.array: element.vx = 0 if element.bounds.colliderect(_player.bounds): if element.type != 3: if element.vx > 0: _player.animator.reproducir_animacion( "hacia_derecha", 1) elif element.vx < 0: _player.animator.reproducir_animacion( "hacia_izquierda", 1) else: _player.explotar() else: _UI.score += 10 self.enemies.delete_element(element) if self.enemies.get_current_element() != None: if self.enemies.get_current_element( ).bounds.left > _player.bounds.right: self.enemies.get_current_element( ).vx = -self.enemies.get_current_element().speed elif self.enemies.get_current_element( ).bounds.right < _player.bounds.left: self.enemies.get_current_element( ).vx = self.enemies.get_current_element().speed else: self.enemies.get_current_element().vx = 0 def remove_trash(self): for enemy in self.enemies.array: if enemy.bounds.top > HEIGHT: enemy.alive = False
def __init__(self, _gestor_estados): self.fondo = cargar_img("fondos/intro") self.temporizador_parpadeo = Timer(250) self.parpadear = True self.gestor_estados = _gestor_estados