def __init__(self, driver, levels: List[Level]): super().__init__(driver) play_background() self.levels: List[Level] = levels self.level_num = 0 self.chars = CustomGroup() for player in self.driver.players: player.restart_char() self.chars.add(player.char)
def __init__(self, driver, level: Level): items = [ MenuText(text="Congratules!", size=100, color=TITLE_COLOR), MenuText(text="Press START to go back to Main Menu", size=40, color=SUBTITLE_COLOR), ] super().__init__(driver, Menu(driver, items)) self.level = level self.chars = CustomGroup() for player in self.driver.players: self.chars.add(player.char) jump_sound.set_volume(0) hit_sound.set_volume(0)
class Level: def __init__(self, coins=CustomGroup(), cannons=CustomGroup(), blocks=CustomGroup(), platforms=CustomGroup()): self.coins = coins self.blocks = blocks self.platforms = platforms self.cannons = cannons self.bullets = CustomGroup() for cannon in cannons: cannon.set_bullet_group(self.bullets) def update(self): self.cannons.update() self.bullets.update() return def detect_collisions(self, char): char.detect_collisions(self.blocks) char.detect_collisions(self.platforms) char.detect_collisions(self.cannons) self.blocks.detect_impacts(self.bullets) char.detect_impacts(self.bullets) char.detect_objectives(self.coins) return def draw(self, screen): screen.fill(BACKGROUND_COLOR) self.blocks.draw(screen) self.platforms.draw(screen) self.cannons.draw(screen) for objective in self.coins: objective.draw(screen) self.bullets.draw(screen) return
def __init__(self, coins=CustomGroup(), cannons=CustomGroup(), blocks=CustomGroup(), platforms=CustomGroup()): self.coins = coins self.blocks = blocks self.platforms = platforms self.cannons = cannons self.bullets = CustomGroup() for cannon in cannons: cannon.set_bullet_group(self.bullets)
def __init__(self, duration=30, coins=CustomGroup(), cannons=CustomGroup(), blocks=CustomGroup(), platforms=CustomGroup()): self.time = duration self.prep_time = 3 self.coins = coins self.blocks = blocks self.platforms = platforms self.cannons = cannons self.bullets = CustomGroup() for cannon in cannons: cannon.set_bullet_group(self.bullets)
class GameWon(MenuState): def __init__(self, driver, level: Level): items = [ MenuText(text="Congratules!", size=100, color=TITLE_COLOR), MenuText(text="Press START to go back to Main Menu", size=40, color=SUBTITLE_COLOR), ] super().__init__(driver, Menu(driver, items)) self.level = level self.chars = CustomGroup() for player in self.driver.players: self.chars.add(player.char) jump_sound.set_volume(0) hit_sound.set_volume(0) def tick(self, events): for char in self.chars: char.jump() self.chars.update() self.level.detect_collisions(self.chars) self.level.draw(self.driver.screen) self.chars.draw(self.driver.screen) self.menu.draw(self.driver.screen) return def press_start(self, player: Player): jump_sound.set_volume(1) hit_sound.set_volume(1) stop_background() self.driver.main_menu() return
class InGame(GameState): def __init__(self, driver, levels: List[Level]): super().__init__(driver) play_background() self.levels: List[Level] = levels self.level_num = 0 self.chars = CustomGroup() for player in self.driver.players: player.restart_char() self.chars.add(player.char) def tick(self, events): chars = self.chars level = self.levels[self.level_num] # mov automático chars.update() level.update() # colisiones level.detect_collisions(chars) # terminar nivel self.check_level_end() # dibujar level.draw(self.driver.screen) chars.draw(self.driver.screen) pass def press_up(self, player: Player): player.char.jump() return def hold_down(self, player: Player): player.char.fall() return def hold_left(self, player: Player): player.char.move_left() return def hold_right(self, player: Player): player.char.move_right() return def press_start(self, player: Player): self.driver.pause(self) return def press_primary(self, player: Player): player.char.jump() return def check_level_end(self): chars = self.chars level = self.levels[self.level_num] if level.is_over(chars): level.end(chars) self.level_num += 1 if self.level_num >= len(self.levels): if len(chars) > 0: self.driver.game_won(level) else: self.driver.game_over(level) if len(chars) == 0: self.driver.game_over(level) return
def load_level2(level): blocks = CustomGroup() for block in level["blocks"]: blocks.add(Block(**block)) platforms = CustomGroup() for platform in level["platforms"]: platforms.add(Platform(**platform)) cannons = CustomGroup() for cannon in level["cannons"]: cannons.add(Cannon(**cannon)) coins = CustomGroup() for coin in level["coins"]: coins.add(Coin(**coin)) return Level(duration=level["duration"], coins=coins, cannons=cannons, blocks=blocks, platforms=platforms)
class Level: def __init__(self, duration=30, coins=CustomGroup(), cannons=CustomGroup(), blocks=CustomGroup(), platforms=CustomGroup()): self.time = duration self.prep_time = 3 self.coins = coins self.blocks = blocks self.platforms = platforms self.cannons = cannons self.bullets = CustomGroup() for cannon in cannons: cannon.set_bullet_group(self.bullets) # TODO: settings level char positions def update(self): if self.prep_time > 0: self.prep_time -= 1.0 / FPS return self.time -= 1.0 / FPS self.cannons.update() self.bullets.update() return def detect_collisions(self, chars): chars.detect_collisions(self.blocks) chars.detect_collisions(self.platforms) chars.detect_collisions(self.cannons) for _ in chars: chars.detect_collisions(chars) self.blocks.detect_impacts(self.bullets) chars.detect_impacts(self.bullets) # no se pueden agarrar objetivos durante la preparación if self.prep_time <= 0: chars.detect_objectives(self.coins) return def draw(self, screen): screen.fill(BACKGROUND_COLOR) self.blocks.draw(screen) self.platforms.draw(screen) number_color = TIMER_COLOR if self.time < 5: number_color = TIMER_ENDING_COLOR Text(str(round(self.time, 1)), x=SCREEN_WIDTH / 2, y=16, color=number_color, center=True, size=30) \ .draw(screen) self.cannons.draw(screen) # no se pueden agarrar objetivos durante la preparación if self.prep_time <= 0: for objective in self.coins: objective.draw(screen) else: # mostrar tiempo de preparación Text(str(int(self.prep_time) + 1), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, TIMER_COLOR, center=True, size=int(80 + self.prep_time % 1 * 60)).draw(screen) self.bullets.draw(screen) return def is_over(self, characters): if self.time <= 0: return True if len(self.coins) == 0: return False for char in characters: if len(char.objectives) < len(self.coins): return False return True def end(self, characters): for char in characters: if len(char.objectives) < len(self.coins): char.kill() else: char.clear_objectives() return
def load_level(level_json): with open(level_json) as f: level = json.load(f) blocks = CustomGroup() for block in level["blocks"]: blocks.add(Block(**block)) platforms = CustomGroup() for platform in level["platforms"]: platforms.add(Platform(**platform)) cannons = CustomGroup() for cannon in level["cannons"]: cannons.add(Cannon(**cannon)) coins = CustomGroup() for coin in level["coins"]: coins.add(Coin(**coin)) return Level(coins=coins, cannons=cannons, blocks=blocks, platforms=platforms)