def generate_trap(b, row, column): # Here we just take a chance and put a trap here. # but we should actually explore the rest of the column for other suitable place if b.available_traps > 0: chance = int(b.max_traps_number / b.size[0] * 100) if random.randint(0, 100) >= 100 - chance: if random.choice([True, False]): trap = Wall( model=bg_color + traps_color + Graphics.Blocks.QUADRANT_UPPER_LEFT_AND_LOWER_RIGHT + Graphics.Blocks.QUADRANT_UPPER_RIGHT_AND_LOWER_LEFT + Graphics.Style.RESET_ALL, type="trap.hfire", ) trap.fire_timer = random.uniform(1.0, 4.0) b.place_item(trap, row, column) if isinstance(b.item(row, column - 1), BoardItemVoid): trap.fdir = Constants.LEFT else: trap.fdir = Constants.RIGHT else: trap = Wall( model=bg_color + Utils.red_bright( Graphics.Blocks. QUADRANT_UPPER_RIGHT_AND_LOWER_LEFT_AND_LOWER_RIGHT # noqa E501 + Graphics.Blocks. QUADRANT_UPPER_LEFT_AND_LOWER_LEFT_AND_LOWER_RIGHT # noqa E501 ) + Graphics.Style.RESET_ALL, type="trap.vfire", ) trap.fire_timer = random.uniform(2.0, 6.0) b.place_item(trap, row, column) trap.fdir = Constants.UP b.available_traps -= 1
def sprout(p, *args): # if p.parent is None: # return None if p is not None: o = Wall( model=bg_color + Graphics.Sprites.DECIDUOUS_TREE + Utils.Style.RESET_ALL, type="sprouted_trees", ) o.pos = p.pos # Projectile is own by the Board by default. # Board is owned by the Game object. if p.parent is not None and p.parent.parent is not None: p.parent.sprouted_count += 1 o.age = p.parent.sprouted_count p.parent.parent.obj_stack.append(o)
def change_level(params): game = params[0] board = Board( size=[250, 30], ui_borders="", ui_board_void_cell=bg_color + " " + Graphics.Style.RESET_ALL, player_starting_position=[25, 0], DISPLAY_SIZE_WARNINGS=False, ) board.ui_border_bottom = Graphics.Sprites.RADIOACTIVE + " " board.sprouted_count = 0 # Let's use a nice curve to increase the trap number. board.available_traps = int(10 + 10 * g.current_level * 0.2) board.max_traps_number = board.available_traps for i in range(0, board.size[0]): board.place_item( Wall(model=block_color() + " " + Utils.Style.RESET_ALL, type="platform"), board.size[1] - 1, i, ) generate_level(game, board) game.score += 50 * game.current_level new_level = game.current_level + 1 game.add_board(new_level, board) game.change_level(new_level) game.move_player(Constants.RIGHT, 3) game.player.last_y = game.player.pos[0] game.player.last_x = game.player.pos[1] g.player.max_y = g.player.pos[0] g.player.dy = gravity_speed g.obj_stack = []
def make_platform(b, row, column): psize = random.randint(2, 10) plateform = [] tmp_game = Game() # Only because Game needs it, we don't care. tmp_game.player = Player() tmp_game.add_board(0, b) tmp_game.change_level(0) # print( # f"[d] make_platform at {row}, {column}, psize is {psize} column will be " # f"between {column} and {column + psize + 1}" # ) get_up = 0 # for i in range(column, column + psize + 1): for i in range(column - psize - 1, column): if i >= b.size[0]: break if not isinstance(b.item(row, i), BoardItemVoid): break if i in b.visited_columns: break # Check if we have other platforms around. # If yes moving the platform up. if get_up < 3: for e in tmp_game.neighbors(2, Door(pos=[row, i])): if e.type == "ground": get_up = 3 break if get_up < 4: for e in tmp_game.neighbors(1, Door(pos=[row, i])): if e.type == "ground": get_up = 4 break m = block_color() + " " + Utils.Style.RESET_ALL plateform.append([Wall(model=m, type="platform"), row, i]) for i in plateform: b.place_item(i[0], i[1] - get_up, i[2]) if random.choice([True, False]): generate_treasure(b, i[1] - get_up - 1, i[2]) else: generate_trap(b, i[1] - get_up - 1, i[2]) b.visited_columns.append(i[2])
g.actuate_npcs(1) pf = PathFinder(game=g, actuated_object=g.player) pf.add_waypoint(dest_row, dest_col) pf.add_waypoint(24, 24) pf.add_waypoint(21, 40) pf.circle_waypoints = True pf.set_destination(dest_row, dest_col) blocker = NPC(model=Sprites.SKULL) g.current_board().place_item(blocker, 20, 1) wall = Wall(model=Sprites.WALL) wall.animation = Animation(animated_object=wall) wall.animation.add_frame(Sprites.BANKNOTE_DOLLARS) wall.animation.add_frame(Sprites.BANKNOTE_EUROS) wall.animation.add_frame(Sprites.BANKNOTE_WINGS) g.current_board().place_item(wall, 5, 25) # 43,28 43,34 39,34 39,40 44,40 44,28 patroller = NPC(model=Sprites.ALIEN, name='patroller') patroller.actuator = PathFinder(game=g, actuated_object=patroller) g.add_npc(1, patroller, 42, 28) patroller.actuator.set_destination(43, 29) patroller.actuator.add_waypoint(43, 29) patroller.actuator.add_waypoint(43, 34) patroller.actuator.add_waypoint(39, 34) patroller.actuator.add_waypoint(39, 40)
dg = RLDungeonGenerator(dg_width, dg_height) dg.generate_map() print("done") print("\rBuilding the map...", end="") random_board = Board( size=[dg_width, dg_height], name="Random dungeon", ui_borders=Utils.WHITE_SQUARE, ui_board_void_cell=Utils.BLACK_SQUARE, DISPLAY_SIZE_WARNINGS=False, ) potential_starting_position = [] for r in range(dg.height): for c in range(dg.width): if dg.dungeon[r][c].get_ch() == "#": random_board.place_item(Wall(model=Sprites.WALL), r, c) elif dg.dungeon[r][c].get_ch() == "+": random_board.place_item(Door(model=Sprites.DOOR), r, c) elif dg.dungeon[r][c].get_ch() == ".": potential_starting_position.append([r, c]) random_board.player_starting_position = choice(potential_starting_position) print("done") print(f"starting position chosen: {random_board.player_starting_position}") # dg.print_map() # random_board.display() input("New random dungeon generated. Next?") g = Game() g.enable_partial_display = True # g.load_board('/home/arnaud/Code/Games/hgl-editor/Large_Dungeon.json', 1)
# First let's create a Board that uses squares as delimiters # Borders are going to be white squares # Cells with nothing inside are going to be black squares myboard = Board( name="A demo board", size=[40, 20], ui_border_left=Utils.WHITE_SQUARE, ui_border_right=Utils.WHITE_SQUARE, ui_border_top=Utils.WHITE_SQUARE, ui_border_bottom=Utils.WHITE_SQUARE, ui_board_void_cell=Utils.BLACK_SQUARE, player_starting_position=[10, 20], ) # Now let's make a couple of walls using colored squares as models green_wall = Wall(model=Utils.GREEN_SQUARE) blue_wall = Wall(model=Utils.BLUE_SQUARE) cyan_wall = Wall(model=Utils.CYAN_SQUARE) magenta_wall = Wall(model=Utils.MAGENTA_SQUARE) # Now let's place these walls on the board for k in range(1, 6, 1): myboard.place_item(green_wall, 1, k) myboard.place_item(magenta_wall, k, 6) for k in range(2, 6, 1): myboard.place_item(blue_wall, k, 1) myboard.place_item(cyan_wall, 5, k) # And just for the fun of it, let's place a yellow and cyan wall # in the middle of that "structure"
global g g.clear_screen() nb_blocks = int((g.player.mp / g.player.max_mp) * 20) print("Mana [" + Utils.BLUE_RECT * nb_blocks + Utils.BLACK_RECT * (20 - nb_blocks) + "]") g.display_board() # manage_fireballs() b = Board( ui_borders=Utils.WHITE_SQUARE, ui_board_void_cell=Utils.BLACK_SQUARE, size=[20, 20], player_starting_position=[5, 5], ) wall = Wall(model=Sprites.WALL) b.place_item(wall, 1, 6) g = Game() g.add_board(1, b) g.player = Player(model=Sprites.MAGE, name="The Maje") g.player.mp = 20 g.player.max_mp = 20 g.change_level(1) key = None black_circle = "-\U000025CF" circle_jot = "-\U0000233E" throw_fireball = False projectile = Projectile()
life_heart = GenericActionableStructure(model=sprite_heart, name='life_100') life_heart.set_overlappable(True) life_heart.action = add_hp life_heart.action_parameters = [game, 100] life_heart_minor = GenericActionableStructure(model=sprite_heart_minor, name='life_25') life_heart_minor.set_overlappable(True) life_heart_minor.action = add_hp life_heart_minor.action_parameters = [game, 25] game.player = p # Adding walls to level 1 lvl1.place_item(Wall(model=sprite_wall), 2, 3) lvl1.place_item(Wall(model=sprite_wall), 2, 2) lvl1.place_item(Wall(model=sprite_wall), 2, 1) lvl1.place_item(Wall(model=sprite_wall), 2, 0) lvl1.place_item(Wall(model=sprite_wall), 3, 3) lvl1.place_item(Wall(model=sprite_wall), 4, 3) lvl1.place_item(Wall(model=sprite_wall), 5, 3) lvl1.place_item(Wall(model=sprite_wall), 6, 3) lvl1.place_item(Wall(model=sprite_wall), 6, 2) lvl1.place_item(Wall(model=sprite_wall), 6, 1) lvl1.place_item(Wall(model=sprite_wall), 7, 1) lvl1.place_item(Wall(model=sprite_wall), 8, 1) lvl1.place_item(Wall(model=sprite_wall), 8, 3) lvl1.place_item(Wall(model=sprite_wall), 9, 3) lvl1.place_item(Wall(model=sprite_wall), 10, 3) lvl1.place_item(Wall(model=sprite_wall), 10, 2)
def generate_level(g, b): # When we get the map, the last row is full of blocks last_alt = b.size[1] - 1 gap = 0 gap_threshold = 2 b.visited_columns = [] # We keep a count of the treasures we can put on the map as well as the rate we # can put them on (in percent). There is no possibility that we will end up with # more items, but there is a small possibility that we end up with less b.treasures = { "timers": { "count": int(math.log(g.current_level + 1)), "rate": 10, "placed": 0, }, "scorers": { "count": 1 + int(math.log(g.current_level + 1)), "rate": 10, "placed": 0, }, "1UP": { "count": 1, "rate": int(math.log(g.current_level + 1)), "placed": 0 }, "diamond": { "count": 1, "rate": 2, "placed": 0 }, } # We go through all the column of the map. for x in range(5, b.size[0]): # print(f"[d] generate level x={x}") # We have 50% chance of putting an obstacle. if random.choice([True, False]): if gap > gap_threshold: for k in range(x - gap, x): b.clear_cell(b.size[1] - 1, k) alt = random.randint(last_alt - 2, b.size[1] - 1) last_alt = alt y = 0 for y in range(alt + 1, b.size[1] - 1): # print(f"[d] generate level y={y}") b.place_item( Wall( model=block_color() + " " + Utils.Style.RESET_ALL, type="ground", ), y, x, ) # generate_treasure(b, y - 1, x) # For the fun of it and also to break monotony we have 20% of chance # of placing a platform here (of random size). We'll put treasures on # them later. if random.randint(0, 100) >= 80: make_platform(b, last_alt - random.randint(2, 3), x) # if y < b.size[1] - 1: # generate_trap(b, alt, x) # Just to break monotony, we have 33% chance of putting a cloud # in the background if random.randint(0, 100) >= 66: b.place_item( Door(model=bg_color + "\U00002601 " + Utils.Style.RESET_ALL), alt - 8 + random.randint(-2, 2), x, ) gap = 0 else: # If we didn't put an obstacle, we keep track of the gap we're leaving. # When the gap is spreading over a threshold, we have 50% chance of # emptying it (we make a pit) gap += 1 b.place_item( GenericActionableStructure( model=bg_color + Graphics.Sprites.CYCLONE + Utils.Style.RESET_ALL, type="exit", action=change_level, action_parameters=[g], ), last_alt - 1, b.size[0] - 1, ) # The map is done, let's add treasures and traps. # we travel the map once again and look for openings idx = 0 while b.available_traps > 0 or idx < 10: for col in range(5, b.size[0]): if random.choice([True, False]): candidates = [] for row in range(0, b.size[1]): item = b.item(row, col) # We only put treasures and traps on the ground if not isinstance(item, BoardItemVoid) and item.type == "ground": # We want at least 2 free cells in all directions (except down) free_cells = [ (row - 1, col), (row - 2, col), (row - 1, col - 1), (row - 1, col - 2), (row - 2, col - 1), (row - 2, col - 2), (row - 1, col + 1), (row - 1, col + 2), (row - 2, col + 1), (row - 2, col + 2), ] good_candidate = True for coord in free_cells: if coord[0] >= b.size[1] or coord[1] >= b.size[0]: continue cell = b.item(coord[0], coord[1]) if not isinstance(cell, BoardItemVoid): good_candidate = False break if good_candidate: candidates.append(item) for c in candidates: generate_trap(b, c.pos[0], c.pos[1]) idx += 1 idx = 0 while True: for col in range(5, b.size[0]): if random.choice([True, False]): candidates = [] for row in range(0, b.size[1]): item = b.item(row, col) # We only put treasures and traps on the ground if not isinstance(item, BoardItemVoid) and item.type == "ground": # We want at least 2 free cells in all directions (except down) free_cells = [ (row - 1, col), (row - 2, col), (row - 1, col - 1), (row - 1, col - 2), (row - 2, col - 1), (row - 2, col - 2), (row - 1, col + 1), (row - 1, col + 2), (row - 2, col + 1), (row - 2, col + 2), ] good_candidate = True for coord in free_cells: if coord[0] >= b.size[1] or coord[1] >= b.size[0]: continue cell = b.item(coord[0], coord[1]) if not isinstance(cell, BoardItemVoid): good_candidate = False break if good_candidate: candidates.append(item) for c in candidates: generate_trap(b, c.pos[0], c.pos[1]) idx += 1 if b.treasures["scorers"]["count"] <= 0 and b.treasures["timers"][ "count"] <= 0: break if idx == 10: break
from gamelib.Board import Board from gamelib.Structures import Wall walls = [] for i in range(0, 11): walls.append(Wall()) b = Board(size=[10, 10]) for i in range(0, 10): b.place_item(walls[i], i, i) b.place_item(Wall(model="*", type="round"), 0, 9) b.place_item(Wall(model="*", type="round"), 1, 9) print(b.get_immovables()) for i in b.get_immovables(type="round"): print(f"{i}: {i.name} pos: {i.pos} ({id(i)})") print("DISCARDING") b._immovables.discard(b.item(1, 9)) for i in b.get_immovables(type="round"): print(f"{i}: {i.name} pos: {i.pos} ({id(i)})")
g.remove_npc(1, o) redraw_screen() g.actuate_npcs(1) g.actuate_projectiles(1) g.animate_items(1) time.sleep(0.1) game_running = True b = Board( ui_borders=Graphics.WHITE_SQUARE, ui_board_void_cell=Graphics.BLACK_SQUARE, size=[20, 20], player_starting_position=[5, 5], ) wall = Wall(model=Graphics.Sprites.BRICK) b.place_item(wall, 1, 6) b.place_item(wall, 5, 10) g = Game() g.add_board(1, b) g.player = Player(model=Graphics.Sprites.MAGE) g.player.level = 1 g.player.mp = 20 g.player.max_mp = 20 g.add_npc( 1, NPC( name="Bob Soontobedead", model=Graphics.Sprites.SKULL, hp=10,