def __init__(self, surface, position_x=Constants.DEFAULT_PLAYER_XY, position_y=Constants.DEFAULT_PLAYER_XY, health=Constants.DEFAULT_PLAYER_HP, speed=Constants.DEFAULT_PLAYER_SPEED, bomb_amount=Constants.DEFAULT_PLAYER_BOMB_AMOUNT_RANGE, bomb_range=Constants.DEFAULT_PLAYER_BOMB_AMOUNT_RANGE, image_name=Constants.HERO_IMG_PATH): self.position_x = position_x self.position_y = position_y self.health = health self.speed = speed self.bomb_amount = bomb_amount self.bomb_range = bomb_range self.position_x_change = 0 self.position_y_change = 0 self.bomb_list = [] self.character_image = pygame.transform.scale( (pygame.image.load(image_name).convert_alpha()), (Constants.IMG_SCALING_WIDTH_HEIGHT, Constants.IMG_SCALING_WIDTH_HEIGHT)) self.is_alive = True self.default_position = init.Point(position_x, position_x) self.last_position = init.Point(1, 1) self.screen = surface
def test_marking_player_on_map(self): temp = False init.mark_player_on_map(self.player) self.player.position_x = 100 init.mark_player_on_map(self.player) if init.game_map[1][1] == ' ' and init.game_map[2][1] == 'P': temp = True self.assertTrue(temp)
def test_remove_block_true(self): self.ghosts_list = [] self.ghosts_list.clear() self.ghosts_list.append(self.ghost_1) init.generate_map(init.game_map) bomb = Bomb.Bomb(self.screen, 150, 100, 10, 10) while not bomb.explosion(self.ghosts_list, self.player.get_player_position_on_map(), self.player.reduce_health_by_one): pass self.assertEqual(init.game_map[3][3], Constants.CLEAR)
def setUpClass(cls): pygame.init() pygame.mixer.init() cls.screen = pygame.display.set_mode((750, 750)) Constants.Assets.load() cls.ghost_1 = Characters.Ghost(cls.screen, 650, 50, 1, 4, 2, 1, Constants.WHITE_GHOST_PATH, Constants.EASY) cls.ghost_2 = Characters.Ghost(cls.screen, 50, 650, 1, 3, 2, 1, Constants.BLUE_GHOST_PATH, Constants.MEDIUM) cls.ghost_3 = Characters.Ghost(cls.screen, 650, 650, 1, 1, 1, 1, Constants.RED_GHOST_PATH, Constants.HARD) cls.ghosts_list = [] init.generate_map(init.game_map)
def set_labels_in_game(self, player, level): """ Function for settting upper labels :param player: :param level: :return: """ # upper info bar #Constants.screen.blit(Constants.TRANSPARENT_SURFACE, (0, 0)) init.print_label(self.__screen, "Player's lifes:", 0, 0, 20) for x in range(0, player.health): self.__screen.blit(Constants.Assets.HEART_IMG, (x * 40, 20)) init.print_label( self.__screen, "Bombs amount: {} Bombs' range: {} Level: {}".format( str(player.bomb_amount), str(player.bomb_range), str(level)), 200, 15, 30)
def __init__(self, surface, position_x, position_y, health, speed, bomb_amount, bomb_range, image_name, mode): super(Ghost, self).__init__(surface, position_x, position_y, health, speed, bomb_amount, bomb_range, image_name) self.character_image = pygame.transform.scale( (pygame.image.load(image_name).convert_alpha()), (44, 44)) self.distance_traveled = 0 self.current_direction = None self.possible_movements = [] self.last_positions = [] self.last_positions.append( init.Point(((self.position_x + Constants.BLOCK_SIZE) // Constants.BLOCK_SIZE - 1), ((self.position_y + Constants.BLOCK_SIZE) // Constants.BLOCK_SIZE - 1))) self.is_alive = True self.mode = mode self.default_position = init.Point(position_x, position_y) self.last_move = None
def menu(self): """ Main menu function that handles buttons """ self.__show_about = False pygame.mixer.music.play(loops=-1) while True: self.draw_menu() init.print_about_game(self.__show_about, self.__screen) pygame.display.update() for event in pygame.event.get(): pos = pygame.mouse.get_pos() if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.MOUSEBUTTONDOWN: if self.__start_button.mouse_hover(pos): pygame.mixer.music.stop() pygame.mixer.music.rewind() return if self.__end_button.mouse_hover(pos): pygame.quit() quit() if self.__about_button.mouse_hover(pos): if not self.__show_about: self.__show_about = True else: self.__show_about = False if event.type == pygame.MOUSEMOTION: if self.__start_button.mouse_hover(pos): self.__start_button.color = Constants.DARK_BLUE else: self.__start_button.color = Constants.BLUE if self.__end_button.mouse_hover(pos): self.__end_button.color = Constants.RED else: self.__end_button.color = Constants.BURGUNDY if self.__about_button.mouse_hover(pos): self.__about_button.color = Constants.DARK_GREEN else: self.__about_button.color = Constants.BRIGHT_GREEN
def get_explosion_blocks_horizontally(self, blocks_to_destroy, current_y, start_point_x, direction): """ Function scanning horizontally blocks, where it should place fire animation of the bomb CAUTION: IT RETURNS BLOCKS AS A LIST IN THROUGH THE PARAMETER :param blocks_to_destroy: output list :param direction: -1 if left, 1 if right """ iterator = start_point_x i = 0 y_coordinate = current_y * Constants.BLOCK_SIZE while True: if init.game_map[iterator][current_y] == Constants.STONE: init.game_map[iterator][current_y] == Constants.CLEAR blocks_to_destroy.append((iterator, current_y)) break if init.game_map[iterator][current_y] == Constants.WALL: break if i > self.range_field: break if init.game_map[iterator][current_y] == Constants.CLEAR: if self.is_intersection(iterator * Constants.BLOCK_SIZE, y_coordinate): self.fire_blocks.append( init.FireTuple(iterator * Constants.BLOCK_SIZE, y_coordinate, self.INTERSECTION, None)) elif self.is_vertical(iterator * Constants.BLOCK_SIZE, y_coordinate): self.fire_blocks.append( init.FireTuple(iterator * Constants.BLOCK_SIZE, y_coordinate, self.STRAIGHT, self.VERTICAL)) elif self.is_horizontal(iterator * Constants.BLOCK_SIZE, y_coordinate): self.fire_blocks.append( init.FireTuple(iterator * Constants.BLOCK_SIZE, y_coordinate, self.STRAIGHT, self.HORIZONTAL)) iterator += direction i += 1
def main_game(self, player, ghosts_list, level): """ Function for handing main game :param player: player object :param ghosts_list: list of ghosts :param level: current level :return: True if player loses, False it player wins """ last_time = time.time() last_time_collision_with_ghost = time.time() init.generate_map(init.game_map) init.place_stones(self.__screen) transparent_surface = pygame.Surface((750, 50)) transparent_surface.set_alpha(128) while True: start_time = time.time() self.__screen.fill((0, 0, 0)) self.__screen.blit(Constants.Assets.BACKGROUND_IMG, (0, 0)) self.__screen.blit(transparent_surface, (0, 0)) self.set_labels_in_game(player, level) init.place_stones(self.__screen) player.handle_movement() init.mark_player_on_map(player) self.handle_all_ghosts(ghosts_list) if self.leave_button_handler(): return True if last_time is not None: now = time.time() if now - last_time > 0.5: # 0.5 if player.is_bomb_added_to_list(): last_time = now if time.time( ) - last_time_collision_with_ghost > self.CHECK_COLLISION_DELAY: player.collision_with_ghosts(ghosts_list) last_time_collision_with_ghost = time.time() player.check_explosion(ghosts_list) player.set_bombs_on_map() end_time = (start_time - time.time()) * 1000 self.end_func() # clock.tick(60) if self.control_end_game(player): Constants.Assets.LOSE_SOUND.play() return True if self.control_end_level(ghosts_list): Constants.Assets.WIN_SOUND.play() return False pygame.display.update() pygame.time.wait(int(end_time))
def following_player(self): """ Ghost trying to follow the player """ try: if not self.possible_movements: path_queue = init.find_shortest_path( init.game_map, self.get_position_on_map()) if path_queue is not None: self.set_possible_movements_for_following_ghost(path_queue) pressed = self.possible_movements[0] for key, direction in X_SPEED_CHANGE.items(): if pressed == key: self.position_x_change = direction * self.speed if self.position_x_change < 0: self.collision_x(self.position_x) else: self.collision_x(self.position_x + self.character_image.get_width()) self.position_y_change = 0 for key, direction in Y_SPEED_CHANGE.items(): if pressed == key: self.position_y_change = direction * self.speed if self.position_y_change < 0: self.collision_y(self.position_y) else: self.collision_y(self.position_y + self.character_image.get_height()) self.position_y_change = 0 self.distance_traveled += self.speed if self.distance_traveled >= self.MAX_MOVEMENT: self.distance_traveled = 0 del self.possible_movements[0] except IndexError: pass except: print("Unknow error in following ghost") finally: self.set_position(self.position_x, self.position_y)
def setUpClass(cls): pygame.init() cls.screen = pygame.display.set_mode((750, 750)) Constants.Assets.load() init.generate_map(init.game_map)
def main(): pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((750, 750)) Constants.Assets.load() pygame.display.set_icon(Constants.Assets.ICON_IMG) pygame.display.set_caption('Bomberman') pygame.mixer.music.load('Sounds/TheFatRat-Xenogenesis.wav') pygame.mixer.music.set_volume(0.2) menu = Menu.Menu(screen) game = MainGame.MainGame(screen) while True: menu.menu() main_hero = Characters.Player(screen, 50, 50, 5, 8, 13, 13, Constants.HERO_IMG_PATH) init.game_map.clear() init.generate_map(init.game_map) ghosts_list = [] init.game_map[main_hero.last_position.x][ main_hero.last_position.y] = Constants.GOAL init.place_stones(screen) transparent_surface = pygame.Surface((750, 750)) transparent_surface.set_alpha(128) transparent_surface.fill((0, 0, 0)) screen.blit(Constants.Assets.BACKGROUND_IMG, (0, 0)) level_iterator = 1 main_hero.set_position(main_hero.default_position.x, main_hero.default_position.y) init.place_stones(screen) for ghost in ghosts_list: ghost.set_position(ghost.default_position.x, ghost.default_position.y) screen.blit(transparent_surface, (0, 0)) init.print_label(screen, "{} {}".format("LEVEL ", str(level_iterator)), 300, 300, 40) pygame.display.update() pygame.time.wait(1000) while True: ghost_easy = Characters.Ghost(screen, 650, 50, 1, 4, 2, 1, Constants.WHITE_GHOST_PATH, Constants.EASY) ghost_medium = Characters.Ghost(screen, 50, 650, 1, 3, 2, 1, Constants.BLUE_GHOST_PATH, Constants.MEDIUM) ghost_hard = Characters.Ghost(screen, 650, 650, 1, 1, 1, 1, Constants.RED_GHOST_PATH, Constants.HARD) ghosts_list.clear() ghosts_list.append(ghost_easy) ghosts_list.append(ghost_medium) ghosts_list.append(ghost_hard) if not game.main_game(main_hero, ghosts_list, level_iterator): level_iterator += 1 ghosts_list.clear() ghosts_list.append(ghost_easy) ghosts_list.append(ghost_medium) ghosts_list.append(ghost_hard) for ghost in ghosts_list: ghost.set_to_default() main_hero.set_to_default() if level_iterator > 5: if main_hero.bomb_amount > 1: main_hero.reduce_bomb_amout_by_one() main_hero.reduce_bomb_range_by_one() if level_iterator > 10: if main_hero.speed >= 3: main_hero.reduce_speed_by_one() if level_iterator > 15: if main_hero.health > 1: main_hero.reduce_health_by_one() init.game_map.clear() init.generate_map(init.game_map) init.place_stones(screen) screen.blit(transparent_surface, (0, 0)) init.print_label(screen, "{} {}".format("LEVEL ", str(level_iterator)), 300, 300, 40) pygame.display.update() pygame.time.wait(3000) else: screen.blit(transparent_surface, (0, 0)) init.print_label(screen, "YOU'VE LOST", 260, 320, 60) pygame.display.update() pygame.time.wait(2000) break